Skip to content

Commit 8d349df

Browse files
committed
Moved XxHash64 library to enable shared use across projects
1 parent ead6a54 commit 8d349df

File tree

12 files changed

+71
-22
lines changed

12 files changed

+71
-22
lines changed

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ include ":grpc-inprocess"
9393
include ":grpc-util"
9494
include ":grpc-opentelemetry"
9595
include ":grpc-context-override-opentelemetry"
96+
include ":grpc-third-party:zero-allocation-hashing"
9697

9798
project(':grpc-api').projectDir = "$rootDir/api" as File
9899
project(':grpc-core').projectDir = "$rootDir/core" as File
@@ -130,6 +131,7 @@ project(':grpc-inprocess').projectDir = "$rootDir/inprocess" as File
130131
project(':grpc-util').projectDir = "$rootDir/util" as File
131132
project(':grpc-opentelemetry').projectDir = "$rootDir/opentelemetry" as File
132133
project(':grpc-context-override-opentelemetry').projectDir = "$rootDir/contextstorage" as File
134+
project(':grpc-third-party:zero-allocation-hashing').projectDir = "$rootDir/third-party/zero-allocation-hashing" as File
133135

134136
if (settings.hasProperty('skipCodegen') && skipCodegen.toBoolean()) {
135137
println '*** Skipping the build of codegen and compilation of proto files because skipCodegen=true'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test")
2+
3+
java_library(
4+
name = "zero-allocation-hashing",
5+
srcs = [
6+
"src/main/java/io/grpc/tp/zah/XxHash64.java",
7+
],
8+
deps = [
9+
"@maven//:com_google_guava_guava",
10+
],
11+
visibility = [
12+
"//xds:__pkg__",
13+
"//util:__pkg__",
14+
],
15+
)
16+
17+
java_test(
18+
name = "XxHash64Test",
19+
size = "small",
20+
srcs = ["src/test/java/io/grpc/tp/zah/XxHash64Test.java"],
21+
deps = [
22+
":zero-allocation-hashing",
23+
"@maven//:com_google_guava_guava",
24+
"@maven//:junit_junit",
25+
],
26+
)
File renamed without changes.
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
id "java-library"
3+
}
4+
5+
description = 'gRPC: Zero Allocation Hashing'
6+
7+
dependencies {
8+
implementation libraries.guava
9+
10+
testImplementation libraries.junit
11+
}
12+
13+
tasks.named("jar").configure {
14+
manifest {
15+
attributes('Automatic-Module-Name': 'io.grpc.tp.zah')
16+
}
17+
}
18+
19+
tasks.named("checkstyleMain").configure {
20+
enabled = false
21+
}
22+
23+
tasks.named("checkstyleTest").configure {
24+
enabled = false
25+
}

xds/third_party/zero-allocation-hashing/main/java/io/grpc/xds/XxHash64.java renamed to third-party/zero-allocation-hashing/src/main/java/io/grpc/tp/zah/XxHash64.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Modified by the gRPC Authors
1919
*/
2020

21-
package io.grpc.xds;
21+
package io.grpc.tp.zah;
2222

2323
import static com.google.common.base.Preconditions.checkArgument;
2424
import static com.google.common.base.Preconditions.checkNotNull;
@@ -33,8 +33,8 @@
3333
* <a href="https://github.com/OpenHFT/Zero-Allocation-Hashing/blob/master/src/main/java/net/openhft/hashing/XxHash.java">
3434
* OpenHFT/Zero-Allocation-Hashing</a>.
3535
*/
36-
final class XxHash64 {
37-
static final XxHash64 INSTANCE = new XxHash64(0);
36+
final public class XxHash64 {
37+
static public final XxHash64 INSTANCE = new XxHash64(0);
3838

3939
// Primes if treated as unsigned
4040
private static final long P1 = -7046029288634856825L;
@@ -47,12 +47,12 @@ final class XxHash64 {
4747
private final long seed;
4848
private final long voidHash;
4949

50-
XxHash64(long seed) {
50+
public XxHash64(long seed) {
5151
this.seed = seed;
5252
this.voidHash = finalize(seed + P5);
5353
}
5454

55-
long hashLong(long input) {
55+
public long hashLong(long input) {
5656
input = byteOrder == ByteOrder.LITTLE_ENDIAN ? input : Long.reverseBytes(input);
5757
long hash = seed + P5 + 8;
5858
input *= P2;
@@ -63,15 +63,15 @@ long hashLong(long input) {
6363
return finalize(hash);
6464
}
6565

66-
long hashInt(int input) {
66+
public long hashInt(int input) {
6767
input = byteOrder == ByteOrder.LITTLE_ENDIAN ? input : Integer.reverseBytes(input);
6868
long hash = seed + P5 + 4;
6969
hash ^= (input & 0xFFFFFFFFL) * P1;
7070
hash = Long.rotateLeft(hash, 23) * P2 + P3;
7171
return finalize(hash);
7272
}
7373

74-
long hashShort(short input) {
74+
public long hashShort(short input) {
7575
input = byteOrder == ByteOrder.LITTLE_ENDIAN ? input : Short.reverseBytes(input);
7676
long hash = seed + P5 + 2;
7777
hash ^= (input & 0xFFL) * P5;
@@ -81,22 +81,22 @@ long hashShort(short input) {
8181
return finalize(hash);
8282
}
8383

84-
long hashChar(char input) {
84+
public long hashChar(char input) {
8585
return hashShort((short) input);
8686
}
8787

88-
long hashByte(byte input) {
88+
public long hashByte(byte input) {
8989
long hash = seed + P5 + 1;
9090
hash ^= (input & 0xFF) * P5;
9191
hash = Long.rotateLeft(hash, 11) * P1;
9292
return finalize(hash);
9393
}
9494

95-
long hashVoid() {
95+
public long hashVoid() {
9696
return voidHash;
9797
}
9898

99-
long hashAsciiString(String input) {
99+
public long hashAsciiString(String input) {
100100
ByteSupplier supplier = new AsciiStringByteSupplier(input);
101101
return hashBytes(supplier);
102102
}
@@ -106,7 +106,7 @@ long hashBytes(byte[] bytes) {
106106
return hashBytes(supplier);
107107
}
108108

109-
long hashBytes(byte[] bytes, int offset, int len) {
109+
public long hashBytes(byte[] bytes, int offset, int len) {
110110
ByteSupplier supplier = new PlainByteSupplier(bytes, offset, len);
111111
return hashBytes(supplier);
112112
}

xds/third_party/zero-allocation-hashing/test/java/io/grpc/xds/XxHash64Test.java renamed to third-party/zero-allocation-hashing/src/test/java/io/grpc/tp/zah/XxHash64Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Modified by the gRPC Authors
1919
*/
2020

21-
package io.grpc.xds;
21+
package io.grpc.tp.zah;
2222

2323
import static org.junit.Assert.assertEquals;
2424

xds/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ java_library(
3131
"//services:metrics",
3232
"//services:metrics_internal",
3333
"//stub",
34+
"//third-party/zero-allocation-hashing",
3435
"//util",
3536
"@com_google_protobuf//:protobuf_java",
3637
"@com_google_protobuf//:protobuf_java_util",
@@ -73,7 +74,6 @@ java_binary(
7374
srcs = glob(
7475
[
7576
"src/main/java/**/*.java",
76-
"third_party/zero-allocation-hashing/main/java/**/*.java",
7777
],
7878
exclude = ["src/main/java/io/grpc/xds/orca/**"],
7979
),

xds/build.gradle

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ description = "gRPC: XDS plugin"
1313

1414
sourceSets {
1515
thirdparty {
16-
java {
17-
srcDir "${projectDir}/third_party/zero-allocation-hashing/main/java"
18-
}
1916
proto {
2017
srcDir 'third_party/cel-spec/src/main/proto'
2118
srcDir 'third_party/envoy/src/main/proto'
@@ -27,11 +24,6 @@ sourceSets {
2724
main {
2825
output.classesDirs.from(sourceSets.thirdparty.output.classesDirs)
2926
}
30-
test {
31-
java {
32-
srcDir "${projectDir}/third_party/zero-allocation-hashing/test/java"
33-
}
34-
}
3527
}
3628

3729
configurations {
@@ -50,6 +42,7 @@ dependencies {
5042
project(':grpc-util'),
5143
project(':grpc-services'),
5244
project(':grpc-auth'),
45+
project(':grpc-third-party:zero-allocation-hashing'),
5346
project(path: ':grpc-alts', configuration: 'shadow'),
5447
libraries.guava,
5548
libraries.gson,

xds/src/main/java/io/grpc/xds/RingHashLoadBalancer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import io.grpc.Metadata;
4040
import io.grpc.Status;
4141
import io.grpc.SynchronizationContext;
42+
import io.grpc.tp.zah.XxHash64;
4243
import io.grpc.util.MultiChildLoadBalancer;
4344
import io.grpc.xds.ThreadSafeRandom.ThreadSafeRandomImpl;
4445
import io.grpc.xds.client.XdsLogger;

0 commit comments

Comments
 (0)