Repository navigation
Hash#dup, #merge, #replace, Hash[]: copy entries without re-hashing keys - #83
Conversation
IDictionaryOps.ReplaceData, unchanged since the 2016 import, cleared
the destination and inserted every pair through IDictionary again, so
each key was hashed again - a #hash call for a String or any object key
- and Hash#dup and #merge copy twice (initialize_copy, then Duplicate).
Hash stores each entry's hash code. When source and destination use
the same comparer those codes are valid in the destination too, so the
new Hash.TryCopyEntriesFrom copies the live entries in one block
(skipping tombstones) and re-chains the buckets from the stored codes:
no comparer call at all. Different comparers, or a hash copied onto
itself, take the old path. A copy into a hash that is being iterated
raises what the first insert would have.
10,000 entries, 20 copies, best of 3 (Release, net8.0; CRuby 3.3):
Hash#dup, Integer keys 18.1 -> 3.9 ms (1.2)
Hash#dup, String keys 28.9 -> 2.1 ms (0.9)
Hash#merge({}) 15.2 -> 1.9 ms (0.8)
{}.replace(h) 26.8 -> 2.3 ms (1.1)
Hash[h] 24.2 -> 2.2 ms (1.0)
Small hashes and Hash#== are unchanged.
Checked against CRuby (inspect format normalized): nil keys,
tombstones after deletes, independent mutation of the copy,
compare_by_identity in both directions of #replace, default values and
procs, subclasses, frozen String keys staying the same objects, replace
during iteration, onto itself and frozen, rehash after a key mutated.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KPwZf5xqgL3eZiUCWPfG2b
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
IDictionaryOps.ReplaceData(Src/Libraries/Extensions/IDictionaryOps.cs) has been unchanged since the 2016 import. It is whatHash#dup,#merge,#replace,Hash[hash]andinitialize_copycopy with. It cleared the destination and inserted every pair again throughIDictionary, so every key was hashed again. For a String or any other object key that is a#hashcall.#dupand#mergeeven copy twice: once ininitialize_copy, then again inDuplicate.Change
Hashstores each entry's hash code. When source and destination use the same comparer, those codes are valid in the destination too.The new
Hash.TryCopyEntriesFrominSrc/Ruby/Builtins/Hash.Storage.cscopies the live entries in one block, skipping tombstones. It then rebuilds the buckets from the stored codes, with no comparer call at all.ReplaceDatauses it when both sides are aHash.The old path still handles these cases:
#replacebetween hashes that differ incompare_by_identity.Hash.A copy into a hash that is being iterated raises the same error the first insert would have.
Numbers
10,000 entries, 20 copies, best of 3, each case in its own process. Release, net8.0; CRuby 3.3 for comparison.
Hash#dup, Integer keysHash#dup, String keysHash#merge({}){}.replace(h)Hash[h]Small hashes and
Hash#==are unchanged.Testing (Linux, net8.0, Release)
Differential check against CRuby (26 cases;
inspectformat normalized, since local CRuby is 3.3): identical output. It covers:compare_by_identityin both directions of#replace;#replaceduring iteration, onto itself and on a frozen hash;#rehashafter a key was mutated.Util/run-tests.sh: PASSED, 0 failure lines.ruby/spec, each suite compared with the build without this change:
core/{hash,env,struct}language,core/{kernel,enumerable,array}core/{marshal,objectspace,set},library/openstructThe failures and errors are the same without the change.
Generated by Claude Code
Summary by CodeRabbit