Skip to content

Commit

Permalink
Merge pull request #88 from OpenHFT/x.26
Browse files Browse the repository at this point in the history
X.26
  • Loading branch information
peter-lawrey authored May 28, 2024
2 parents 008585e + 43fcaf2 commit a8df132
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 27 deletions.
1 change: 0 additions & 1 deletion .mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,4 @@ protected PasswordAuthentication getPasswordAuthentication() {
fos.close();
rbc.close();
}

}
41 changes: 37 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<parent>
<groupId>net.openhft</groupId>
<artifactId>java-parent-pom</artifactId>
<version>1.1.36</version>
<version>1.26.0</version>
<relativePath />
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>zero-allocation-hashing</artifactId>
<version>0.17-SNAPSHOT</version>
<version>0.26ea0-SNAPSHOT</version>
<name>Zero-allocation Hashing</name>
<description>Zero-allocation implementations of fast non-cryptographic hash functions
for byte sequences or blocks of memory</description>
Expand Down Expand Up @@ -206,6 +206,7 @@

<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down Expand Up @@ -331,7 +332,7 @@
<profile>
<id>jdk9+-profile</id>
<activation>
<jdk>[9,)</jdk>
<jdk>[9,21)</jdk>
</activation>
<properties>
<maven.compiler.release>${project.target.release}</maven.compiler.release>
Expand All @@ -355,7 +356,39 @@
</plugins>
</build>
</profile>

<profile>
<id>jdk21-profile</id>
<activation>
<jdk>[21,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<failOnWarning>false</failOnWarning>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>test-without-compact-string</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>@{argLine} -XX:-CompactStrings ${jvm.requiredArgs}</argLine>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>sonar</id>
<build>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/openhft/hashing/CityAndFarmHash_1_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ static LongHashFunction asLongHashFunctionWithTwoSeeds(long seed0, long seed1) {
return new AsLongHashFunctionSeeded(seed0, seed1);
}


// FarmHash

private static <T> long naHashLen33To64(Access<T> access, T in, long off, long len) {
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/net/openhft/hashing/Maths.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import org.jetbrains.annotations.NotNull;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;

class Maths {
@NotNull
private static final Maths INSTANCE;

static {
boolean hasMultiplyHigh = true;
Maths maths = null;
try {
Math.multiplyHigh(0, 0);
Method multiplyHigh = Math.class.getDeclaredMethod("multiplyHigh", int.class, int.class);
MethodHandle multiplyHighMH = MethodHandles.lookup().unreflect(multiplyHigh);
maths = new MathsJDK9(multiplyHighMH);
} catch (final Throwable ignore) {
hasMultiplyHigh = false;
maths = new Maths();
}
INSTANCE = hasMultiplyHigh ? new MathsJDK9() : new Maths();
INSTANCE = maths;
}

public static long unsignedLongMulXorFold(final long lhs, final long rhs) {
Expand Down Expand Up @@ -62,16 +68,30 @@ long unsignedLongMulHighImp(final long lhs, final long rhs) {
}

class MathsJDK9 extends Maths {
private final MethodHandle multiplyHighMH;

public MathsJDK9(MethodHandle multiplyHighMH) {
this.multiplyHighMH = multiplyHighMH;
}

// Math.multiplyHigh() is intrinsified from JDK 10. But JDK 9 is out of life, we always prefer
// this version to the scalar one.
@Override
long unsignedLongMulXorFoldImp(final long lhs, final long rhs) {
final long upper = Math.multiplyHigh(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
final long upper = invokeExact(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
final long lower = lhs * rhs;
return lower ^ upper;
}
@Override
long unsignedLongMulHighImp(final long lhs, final long rhs) {
return Math.multiplyHigh(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
return invokeExact(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
}

private long invokeExact(long lhs, long rhs) {
try {
return (long) multiplyHighMH.invokeExact(lhs, rhs);
} catch (Throwable e) {
throw new AssertionError(e);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/net/openhft/hashing/MurmurHash_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ private static long finalize(long length, long h1, long h2, @Nullable long[] res
h1 = fmix64(h1);
h2 = fmix64(h2);


if (null != result) {
h1 += h2;
result[0] = h1;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/City64_1_1_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void testCityWithOneSeed() {
test(LongHashFunction.city_1_1(0L, 0L), HASHES_OF_LOOPING_BYTES_WITH_SEEDS_0_0);
}


public void test(LongHashFunction city, long[] hashesOfLoopingBytes) {
byte[] data = new byte[len];
for (int j = 0; j < data.length; j++) {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/FarmHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ public void testNa() {
LongHashFunctionTest.test(f, data, f.hashBytes(data));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ private static void testArrays(LongHashFunction f, byte[] data, long eh, int len
System.arraycopy(shorts, 0, shorts2, 1, shortLen);
assertEquals("short array off len", eh, f.hashShorts(shorts2, 1, shortLen));


char[] chars = new char[shortLen];
bb.asCharBuffer().get(chars);
assertEquals("char array", eh, f.hashChars(chars));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ private static void testArrays(LongTupleHashFunction f, byte[] data, long[] eh,
System.arraycopy(shorts, 0, shorts2, 1, shortLen);
assertArrayEquals("short array off len", eh, f.hashShorts(shorts2, 1, shortLen));


char[] chars = new char[shortLen];
bb.asCharBuffer().get(chars);
assertArrayEquals("char array", eh, f.hashChars(chars));
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/net/openhft/hashing/MetroHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static Collection<Object[]> data() {
@Parameterized.Parameter
public int len;


@Test
public void testMetroWithoutSeeds() {
test(LongHashFunction.metro(), HASHES_OF_LOOPING_BYTES_WITHOUT_SEED);
Expand Down Expand Up @@ -2123,4 +2122,4 @@ public void test(LongHashFunction metro, long[] hashesOfLoopingBytes) {
2937352060494234522L,
-2470412390340197582L
};
}
}
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/MurmurHash3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package net.openhft.hashing;


import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.junit.Test;
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/net/openhft/hashing/OriginalFarmHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ static int testUo(int offset, int len, int expectedIndex) {
return expectedIndex;
}


@Test
public void testUoGo() {
for (Object[] g : GOLDEN_64) {
Expand Down Expand Up @@ -1268,7 +1267,7 @@ public void testUoGo() {
4144237690L, 3350490823L,
4166253320L, 2747410691L,
};

static final long[] UO_EXPECTED = {
3277735313L, 2681724312L,
2598464059L, 797982799L,
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/WyHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void testWyHasWithOneSeed() {
test(LongHashFunction.wy_3(42L), HASHES_OF_LOOPING_BYTES_WITH_SEED_42);
}


public void test(LongHashFunction wyHash, long[] hashesOfLoopingBytes) {
byte[] data = new byte[len];
for (int j = 0; j < data.length; j++) {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/XXH128Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void test(LongTupleHashFunction h, LongHashFunction hl, long[][] hashesOf
LongHashFunctionTest.test(hl, data, hashesOfLoopingBytes[len][0]);
}
}

/**
* Test data is output of the following program with xxh3 implementation
* from https://github.com/Cyan4973/xxHash
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/net/openhft/hashing/XXH3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void testXXH3WithOneSeed() {
test(LongHashFunction.xx3(42L), XXH3Test_HASHES.HASHES_OF_LOOPING_BYTES_WITH_SEED_42);
}


public void test(LongHashFunction h, long[] hashesOfLoopingBytes) {
byte[] data = new byte[len];
for (int j = 0; j < data.length; j++) {
Expand All @@ -61,7 +60,6 @@ public void test(LongHashFunction h, long[] hashesOfLoopingBytes) {
LongHashFunctionTest.test(h, data, hashesOfLoopingBytes[len]);
}
}

/**
* Test data is output of the following program with xxh3 implementation
* from https://github.com/Cyan4973/xxHash
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/openhft/hashing/XxHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void testCityWithOneSeed() {
test(LongHashFunction.xx(42L), HASHES_OF_LOOPING_BYTES_WITH_SEED_42);
}


public void test(LongHashFunction city, long[] hashesOfLoopingBytes) {
byte[] data = new byte[len];
for (int j = 0; j < data.length; j++) {
Expand Down

0 comments on commit a8df132

Please sign in to comment.