2 ms·
Objects in some programming languages (like JavaScript, but not like others such as Haskell) have an identity value. This is an abstract value. It's a theoretic
by chrisseaton 9y ago
Objects in some programming languages (like JavaScript, but not like others such as Haskell) have an identity value. This is an abstract value. It's a theoretical thing. We pretend it's there but it's not like a usual value. It's not something you can actually see or calculate with. You can just compare them.
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.
- gpderetta 9y ago> An identity hash function takes an identity value and produces a simpler integer from it I would expect an identity hash function to be exactly that: i.e. a trivial function that takes a value and returns that value unmodified (i.e. the identity function). It happens to describe well this case because the input to the has function itself is the unique identifier and as it is already uniformly distributed, the has function can just return it unmodified.
- chrisseaton 9y agoIn this context, an 'identity hash function' means that it's a 'hash function for an identity value'. It doesn't mean 'a hash function that is an identity function'. It's confusing!
- dfox 9y agoIn the simple case of using memory location as object identity (ie. in system without moving GC) and typical textbook hashtable implementation simply using the pointer as hash value is highly suboptimal, because heap pointers do not have remotely uniform distribution (low order bits are always zero due to allignment constraints, high order bits tend to be constant and usually also zero...). On the other hand you can use more advanced hashtable design that is explicitly tuned for non-uniform hash value distribution. See CPythons dict for cannonical example of that, although it's motivations are somewhat different (the major reason is that in CPython even real hash values of object's content are intentionally non-uniform to allow for simple way of causing integer-valued floats to have same hash value as ints of same value).
- onli 9y agoThanks for explaining, also to the others. > 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. Neither, I just felt the terminology not fitting, I'd have called it id generation and left out the hash. V8 is generating the hash/id in the hash function, not doing something directly with the input or computing it (as in, calculating) from the input, right? Like if the hash terminology was added because one came from hash tables. I guess what I meant is that using and storing a random number here feels like a trick to fulfill the contract of a hash function without actually implementing one, since those usually look different (but I'm more used to see cryptographic hash functions). My definition of a hash function includes returning a word of fixed length, in practice that is not relevant here because the numerical representation is the same (001 = 01 = 1). But apart from that I can see now that the shown scheme fulfills the contract of a non-cryptographic hash function, the storing of the id and the virtual identity make it somehow fit.
- chrisseaton 9y ago> not doing something directly with the input or computing it (as in, calculating) from the input, right? It is doing something directly with the input and calculating something, if you make the leap to consider that the input object has a hidden abstract value which represents the object's identity.
- onli 9y agoBig leap for me. But thanks, this helped understanding why it is called that way :)