General Performance Tip: Comparing ComputeHash(), HashData(), and TryHashData() Methods

Developers can simplify hashing byte arrays by using HashData() instead of ComputeHash(), as it requires less code and eliminates the need to manage the SHA256 object. Benchmarking shows HashData() is about 1.16x faster than ComputeHash() and 1.02x faster than TryHashData(). EditorConfig helps manage related diagnostics.

General Performance Tip: Creating an Object

In this investigation of object creation in .NET, various methods were explored, including "new" keyword, Activator.CreateInstance, and RuntimeHelpers.GetUninitializedObject.

dotNetDave Rocks Warsaw IT Days 2024

I'm excited to announce that, for the first time ever, the For Those About To Code: World Wide Tour will be making a virtual stop at the 2024 Warsaw IT Days event in Warsaw, Poland, taking place from April 5th to 6th!

General Performance Tip: Avoid Unnecessary Variable Initialization

In the analysis of code, it's noted that explicit initialization of variables, when the default state is already set, can impact performance. Situations requiring variable initialization are discussed, with examples showing unnecessary explicit initialization. The default values for reference types and the default values for properties of a custom type are also examined.

String Performance: Comparing Strings with Globalization

In the String Performance chapter of the code performance book, the author demonstrates the usage of string.Compare() with StringComparison. Benchmark results are provided, along with a recommendation to use cultures or ordinals based on the strings being compared. The chapter also touches on setting up EditorConfig for dotnet_diagnostic.CA1862.

String Performance: String Compression

The content discusses the significance of string compression in programming, outlining four available data formats and demonstrating how to compress and decompress strings using Brotli. It also mentions benchmark results and a tool called Spargine for streamlining the process. For more details, visit the provided link and subscribe for access to exclusive content.

Collection Performance: Iterating Through Reference Value, and Record Types

The author delves into the performance disparities of using reference types, value types, and record types in collections.

General Performance Tip: Constant vs Variable

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: Null Coalescing Assignment

In C# 8, a new method for null coalescing assignments was introduced, simplifying the code. Performance benchmark results show a slight advantage in using the traditional approach for null checking compared to the new method. Previously, the performance difference between the two methods was negligible.

General Performance Tip: Performance Impact of Sealing Attributes

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.