Comments (10)
also,
ghci> hash [1,-1,1]
-4935772016787126823
ghci> hash [1,1,-1]
-4935772016787126823
from hashable.
Interesting. I guess it's not a problem with vanilla FNV1 as it hashes byte at the time, so -1
something different.
That's unfortunate. I would like to mix-in Int
s efficiently.
I wonder whether there are hash function with machine integers (Word64) as native input type (instead of single bytes).
EDIT: Now as I think of it, given that internal state is 64bit, the 128 bit input will always have collisions. hashable
doesn't claim to be collision resistant, but it's not great that there are such easily colliding inputs (which I can imagine can happen, lists of -1 and 1 are not that far fetched key string).
from hashable.
... hash function with machine integers (Word64) as native input type (instead of single bytes).
a quick search turned up this comment "... will provide poorer mixing of the bits" and gives the example of bit flips - I guess that's similar to flipping signs, observed in the present issue
https://cs.stackexchange.com/questions/60950/hash-multiple-integers-directly-using-fnv-1a
This answers the comment in the source
-- Note: FNV-1 hash takes a byte of data at once, here we take an 'Int',
-- which is 4 or 8 bytes. Whether that's bad or not, I don't know.
because ... it is bad? The code (hashInt
) is trying to reduce the number of operations (significantly) w.r.t. FNV1 without citing any justification. Presumably there is some paper that defines and proves dispersion for FNV1, but I did not find it. If we had it, then we could re-do the computations for other bit widths.
I am experimenting with
-- hashInt s x = (s * 1099511628211) `xor` x
hashByte s x = (s * 1099511628211) `xor` (x .&. 255)
hashInt s x =
let rots x = take 8 $ iterate (flip shiftR 8) x
in foldl' hashByte s $ rots x
and it does look better (w.r.t. collisions. for run-time - did not measure but it should probably be unrolled)
from hashable.
Could you test the current version 0cfa4fd in #270. I added more mixing, but just 4 rounds, not 8.
from hashable.
Will do (later today). I am thinking both:
- Great,
- Wat?
This is a change (different trade-off between no. of collisions and time for computing hash function) that might affect a lot of users of hashable/unordered-containers? So, "4 rounds not 8" should be scrutinized further? (with proofs, or experiments)
On the other hand, perhaps not so much code is hashing numbers and tuples. Else, people would have complained already? But the change also applies to every generic instance of Hashable (does it?)
Perhaps the saving grace is that instances for bytestring (and similar) were using byte-wise computation already, and remain as they are?
from hashable.
But the change also applies to every generic instance of Hashable (does it?)
It does as we hash the constructor number. Good point, that can be changed to hash as previously. (I have to see a Generic instance of type with over 65536 constructors to compile, so hashing three zeros doesn't add much).
That said, the exact values of hash
are not part of the API contract, and we are free to change them. It says it in the docs
Note: the hash is not guaranteed to be stable across library versions, operating systems or architectures. For stable hashing use named hashes: SHA256, CRC32 etc.
I should add that to the package description as well. (EDIT: Done)
from hashable.
I checked your 0cfa4fd . The collisions that I was complaining about, are gone.
NB: and I was thinking: for the SAT/SMT encoding folks (like me), it's a nice challenge to find a small collision (for (Int,Int)
, say) with 2, 4, 8 rounds.
from hashable.
it's a nice challenge to find a small collision (for (Int,Int), say) with 2, 4, 8 rounds.
What you mean by small? I guess you want to ask SMT for a counterexample of "there aren't simple bit flip collisions for hash(x,y)"
from hashable.
To add to the last, don't spend too much time. I'll investigate how much breakage switching hashable
to using siphash would cause, and how much performance changes (where hashing Text
or ByteString
is what really matters, not tuples or lists of Int
s - though they shouldn't be too slow either).
from hashable.
small
e.g., hash(x1,y1)==hash(x2,y2)
with
I did use leancheck for enumeration, but with the improved code now that takes too long (which is a good sign - there are no small collisions)
or something like http://www.isthe.com/chongo/tech/comp/fnv/#zero-hash
what really matters
that, of course, depends on the hashmap key type in the application. For me, right now, it's not (byte)strings but trees. I think I want hash-consing, really.
from hashable.
Related Issues (20)
- rejects ghc-bignum-1.1 from ghc-9.0.2 HOT 2
- Document guarantee that hash (Arg x _) == hash x HOT 2
- Avoidable reboxing in `Hashable ByteString`? HOT 5
- xxHash instead of FNV?! HOT 9
- Add instances for type(s) in Data.Array.Byte (base-4.17)
- Add instances for new types in `filepath`
- Support GHC 9.4 HOT 1
- Allow GHC-9.4? HOT 2
- Unable to compile on Mac M1 architecture HOT 2
- What's the deal with Fixed? HOT 1
- Hash unstable across executions HOT 6
- hashable doesn't compile in ghc 9.6.1-alpha3 HOT 1
- Hashable 1.4.2.0 fails to build on integer-simple GHC HOT 2
- better instance Hashable IntSet? HOT 5
- using Hashed inside a tuple produces collisions HOT 4
- Caching hash value sometimes leads to equal hashes for different inputs HOT 1
- Minor release 1.4.3.0 introduces semantic changes HOT 1
- Use fromThreadId HOT 1
- Allow containers-0.7 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashable.