Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Discussion

FrozenDictionary and FrozenSet in .NET 8: Optimizing Read-Only Lookups to Maximum Speed [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
прадстаўнік: 103
Дата далучэння: Apr 2018
Паведамленні: 40
Дзякуй: 24
1 месяцаў таму · Jul 24, 2026 6:57 AM
#1

When your dictionary is initialized once at startup and read millions of times during runtime:

Standard Dictionary<TKey, TValue> handles dynamic insertions and resizes. FrozenDictionary<TKey, TValue> analyzes key distributions during construction and generates specialized, branchless hash lookup routines.

CSHARP
var readonlyDict = originalDict.ToFrozenDictionary();

Benchmarks: Lookups on FrozenDictionary are 25% to 50% faster than standard Dictionary and allocate 0 bytes during reads!

profiler_pat
Performance Hunter
MEMBER
прадстаўнік: 146
Дата далучэння: Aug 2019
Паведамленні: 33
Дзякуй: 31
1 месяцаў таму · Jul 24, 2026 9:37 AM
#2

Perfect for configuration settings, routing tables, and static lookup tables loaded during application startup.

generic_math_geek
C# 11 Math Guru
MEMBER
прадстаўнік: 138
Дата далучэння: Jul 2021
Паведамленні: 10
Дзякуй: 88
1 месяцаў таму · Jul 24, 2026 7:52 PM
#3

The key optimization is that FrozenDictionary picks specialized lookup algorithms based on whether keys are short strings, integers, or Guids.