Created
March 21, 2015 06:11
-
-
Save leifwickland/d8e1fdef276bb1a2170f to your computer and use it in GitHub Desktop.
JumpConsistentHash Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def jumpConsistentHash(theKey: Long, bucketCount: Int): Int = { | |
var b = -1L | |
var j = 0L | |
var key = theKey | |
while (j < bucketCount && j >= 0) { | |
b = j | |
key = key * 2862933555777941757L + 1 | |
j = ((b + 1) * ((1L << 31).toDouble / ((key >> 33) + 1).toDouble)).toLong | |
} | |
return b.toInt | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def jumpConsistentHash(key: Long, bucketCount: Int): Int = { | |
@annotation.tailrec | |
def loop(key: Long, j: Long, bucket: Long): Int = { | |
if (j < bucketCount && j >= 0) { | |
val newKey = key * 2862933555777941757L + 1L | |
loop(newKey, | |
((j + 1) * ((1L << 31).toDouble / (((newKey >> 33) + 1).toDouble))).toLong, | |
j) | |
} else { | |
bucket.toInt | |
} | |
} | |
loop(key, 0, -1) | |
} | |
val h = jumpConsistentHash _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On old 1.6 GHz Atom, 20M hashes on cross product of keys (1,1000) and bucket sizes (1, 20000) was 8.9 seconds.