8000 Changed modulo to bitwise AND in hash calculation · SethTisue/scala@7952525 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7952525

Browse files
committed
Changed modulo to bitwise AND in hash calculation
1 parent 9c5d3f8 commit 7952525

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed

src/library/scala/collection/mutable/HashTable.scala

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -411,58 +411,23 @@ private[collection] object HashTable {
411411

412412
protected def elemHashCode(key: KeyType) = key.##
413413

414-
protected final def improve(hcode: Int, seed: Int) = {
415-
/* Murmur hash
416-
* m = 0x5bd1e995
417-
* r = 24
418-
* note: h = seed = 0 in mmix
419-
* mmix(h,k) = k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; */
420-
// var k = hcode * 0x5bd1e995
421-
// k ^= k >> 24
422-
// k *= 0x5bd1e995
423-
// k
424-
425-
/* Another fast multiplicative hash
426-
* by Phil Bagwell
427-
*
428-
* Comment:
429-
* Multiplication doesn't affect all the bits in the same way, so we want to
430-
* multiply twice, "once from each side".
431-
* It would be ideal to reverse all the bits after the first multiplication,
432-
* however, this is more costly. We therefore restrict ourselves only to
433-
* reversing the bytes before final multiplication. This yields a slightly
434-
* worse entropy in the lower 8 bits, but that can be improved by adding:
435-
*
436-
* `i ^= i >> 6`
437-
*
438-
* For performance reasons, we avoid this improvement.
439-
* */
440-
val i= scala.util.hashing.byteswap32(hcode)
441-
442-
/* Jenkins hash
443-
* for range 0-10000, output has the msb set to zero */
444-
// var h = hcode + (hcode << 12)
445-
// h ^= (h >> 22)
446-
// h += (h << 4)
447-
// h ^= (h >> 9)
448-
// h += (h << 10)
449-
// h ^= (h >> 2)
450-
// h += (h << 7)
451-
// h ^= (h >> 12)
452-
// h
453-
454-
/* OLD VERSION
455-
* quick, but bad for sequence 0-10000 - little entropy in higher bits
456-
* since 2003 */
457-
// var h: Int = hcode + ~(hcode << 9)
458-
// h = h ^ (h >>> 14)
459-
// h = h + (h << 4)
460-
// h ^ (h >>> 10)
461-
462-
// the rest of the computation is due to SI-5293
463-
val rotation = seed % 32
464-
val rotated = (i >>> rotation) | (i << (32 - rotation))
465-
rotated
414+
/**
415+
* Defer to a high-quality hash in [[scala.util.hashing]].
416+
* The goal is to distribute across bins as well as possible even if a hash cod 9864 e has low entropy at some bits.
417+
* <p/>
418+
* OLD VERSION - quick, but bad for sequence 0-10000 - little entropy in higher bits - since 2003
419+
* {{{
420+
* var h: Int = hcode + ~(hcode << 9)
421+
* h = h ^ (h >>> 14)
422+
* h = h + (h << 4)
423+
* h ^ (h >>> 10)
424+
* }}}
425+
* the rest of the computation is due to SI-5293
426+
*/
427+
protected final def improve(hcode: Int, seed: Int): Int = {
428+
val hash = scala.util.hashing.byteswap32(hcode)
429+
val shift = seed & ((1 << 5) - 1)
430+
(hash >>> shift) | (hash << (32 - shift))
466431
}
467432
}
468433

0 commit comments

Comments
 (0)
0