-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Hashed type for caching the 'hash' function result. #76
Conversation
See discussion in #22 |
Hm, one benefit of the polymorphic version is that you don't have to write type MyText = Hashed Text
instance IsString MyText where
fromString = hashed . Text.pack and you are good to go. And even if you want a monomorphic type, the polymorphic version instances can still be reused via newtype deriving: newtype MyText = MyText (Hashed Text) deriving (Eq, Ord, Hashable) |
@mvv I'm willing to add (some version of) this if you have a real need where you can show a speed-up. |
I'm going to use this in the "renamer" stage of a compiler I'm writing (The parser represents symbols as |
Hashed a _ `compare` Hashed b _ = a `compare` b | ||
|
||
instance Hashable a => Hashable (Hashed a) where | ||
hashWithSalt s = hashWithSalt s . unhashed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this one be defaultHashWithSalt
to benefit from Hashed
property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should define both hash
and hashWithSalt
. The latter could use defaultHashWithSalt
.
I'm currently using something similar to optimize a syntax tree. It would be nice to have this functionality in
Since the purpose of a hash is to speed up equality tests, I think it should be:
Or more succinctly:
The |
@andrewthad the point is to avoid hashing, but we should definitely use the cached hash to speed up the negative case for equality checking as well. This pull request seems abandoned. Do you want to pick it up? |
Yeah, I'll commandeer this. |
New PR is at #121 |
Supposed to save time spent on calculating hashes in lookups and other operations in
unordered-containers
.