Skip to content

Commit 26d11b9

Browse files
committed
should be better hash for long
1 parent 6c44e53 commit 26d11b9

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

util/src/java/com/github/oeuvres/alix/util/LongIntMap.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,7 @@ public boolean trimToSize()
365365
return true;
366366
}
367367

368-
// ---------------------------------------------------------------------
369-
// Internals
370-
// ---------------------------------------------------------------------
371-
368+
372369
private void allocate(int capacity)
373370
{
374371
keys = new long[capacity];
@@ -448,26 +445,26 @@ private void rehash(int newCapacity)
448445
}
449446

450447
/**
451-
* Hash mixing for long keys.
448+
* Hash mixing for {@code long} keys.
452449
*
453450
* <p>
454-
* Cheap enough for hot paths; avoids the "low bits only" trap when masking.
451+
* This method applies a 64-bit finalizer before folding the result to
452+
* 32 bits. This avoids losing information for packed {@code (int,int)}
453+
* keys before the avalanche step.
455454
* </p>
456455
*
457-
* <p>
458-
* Implementation: a 32-bit finalizer similar to Murmur3 fmix32 applied to
459-
* {@code (int)(key ^ (key >>> 32))}.
460-
* </p>
456+
* @param key the key to mix
457+
* @return a 32-bit hash suitable for masking into a power-of-two table
461458
*/
462-
static int mix32(long key)
459+
static int mix32(final long key)
463460
{
464-
int h = (int) (key ^ (key >>> 32));
465-
h ^= (h >>> 16);
466-
h *= 0x85ebca6b;
467-
h ^= (h >>> 13);
468-
h *= 0xc2b2ae35;
469-
h ^= (h >>> 16);
470-
return h;
461+
long h = key;
462+
h ^= h >>> 30;
463+
h *= 0xbf58476d1ce4e5b9L;
464+
h ^= h >>> 27;
465+
h *= 0x94d049bb133111ebL;
466+
h ^= h >>> 31;
467+
return (int) (h ^ (h >>> 32));
471468
}
472469

473470
/**

0 commit comments

Comments
 (0)