You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to add a Hashable typeclass to the prelude and deriving support for it to the compiler. It is intended as an approximation of equality for use in hash-based algorithms and data structures, such as hash maps, not cryptographically secure digests or password hashes.
classEqa<=Hashableawherehash::a->Int
There are two reasons for adding it to the prelude instead of having a standalone library:
Dependencies: To be useful in general data structures, as many datatypes as possible should be hashable. Ideally, whenever a datatype is Eq, it should also have Ord and Hashable instances, if possible. Most packages depend on the prelude already.[citation needed] This should make it easier to convince authors to add Hashable instances, because they do not need to add another dependency, all that is required is one more deriving line below the lines for Eq and Ord.
Performance: Usually, the main goal of hashing is to avoid expensive comparisons. One could write a generic hash function in terms of purescript-generics-rep. This would be fine for many applications, but for hash-based data structures etc., the hash function should really be as fast as possible.
Since this is primarily done for performance reasons I'd like to make sure the newtype doesn't incur any annoying penalties, obviously newtype means the underlying representation is an Int, but if we find ourselves mapping the newtype constructor a lot we'll end up with identity calls that the compiler currently doesn't optimize.
Also, equality comparisons on Int are special-cased by the compiler to emit ===. This is not currently done for (newtype) derived instances for newtypes over Int.
newtypeHash = HashIntderive newtype{- or not -}instanceeqHash :: EqHashapproxEq::Hash->Hash->Boolean
approxEq a b = a == b -- ... return Data_Eq.eq(eqHash)(a)(b);-- can be avoided by being carefulapproxEq'::Hash->Hash->Boolean
approxEq' (Hash a) (Hash b) = a == b -- ... return a === b;
I guess just not providing the Eq instance would force the second version, but would be a bit annoying.
On the other hand, one more dictionary lookup and two applications is probably not the world, and writing performance critical code is annoying anyway.
I think this almost makes sense because there is rarely a reason to do anything with hash values from different source types. On the other hand, I think I'd rather avoid providing an Eq Hash instance, at least at the moment, so it hardly makes a difference.
I don't really know enough to give an opinion on whether the lack of inlining for the instances is a noticeable expense or not though - you're probably best placed to determine that @fehrenbach, since you've done all the work with hashed values in -unordered-collections 😄
While we're here, do we think it would be a good idea to derive Hashable in the compiler like we do for Eq and Ord? That way it's even less likely that people have disagreeing Eq and Hashable instances.
@garyb I'll go for the type-indexed newtype and the usual classes. It's probably not a big performance problem and if it turns out to be, unwrapping is an easy workaround or we could just inline Eq for Hash too.
@kritzcreek Yes, I think so. This is a big part of the motivation for having Hashable in the prelude. The original issue text was meant to say that, but there's only a hint left. I must have accidentally removed it while editing... I already have a branch that derives Hashable, but without the newtype. It will take some work to change it to wrap and unwrap as necessary.
Activity
kritzcreek commentedon Oct 11, 2018
I'd definitely like to have this 👍
fehrenbach commentedon Oct 11, 2018
Great.
I think the only major design decision is whether to
newtype Hash = Hash Intor not.garyb commentedon Oct 11, 2018
I'd like the
newtypepersonally, as the name at least gives a bit of a semantic clue to the value's intention.kritzcreek commentedon Oct 12, 2018
Since this is primarily done for performance reasons I'd like to make sure the newtype doesn't incur any annoying penalties, obviously newtype means the underlying representation is an Int, but if we find ourselves mapping the newtype constructor a lot we'll end up with
identitycalls that the compiler currently doesn't optimize.fehrenbach commentedon Oct 12, 2018
Also, equality comparisons on Int are special-cased by the compiler to emit ===. This is not currently done for (newtype) derived instances for newtypes over Int.
I guess just not providing the Eq instance would force the second version, but would be a bit annoying.
On the other hand, one more dictionary lookup and two applications is probably not the world, and writing performance critical code is annoying anyway.
I don't have a strong opinion either way.
fehrenbach commentedon Oct 20, 2018
We could even index the Hash newtype by the type it's a hash of:
I think this almost makes sense because there is rarely a reason to do anything with hash values from different source types. On the other hand, I think I'd rather avoid providing an
Eq Hashinstance, at least at the moment, so it hardly makes a difference.garyb commentedon Oct 23, 2018
☝️ I like that (type-indexing the
Hash).I don't really know enough to give an opinion on whether the lack of inlining for the instances is a noticeable expense or not though - you're probably best placed to determine that @fehrenbach, since you've done all the work with hashed values in
-unordered-collections😄kritzcreek commentedon Oct 23, 2018
While we're here, do we think it would be a good idea to derive
Hashablein the compiler like we do forEqandOrd? That way it's even less likely that people have disagreeing Eq and Hashable instances.fehrenbach commentedon Oct 23, 2018
@garyb I'll go for the type-indexed newtype and the usual classes. It's probably not a big performance problem and if it turns out to be, unwrapping is an easy workaround or we could just inline Eq for Hash too.
@kritzcreek Yes, I think so. This is a big part of the motivation for having Hashable in the prelude. The original issue text was meant to say that, but there's only a hint left. I must have accidentally removed it while editing... I already have a branch that derives Hashable, but without the newtype. It will take some work to change it to wrap and unwrap as necessary.
fehrenbach commentedon Feb 11, 2019
@hdgarrood points out that adding something like
hashWithSaltto the class might be better (and providinghashas a convenience function).fehrenbach/purescript-unordered-collections#8 (comment)