Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize XxHash64.apply method #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 37 additions & 45 deletions webbeans-impl/src/main/java/org/apache/webbeans/hash/XxHash64.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,10 @@ public static long apply(final ByteBuffer input)

do
{
v1 += input.getLong(off) * PRIME64_2;
v1 = Long.rotateLeft(v1, 31);
v1 *= PRIME64_1;

v2 += input.getLong(off + 8) * PRIME64_2;
v2 = Long.rotateLeft(v2, 31);
v2 *= PRIME64_1;

v3 += input.getLong(off + 16) * PRIME64_2;
v3 = Long.rotateLeft(v3, 31);
v3 *= PRIME64_1;

v4 += input.getLong(off + 24) * PRIME64_2;
v4 = Long.rotateLeft(v4, 31);
v4 *= PRIME64_1;
v1 = processChunk(input, off, v1);
v2 = processChunk(input, off + 8, v2);
v3 = processChunk(input, off + 16, v3);
v4 = processChunk(input, off + 24, v4);

off += 32;
remaining -= 32;
Expand All @@ -100,29 +89,10 @@ public static long apply(final ByteBuffer input)
+ Long.rotateLeft(v3, 12)
+ Long.rotateLeft(v4, 18);

v1 *= PRIME64_2;
v1 = Long.rotateLeft(v1, 31);
v1 *= PRIME64_1;
hash ^= v1;
hash = hash * PRIME64_1 + PRIME64_4;

v2 *= PRIME64_2;
v2 = Long.rotateLeft(v2, 31);
v2 *= PRIME64_1;
hash ^= v2;
hash = hash * PRIME64_1 + PRIME64_4;

v3 *= PRIME64_2;
v3 = Long.rotateLeft(v3, 31);
v3 *= PRIME64_1;
hash ^= v3;
hash = hash * PRIME64_1 + PRIME64_4;

v4 *= PRIME64_2;
v4 = Long.rotateLeft(v4, 31);
v4 *= PRIME64_1;
hash ^= v4;
hash = hash * PRIME64_1 + PRIME64_4;
hash = finalizeHash(hash, v1);
hash = finalizeHash(hash, v2);
hash = finalizeHash(hash, v3);
hash = finalizeHash(hash, v4);
}
else
{
Expand All @@ -133,12 +103,7 @@ public static long apply(final ByteBuffer input)

while (remaining >= 8)
{
long k1 = input.getLong(off);
k1 *= PRIME64_2;
k1 = Long.rotateLeft(k1, 31);
k1 *= PRIME64_1;
hash ^= k1;
hash = Long.rotateLeft(hash, 27) * PRIME64_1 + PRIME64_4;
hash = processRemaining(input, off, hash, 8);
off += 8;
remaining -= 8;
}
Expand All @@ -162,6 +127,33 @@ public static long apply(final ByteBuffer input)
return finalize(hash);
}

private static long processChunk(ByteBuffer input, int offset, long value)
{
value += input.getLong(offset) * PRIME64_2;
value = Long.rotateLeft(value, 31);
value *= PRIME64_1;
return value;
}

private static long finalizeHash(long hash, long value)
{
value *= PRIME64_2;
value = Long.rotateLeft(value, 31);
value *= PRIME64_1;
hash ^= value;
return hash * PRIME64_1 + PRIME64_4;
}

private static long processRemaining(ByteBuffer input, int offset, long hash, int length)
{
long k1 = input.getLong(offset);
k1 *= PRIME64_2;
k1 = Long.rotateLeft(k1, 31);
k1 *= PRIME64_1;
hash ^= k1;
return Long.rotateLeft(hash, 27) * PRIME64_1 + PRIME64_4;
}

private static long finalize(long hash)
{
hash ^= hash >>> 33;
Expand All @@ -171,4 +163,4 @@ private static long finalize(long hash)
hash ^= hash >>> 32;
return hash;
}
}
}