.NET 8 — FrozenDictionary and FrozenSet Benchmark

Victor Magalhães
3 min readSep 17, 2023
.NET 8 — FrozenDictionary and FrozenSet

With the arrival of .NET 8, two new collections will be introduced: FrozenDictionary and FrozenSet. These collections have been designed to enhance efficiency in read operations, as stated by Microsoft:

Microsoft: https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8

.NET 8 introduces several new types aimed at improving app performance.

The new System.Collections.Frozen namespace includes the collection types FrozenDictionary<TKey,TValue> and FrozenSet<T>. These types don’t allow any changes to keys and values once a collection created. That requirement allows faster read operations…

You can convert an IEnumerable into a FrozenDictionary using the .ToFrozenDictionary() method or transform it into a FrozenSet using the .ToFrozenSet() method.

Benchmark

To confirm the performance improvement, some benchmarks will be conducted.

Device Setup

Here, the device settings that conducted the tests will be displayed.

Device Setup

FrozenDictionary

Test Scenario

private const int OneMilion = 1_000_000;
private readonly Dictionary<int, int> _dictionary = Enumerable.Range(0, OneMilion).ToDictionary(x => x);
private readonly FrozenDictionary<int, int> _frozenDictionary = Enumerable.Range(0, OneMilion).ToFrozenDictionary(x => x);
private readonly ImmutableDictionary<int, int> _immutableDictionary = Enumerable.Range(0, OneMilion).ToImmutableDictionary(x => x);

The TryGetValue method will be used to assess the performance of each of the collections.

Test Result

FrozenDictionary TryGetValue method benchmark

Smaller values are better:

— Mean: Arithmetic mean of all measurements

— Error: Half of 99.9% confidence interval

— StdDev: Standard deviation of all measurements

— Rank: Relative position of current benchmark mean among all benchmarks

The FrozenDictionary secured the first place with an impressive approximately 50% improvement in processing speed compared to the second-place Dictionary. This signifies a significant enhancement in efficiency and performance for the operation in question.

FrozenSet

Test Scenario

private const int OneMilion = 1_000_000;
private readonly HashSet<int> _hashSet = Enumerable.Range(0, OneMilion).ToHashSet();
private readonly FrozenSet<int> _frozenSet = Enumerable.Range(0, OneMilion).ToFrozenSet();
private readonly ImmutableHashSet<int> _immutableHashSet = Enumerable.Range(0, OneMilion).ToImmutableHashSet();

The TryGetValue method will be used to assess the performance of each of the collections.

Test Result

FrozenSet TryGetValue method benchmark

Smaller values are better:

— Mean: Arithmetic mean of all measurements

— Error: Half of 99.9% confidence interval

— StdDev: Standard deviation of all measurements

— Rank: Relative position of current benchmark mean among all benchmarks

The FrozenSet claimed the top position with an approximately 17% increase in processing speed when compared to the second-place HashSet. This underscores a substantial improvement in both efficiency and overall performance for the specific operation being assessed.

Conclusion

The test results have been positive, demonstrating a significant improvement in performance for the new collections, but please note that test results may vary depending on the specific machine and the test scenario used.

--

--