Bug 19071 – core.internal.hash should have non-chained toHash overloads

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-09T16:46:27Z
Last change time
2018-08-15T14:13:04Z
Assigned to
No Owner
Creator
Nathan S.

Comments

Comment #0 by n8sh.secondary — 2018-07-09T16:46:27Z
Each hash function in `core.internal.hash` currently has the form: ``` size_t hashOf(T)(auto ref T val, size_t seed = 0); ``` The idea behind the second `seed` argument was to allow "chaining", e.g. `hashOf(z, hashOf(x, hashOf(y)))`. The downside is that for any type for which `hashOf` is not a wrapper around `bytesHash` this performs additional work which can be avoided if chaining is not needed. To fix this instead of a single function with a default argument there should be two overloads: ``` size_t hashOf(T)(auto ref T val, size_t seed); size_t hashOf(T)(auto ref T val); ``` Aside from avoiding unnecessary work for cases like `hashOf(uint(x))`, this would also cause `hashOf(y)` to be the same as `y.toHash`: ``` unittest { static struct Record { size_t id; int[string] properties; size_t toHash() const @nogc nothrow pure @safe { return id; } } Record record = { id: 3 }; assert(record.toHash() == hashOf(record)); // Currently fails! } ```
Comment #1 by github-bugzilla — 2018-08-15T14:13:04Z
Commits pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/2f1275cee4fc865ae32c14be43a3a106c425eb92 Fix Issue 19071 - core.internal.hash should have non-chained toHash overloads https://github.com/dlang/druntime/commit/711ab1c68094458c1ba0a6ea7a214c35157391a2 Merge pull request #2246 from n8sh/core-hash-19071 Fix Issue 19071 - core.internal.hash should have non-chained toHash overloads merged-on-behalf-of: Sebastian Wilzbach <[email protected]>