summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPap Lőrinc <paplorinc@gmail.com>2016-11-22 10:59:30 +0200
committerPap Lőrinc <paplorinc@gmail.com>2016-11-23 23:57:06 +0200
commit7952525e7119282ec8308a0076db54923f95dc21 (patch)
tree9659671ceb251d641184de23beee416b84721200
parent9c5d3f81a667917b6a3aed5098182623c417c137 (diff)
downloadscala-7952525e7119282ec8308a0076db54923f95dc21.tar.gz
scala-7952525e7119282ec8308a0076db54923f95dc21.tar.bz2
scala-7952525e7119282ec8308a0076db54923f95dc21.zip
Changed modulo to bitwise AND in hash calculation
-rw-r--r--src/library/scala/collection/mutable/HashTable.scala69
1 files changed, 17 insertions, 52 deletions
diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala
index a6a6e1e432..9cb40e3f50 100644
--- a/src/library/scala/collection/mutable/HashTable.scala
+++ b/src/library/scala/collection/mutable/HashTable.scala
@@ -411,58 +411,23 @@ private[collection] object HashTable {
protected def elemHashCode(key: KeyType) = key.##
- protected final def improve(hcode: Int, seed: Int) = {
- /* Murmur hash
- * m = 0x5bd1e995
- * r = 24
- * note: h = seed = 0 in mmix
- * mmix(h,k) = k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; */
- // var k = hcode * 0x5bd1e995
- // k ^= k >> 24
- // k *= 0x5bd1e995
- // k
-
- /* Another fast multiplicative hash
- * by Phil Bagwell
- *
- * Comment:
- * Multiplication doesn't affect all the bits in the same way, so we want to
- * multiply twice, "once from each side".
- * It would be ideal to reverse all the bits after the first multiplication,
- * however, this is more costly. We therefore restrict ourselves only to
- * reversing the bytes before final multiplication. This yields a slightly
- * worse entropy in the lower 8 bits, but that can be improved by adding:
- *
- * `i ^= i >> 6`
- *
- * For performance reasons, we avoid this improvement.
- * */
- val i= scala.util.hashing.byteswap32(hcode)
-
- /* Jenkins hash
- * for range 0-10000, output has the msb set to zero */
- // var h = hcode + (hcode << 12)
- // h ^= (h >> 22)
- // h += (h << 4)
- // h ^= (h >> 9)
- // h += (h << 10)
- // h ^= (h >> 2)
- // h += (h << 7)
- // h ^= (h >> 12)
- // h
-
- /* OLD VERSION
- * quick, but bad for sequence 0-10000 - little entropy in higher bits
- * since 2003 */
- // var h: Int = hcode + ~(hcode << 9)
- // h = h ^ (h >>> 14)
- // h = h + (h << 4)
- // h ^ (h >>> 10)
-
- // the rest of the computation is due to SI-5293
- val rotation = seed % 32
- val rotated = (i >>> rotation) | (i << (32 - rotation))
- rotated
+ /**
+ * Defer to a high-quality hash in [[scala.util.hashing]].
+ * The goal is to distribute across bins as well as possible even if a hash code has low entropy at some bits.
+ * <p/>
+ * OLD VERSION - quick, but bad for sequence 0-10000 - little entropy in higher bits - since 2003
+ * {{{
+ * var h: Int = hcode + ~(hcode << 9)
+ * h = h ^ (h >>> 14)
+ * h = h + (h << 4)
+ * h ^ (h >>> 10)
+ * }}}
+ * the rest of the computation is due to SI-5293
+ */
+ protected final def improve(hcode: Int, seed: Int): Int = {
+ val hash = scala.util.hashing.byteswap32(hcode)
+ val shift = seed & ((1 << 5) - 1)
+ (hash >>> shift) | (hash << (32 - shift))
}
}