Skip to content

Commit 4c729fe

Browse files
committed
Changed modulo to bitwise and in hash calculation
1 parent 9c5d3f8 commit 4c729fe

File tree

2 files changed

+5
-53
lines changed

2 files changed

+5
-53
lines changed

src/library/scala/collection/immutable/HashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ sealed class HashMap[A, +B] extends AbstractMap[A, B]
8080

8181
protected def elemHashCode(key: A) = key.##
8282

83-
protected final def improve(hcode: Int) = {
83+
protected final def improve(hcode: Int) = { // TODO unify with HashUtils.improve
8484
var h: Int = hcode + ~(hcode << 9)
8585
h = h ^ (h >>> 14)
8686
h = h + (h << 4)

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

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -411,58 +411,10 @@ 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+
protected final def improve(hcode: Int, seed: Int): Int = {
415+
val i = scala.util.hashing.byteswap32(hcode)
416+
val rotation = seed & ((1 << 5) - 1)
417+
(i >>> rotation) | (i << (32 - rotation))
466418
}
467419
}
468420

0 commit comments

Comments
 (0)