Bug 21005 – Speed up hashOf for associative arrays

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-07-01T23:41:34Z
Last change time
2021-08-31T10:45:03Z
Assigned to
No Owner
Creator
Nathan S.

Comments

Comment #0 by n8sh.secondary — 2020-07-01T23:41:34Z
Current code: --- size_t h = 0; foreach (key, ref val; aa) { size_t[2] hpair; hpair[0] = key.hashOf(); hpair[1] = val.hashOf(); h += hpair.hashOf(); } --- Proposed code: --- size_t h = 0; foreach (ref key, ref val; aa) h += hashOf(hashOf(val), hashOf(key)); --- On a 32-bit machine the old code is equivalent to: --- size_t h = 0; foreach (key, ref val; aa) { size_t k = hashOf(hashOf(key), 0); k = hashOf(hashOf(val), h); k = fmix32(k ^ (size_t.sizeof * 2)); // fmix32 being the MurmurHash3 finalizer. h += k; } --- On a 64-bit machine the work involved is greater. That level of mixing at each step is excessive. Note: Writing `hashOf(val, hashOf(key))` might seem better than `hashOf(hashOf(key), hashOf(key))` as it possibly avoids redundancy, but that can't be used by the TypeInfo-based hash in rt.aaA._aaGetHash.
Comment #1 by dlang-bot — 2020-07-01T23:44:59Z
@n8sh created dlang/druntime pull request #3147 "Issue 21005 - Speed up hashOf for associative arrays" mentioning this issue: - Issue 21005 - Speed up hashOf for associative arrays https://github.com/dlang/druntime/pull/3147
Comment #2 by dlang-bot — 2021-08-31T10:45:03Z
dlang/druntime pull request #3147 "Speed up hashOf for associative arrays" was merged into master: - d1142b8005d26741b596f3d7d3ed0acdb902a7a1 by Nathan Sashihara: Fix Issue 21005 - Speed up hashOf for associative arrays https://github.com/dlang/druntime/pull/3147