System.Collections.Hashtable
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”). Measured for #367 and not flipped then;
flipped by #373 (the shadow corelib/Hashtable.cs is deleted). ArrayList and BitArray, the other System.Collections types of CoreLib, are
flipped with it (#372, System.Collections.ArrayList.md).
| 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.Hashtable (src/Csharpc/upstream-types/System.Collections.Hashtable.txt, #373) |
the Hashtable samples (102, 504, 518, 547, 601, 1160, 1312 and the rest of the collections set) byte-identical at -O0, -O2, via IL and on wasm32. New sample 1342 (recorded from .NET): the duplicate-key message for keys of every kind (string, primitives, DateTime, Guid, an enum, an IFormattable, an ISpanFormattable whose TryFormat and ToString differ, a type with only ToString, a null ToString); the shadow differs from .NET on it (it concatenated key.ToString(), where .NET’s string.Format takes ToString(null, provider) / TryFormat) |
hashtable_throughput 145 512 to 181 336 B (1.25x: upstream’s reader-consistency loop and KeyEquals/InitHash paths, and the duplicate-key message formatted exactly, which brings the boxed primitives’ TryFormat); size programs: regex 834 376 to 857 320 B and regexoptions 2 070 056 to 2 112 808 (Regex keeps its capture names in a Hashtable), budgets raised to measured + 3 % |
5.9 / 8.4 s (same host, same base) | hashtable_throughput: 9 202 M to 9 746 M instructions (1.06x), median of 5 1 262 to 1 335 ms |
523 (corelib/Hashtable.cs) |
From IL, flipped. SR.Format (here Argument_AddingDuplicate__) goes to UpstreamSupport.Format in the csharpc profile: the {n} / {n:spec} grammar, arguments formatted as string.Format formats them (the provider’s ICustomFormatter, ISpanFormattable, IFormattable, ToString, "" for null), without composite formatting’s parser and per-type switch. The slicer installs it only when it proves every SR.Format call of the slice formats a CoreLib resource text of that grammar (the format argument traced back to an SR getter or literal in straight-line IL, Emit.SrFormatIsSimple); otherwise SR.Format keeps string.Format. Over the whole pinned CoreLib: 559 SR.Format call sites, 543 traced to a resource text, all 543 of the grammar (16 compute their format, none of them in the slice). The 6 % instruction cost is upstream’s: get_Item re-reads the bucket until no writer is in progress and the version is unchanged (volatile _isWriterInProgress/_version, SpinWait), the single-writer/multi-reader guarantee .NET documents for Hashtable, which the shadow did not have. Security: that reader-consistency guarantee is gained; buckets are array-indexed with checks. |
hashtable_throughput: 181 064 / 1 353 576 B; compile cpu 25.9 / 15.0 s (a loaded host: 5.9 s alone above); run 1 387 / 2 931 ms (0.47x); 9.67 G / 20.3 G instructions |
(#367, the first try: the measurements before the SR.Format contract) System.Collections.Hashtable |
correct: with it listed, the Hashtable samples (102, 504, 518, 601 and the rest of the 104-sample set) are byte-identical to real .NET at -O0 and -O2 | hashtable_throughput 145 416 to 188 744 B (1.30x): Insert’s duplicate-key message SR.Format(Argument_AddingDuplicate__, key, key) formats objects through composite formatting, which brings in every primitive’s formatter, culture data and decimal/BigInteger formatting |
4.4 to 5.7 s | hashtable_throughput (20 rounds: 200 k inserts, 1 M lookups with misses, enumeration, 100 k Remove, string keys): instructions 9 391 M to 9 648 M (1.03x), median of 5 1 103 to 1 133 ms |
(523, corelib/Hashtable.cs) |
Not flipped: slower and 30 % larger, and the shadow is already a port of .NET’s bucket layout (same order, same growth). #373: replace the SR.Format call (a general slicer replacement that formats {n} items without composite formatting would serve every sliced type), then flip. |
the shadow, as shipped: hashtable_throughput 145 176 / 1 353 576 B; compile cpu 4.2 / 10.9 s; run 1 016 / 2 943 ms (0.35x); 9.39 G / 20.5 G instructions |