CaffeineMC / hydrogen-fabric

Things of which are too dangerous to put in Lithium.
GNU Lesser General Public License v3.0
511 stars 66 forks source link

Correctly hash int-arrays #2

Closed tr7zw closed 4 years ago

tr7zw commented 4 years ago

Arrays can't be compared using .hashcode()/equals(), so the ObjectOpenHashSet will create an entry for each array. Now using a Map<int, int[]> and the correct Arrays.hashCode(...) to compute the hashcode.

Chocohead commented 4 years ago

Would it not be a clearer way of storing int arrays to use an ObjectOpenCustomHashSet with the int array hashing strategy provided in IntArrays#HASH_STRATEGY?

jellysquid3 commented 4 years ago

This "fixed" implementation is broken in a different way, as it's using an int->obj hash table where the integer key is the hash code of the object. These 32-bit hash codes can have collisions and will cause other issues.

The best solution is to just use a collection type with a custom hash/equals strategy as Chocohead points out above.

tr7zw commented 4 years ago

Oh you're right. At first I was looking into ObjectOpenCustomHashSet, but I didn't know about IntArrays.HASH_STRATEGY. I see you also fixed it in FastImmutableTableCache 👍