Skip to content
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

deep hash code for byte[] and other array in tuple #746

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion storm-core/src/clj/backtype/storm/tuple.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,37 @@

(bootstrap)

(def ARRAY-TYPES
#{
(type (to-array []))
(type (byte-array 0))
(type (char-array 0))
(type (short-array 0))
(type (int-array 0))
(type (long-array 0))
(type (float-array 0))
(type (double-array 0))
(type (boolean-array 0))
})

(declare hash-code)

(defn deep-hash-code [alist]
"deep hash code based on array/list content.
it's the same as java.util.Arrays.deepHashCode(),
but without convert a list to array to avoid copy.

use unchecked-* to make sure use int instead of long."
(reduce
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this code much faster by not doing the reduce (which is constantly boxing and unboxing the numbers) and instead iterating through the list. It's doable in Clojure, though I'm ok with writing this code in Java and then calling it from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean ".intValue" and "+" cause constantly boxing and unboxing the numbers? Can we use unchecked-add-int and unchecked-multiply-int to avoid boxing and unboxing?
I tested with unchecked-* and it's 10% faster than ".intValue" and "+" on 100KB and 1000KB byte array.

#(unchecked-add-int (unchecked-multiply-int 31 %1) (hash-code %2))
1 alist))

(defn hash-code [obj]
(let [t (type obj)]
(if (contains? ARRAY-TYPES t)
(deep-hash-code obj)
(.hashCode obj))))

(defn list-hash-code [^List alist]
(.hashCode alist))
(deep-hash-code alist))