csharpc

System.Collections.Generic.Dictionary

A library or CoreLib type served from the framework’s IL: its row of the per-library table of ADR 0023 (columns explained there, under “Measurements”).

Library (switch) Correctness (samples from IL) Binary size, IL / shadow Compile cpu, IL / shadow Speed, IL / shadow (time) Lines removed on a flip Decision NativeAOT on the same host, csharpc / NativeAOT (tools/il-first/measure-aot.sh: stripped size; compile cpu; run, median of 7; instructions)
System.Collections.Generic.Dictionary<TKey,TValue>, HashSet<T> and System.Runtime.InteropServices.CollectionsMarshal (CoreLib types, #274) the 116 samples that mention them (Dictionary, HashSet, ToLookup, GroupBy, Distinct, JSON, Linq, Roslyn library members…) pass at -O0 and -O2, 36 Int128/Vector128/BigInteger samples too, tests/slicer.sh (11 slice runs) passes; three new samples recorded from real .NET for what the flip exposed (1305: boxed Index/Range as keys and object, an Int128 in a boxed struct and a Dictionary<Int128,V> enumerator boxed through IEnumerable, Comparer<Int128>.Default which threw InvalidOperationException; 1306: Comparer of string collections and ReadOnlySpan<char> alternate lookups; 1307: the missing-key and duplicate-key messages for 18 key types), found along the way: #277 (SortedDictionary duplicate-key message) +5 % to +14 % on programs that format a number, because the corelib’s culture caches are Dictionary<string, ...> and now carry the real Resize/Remove/enumerator code and tables: linux-x86_64 async 156 544 to 172 144 B (+10.0 %), collections 143 448 to 163 032 (+13.7 %), linq 174 600 to 189 864 (+8.7 %), numbers 129 096 to 143 912 (+11.5 %), regex 825 600 to 870 304 (+5.4 %), regexoptions 1 949 520 to 2 093 104 (+7.4 %); hello, exceptions, typeinfo unchanged. A 4-collection program (Dictionary<string,int>, HashSet<int>, HashSet<string>, Dictionary<int,int>) 145 056 to 155 744 B (+7.4 %). Without the two profile replacements below it was +46 % (212 368 B) collections 3.92 to 4.08 s (+4 %), linq 4.59 to 4.95 s (+8 %) CPU instructions, same program, work beyond start-up: Dictionary<string,int> set/get/remove 33.5 M to 33.9 M (+1.4 %), HashSet<int> add/contains 69.2 M to 67.4 M (-2.5 %), HashSet<string> 28.1 M to 31.0 M (+10 %), Dictionary<int,int> 93.7 M to 84.3 M (-10 %); the spike before the type-guarded default comparer (#269) was 21 to 31 % behind 1 425 deleted and 71 added in corelib (HashSet.cs and CollectionsMarshal.cs deleted, DictionaryImpl out of Dictionary.cs; added: contract members in UpstreamSupport.cs), against 1 431 deleted and 218 added over src and tools (the compiler side: boxed Index/Range method tables, 8-aligned Int128 aggregates, Comparer<Int128>, IL folding of typeof(A) == typeof(B), generic forwarding in the slicer) Flipped. Two things the flip decides: (1) Security (hash flooding, #285): the string comparer family is csharpc’s own (redirected contract classes, corelib/UpstreamSupport.cs) because upstream’s reaches ICU casing and GenericEqualityComparer. As in .NET, Dictionary<string,V>/HashSet<string> with the default, StringComparer.Ordinal or OrdinalIgnoreCase comparer start on the fast non-randomized hash (string.GetNonRandomizedHashCode) and, once an operation meets more than 100 collisions, switch to a randomized comparer and rehash; string.GetHashCode() is Marvin seeded per process from the OS random number generator (getentropy/rand_s; a failing generator stops the program), where it used to be a fixed multiplicative hash that an attacker could build collisions against. Sample 1308 forces the switch with colliding keys built from the documented hash (200 000 keys; without the switch it does not finish in minutes: checked by making the randomized hash the fast one, 40 s and still running) and tests/hashseed.sh checks that processes seed differently. Costs: string.GetHashCode() is Marvin (slower than the old hash on programs that hash strings with it; Dictionary<string,V> itself keeps the fast hash), HashSet<string>/Dictionary<string,V> net instructions Dictionary<string,int> 33.9 M to 33.1 M and HashSet<string> 31.0 M to 30.4 M (the C# non-randomized hash replaced the C one in the guard), binary +4 KB on a program with four collections. The wrappers for Ordinal/OrdinalIgnoreCase are made by those comparers, so a program that never creates them does not carry their code. (2) The throw helpers that format the key with string.Format are replaced (csharpc profile) by direct text for string, int, long, uint, ulong and char keys and ToString/IFormattable for the rest, so a program reading a Dictionary does not carry composite formatting and every number formatter. The size cost is the owner’s call under the standing authorisation; a follow-up could replace the culture caches’ Dictionaries with something smaller. collections_throughput: 155 864 / 1 349 448 B; compile cpu 5.3 / 15.6 s; run 353 / 688 ms (0.51x); 2.96 G / 3.12 G instructions