by esnard · 8y ago · 0 comments
Randomizing the hash function makes it unpredictible to an attacker.
The result is consistent hashing within a given interpreter process, but unpredictability across multiple processes/invocations, which is what's needed to avoid a denial-of-service vector created by predictable hash values for built-in types.
Either way, terminology is weird here, but it seems like in V8’s case the word “hashing” better describes what happens to the hash code when the hashtable algorithm decides what bucket to place the object in (likely a modulus operator or a bit shift), at which point the “hash code” isn’t a hash value at all but just a unique ID that’s a seed value for the real hash function... but again, terminology can be misleading.
When I think of a hash value, I think of something that’s calculated via some properties of an object, not just a stored number.
Edit: https://github.com/multiformats/multihash/issues/13 suggests this is a replacement for using the content of the object, which is not available, to replace the step of hashing something. Is that it? Then it would be a replacement for a replacement of a hash, and not a hash anymore.
I think you're OK with that definition of a hash function, right?
In a mutable object-oriented language like JavaScript, two objects with the same properties are not the same object. Conversely, an object before and after mutating a property is still the same object.
Thus, the "state" that matters for hashing is the object's identity, not its properties. Thus, V8's hash function produces a hash value given the identity of some object. Since there are no useful bits associated with "identity" that V8 can use to produce a hash value, it has to synthesize some at object creation time by calling rand.
(If V8 didn't move objects in memory during GC, then the object's address in memory would be a good candidate for its identity. Indeed, in language implementations where objects don't move, the address is often used as an input to the hash function.)
A hash function takes a value or a compound value and produces a simpler integer from it.
An identity hash function takes an identity value and produces a simpler integer from it.
Based on all these definitions, returning a random integer, as long as you store it and return it again the next time, is an identity hash function.
> you normally hash the id/content of the thing you want to store
That's what we're doing. You've said it yourself. We hash the id (identity) of the thing you want to store. You could call the identity the content of the object as well if you wanted to.
It's not about replacing the actual content on the object, because JavaScript maps are, by design and by definition (23.1.3.6, 7.2.10), explicitly identity maps. They don't want to consider the content of an object, except for this abstract identity content.
I'm not sure if you're arguing I'm mistaken, or the that the whole idea is wrong. But this is what many languages do and it's all very conventional terminology and practice.
For a string key, the hashcode field is probably a cached hash of the value, since that's how an equality comparison works. Most objects are only equal to themselves, are mutable, and get moved in memory, so storing a unique identifier is the only way to know if it's the same object that you saw before.
For a hash code, the identifier doesn't need to be unique since all that matters is that it's consistently placed in the same bucket. All you need is to store a sufficiently long random number.
[1] SameValueZero equality as defined at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equa...
the random number /is/ the id. You can think of it as the unique address of the object in some other memory mapping.
The hash function just happens to be the identity function in this case.