aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ziamos <iziamos@gmail.com>2024-01-28 16:03:42 +0000
committerGitHub <noreply@github.com>2024-01-28 17:03:42 +0100
commit97334e862187b958425acc495575d481be0becf3 (patch)
tree79b94befb2adaf1227c05b347507ec8b15a5d442
parent8ef22ab1bd055775ee1d1b63295feb35600741ce (diff)
use long for string equals (#613)
use more generic hashcode
-rw-r--r--src/main/java/dev/morling/onebrc/CalculateAverage_iziamos.java19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_iziamos.java b/src/main/java/dev/morling/onebrc/CalculateAverage_iziamos.java
index ad2bf05..faa6054 100644
--- a/src/main/java/dev/morling/onebrc/CalculateAverage_iziamos.java
+++ b/src/main/java/dev/morling/onebrc/CalculateAverage_iziamos.java
@@ -185,7 +185,7 @@ public class CalculateAverage_iziamos {
byte b = UNSAFE.getByte(pointer);
for (; b != ';'; ++strLen, b = UNSAFE.getByte(pointer + strLen)) {
- hash += b << strLen;
+ hash = 31 * hash + b;
}
pointer += strLen + 1;
@@ -351,22 +351,25 @@ public class CalculateAverage_iziamos {
}
}
- private static boolean stringEquals(final long thisNameAddress, final int thisStringLength, final long otherNameAddress, final long otherNameLength) {
+ private static boolean stringEquals(final long thisNameAddress,
+ final int thisStringLength,
+ final long otherNameAddress,
+ final long otherNameLength) {
if (thisStringLength != otherNameLength) {
return false;
}
int i = 0;
- for (; i < thisStringLength - 3; i += 4) {
- if (UNSAFE.getInt(thisNameAddress + i) != UNSAFE.getInt(otherNameAddress + i)) {
+ for (; i < thisStringLength - 7; i += 8) {
+ if (UNSAFE.getLong(thisNameAddress + i) != UNSAFE.getLong(otherNameAddress + i)) {
return false;
}
}
- final int remainingToCheck = thisStringLength - i;
- final int finalBytesMask = ((1 << remainingToCheck * 8)) - 1;
- final int thisLastWord = UNSAFE.getInt(thisNameAddress + i);
- final int otherLastWord = UNSAFE.getInt(otherNameAddress + i);
+ final long remainingToCheck = thisStringLength - i;
+ final long finalBytesMask = ((1L << remainingToCheck * 8)) - 1;
+ final long thisLastWord = UNSAFE.getLong(thisNameAddress + i);
+ final long otherLastWord = UNSAFE.getLong(otherNameAddress + i);
return 0 == ((thisLastWord ^ otherLastWord) & finalBytesMask);
}