uniqueness of hash if computed on different jvms across different machines.

172 views
Skip to first unread message

Sunil S Nandihalli

unread,
Oct 13, 2014, 2:04:57 AM10/13/14
to clo...@googlegroups.com
Hi,
Is the clojure hash function guaranteed to produce the same hash on different jvms running on different jvms for the same "data-structure" which would satisfy "equality" if checked on a single jvm. The data structure is simply a hash-map.
Thanks,
Sunil.

Andy Fingerhut

unread,
Oct 13, 2014, 10:55:09 AM10/13/14
to clo...@googlegroups.com
For immutable values (including hash-maps containing only immutable values), I believe the answer is yes, if you are using the same version of Clojure on the different jvms (at least, I cannot think of any counterexamples in a few minutes of thinking about it, and I have looked at the hash function implementations in Clojure in some detail before because of [1]).  For mutable values (or hash-maps containing mutable values), there are whatever guarantees Java makes for its .hashCode method.

Most of the hash values computed changed between Clojure 1.5.1 and Clojure 1.6.0 [1] to avoid hash collisions that were common for collections.

There is no guarantee I know of that future versions of Clojure will not change hash functions again.

These facts prompt me to wonder: Why do you want to know if there is such a guarantee?  Saving such hash values on disk, for example, will expose you to possible changes to those values in future versions of Clojure.  Using them to communicate between different JVMs will limit the application to running versions of Clojure on those multiple JVMs with the same hash functions (either the same version of Clojure, or 'hash compatible' versions of Clojure).

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Fikes

unread,
Oct 13, 2014, 11:55:53 AM10/13/14
to clo...@googlegroups.com
In addition to Andy's caveats, remember that hash code equality doesn't imply object equality.

In concrete terms,

   a = b implies h(a) = h(b),

with the useful bit being

  h(a) ≠ h(b) implies a ≠ b.

Alex Miller

unread,
Oct 13, 2014, 4:40:50 PM10/13/14
to clo...@googlegroups.com
I would expect that right now the same version of Clojure would typically give you the same hash for the same data structure across different JVM instances. HOWEVER, I would consider this accidental, not a guarantee. Certainly the hash values may change across Clojure or JDK versions. Additionally the JVM (specifically in some JDK 1.7 versions and settings) and other languages seed hash calculation with an instance-specific value to foil hash-based DDOS attacks. Clojure may employ such a strategy in the future. I would not state any guarantee beyond this:  "In a particular JVM instance, two Clojure data structures that are =, will have the same hasheq value."

If you want a hash computation you can rely on across JVM and Clojure versions and instances, you should compute it with your own code.
Reply all
Reply to author
Forward
0 new messages