Reference Type & Structure Performance: Hashing Classes, Records, and Structures

A SHA256 hash can be generated for a class, record, or structure using a specific code. Performance analysis shows that hashing a class is 1.032 times faster than a structure and 1.048 times more efficient than a record. The memory allocations for record, reference, and value types are also provided in bytes.

General Performance Tip: Cloning Various Types

The post discusses the process of cloning different types of objects, with Microsoft recommending the use of JSON serialization for this purpose. The author explores the performance differences in cloning reference types, value types, and record types. Benchmark results using JsonSerializer reveal that cloning reference types is 1.015 times faster than cloning record types and 1.24 times more performant than cloning value types. The findings are shared for reference.

String Performance: Optimizing String Substring Extraction – Slicing vs. AsSpan()

The content presents an alternative approach to obtaining a substring using slicing with ReadOnlySpan and AsSpan() methods.

General Performance Tip: Optimizing Method Returns

When customizing attributes, Microsoft and I both advise sealing them for clarity and performance. However, benchmark results show sealing attributes to be marginally less performant, with both outcomes indicating a memory allocation of 24 bytes. Despite this, I maintain that sealing non-inheritable classes is essential for robust class design.

Collection Performance: Comparing Key Search Methods in Dictionaries

The post discusses two methods for searching a specific key in a Dictionary. The ContainsKey() method is recommended by Microsoft for improved performance but is it more performant?

String Performance Tip: Verifying if an Object is a String

The post explains two methods to confirm if an object is a string. It provides code examples for both methods and states that the first method is 1.17 times more efficient than pattern matching.

General Performance Tip: Constant vs Read-Only Property

It is recommended to use constants for static numerical or string values in code. This helps maintain code clarity and adhere to best practices. Benchmark tests show variables are slightly more performant than constants, but it is still advisable to use constants where appropriate to reflect the intent of the variable.

General Performance Tip: Generating Random Numbers

.NET's Random type has been a popular choice for generating random numbers. However, using RandomNumberGenerator for this purpose can result in a six-fold performance improvement, as shown in benchmark tests. Both methods allocate the same amount of memory.

General Performance Tip: Enhanced Logging Approach

The article discusses advancements in logging techniques in .NET, particularly with the new source generator in .NET 6 that enhances performance using LoggerMessage. This new method improves logging efficiency by 11.25 times compared to previous approaches. It emphasizes the importance of extensive logging for issue resolution in code.

General Performance Tip: Handling Exceptions

In my presentation at the Silicon Valley Code Camp, a question about the performance of using the When() clause for catching exceptions prompted me to conduct performance testing. While traditional exception handling is crucial, my testing revealed that the When() clause is more performant when capturing multiple similar exceptions.