Skip to content

Commit c747024

Browse files
authored
Merge pull request #200 from dynatrace-oss/errorprone
introduced errorprone, fixed hashTestObject4 jmh benchmark
2 parents c29f726 + aa74d70 commit c747024

40 files changed

+626
-612
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ However, this algorithm can be useful for file indexes, for example, to find ide
184184
```java
185185
// create some file in the given path
186186
File file = path.resolve("test.txt").toFile();
187-
try (FileWriter fileWriter = new FileWriter(file)) {
187+
try (FileWriter fileWriter = new FileWriter(file, StandardCharsets.UTF_8)) {
188188
fileWriter.write("this is the file content");
189189
}
190190

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ plugins {
2020
id 'signing'
2121
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
2222
id "com.palantir.revapi" version "1.7.0"
23+
id "net.ltgt.errorprone" version "3.1.0"
2324
}
2425

2526
static def readJavaLicense(licenseName) {
@@ -185,6 +186,7 @@ dependencies {
185186
testImplementation group: 'net.openhft', name: 'zero-allocation-hashing', version: '0.16'
186187
testImplementation group: 'com.appmattus.crypto', name: 'cryptohash', version: '0.10.1'
187188
testImplementation group: 'org.greenrobot', name: 'essentials', version: '3.1.0'
189+
errorprone("com.google.errorprone:error_prone_core:2.24.0")
188190
}
189191

190192
jacocoTestReport {
@@ -383,4 +385,10 @@ nexusPublishing {
383385

384386
if (file("extra-configuration.gradle").exists()) {
385387
apply from: 'extra-configuration.gradle'
386-
}
388+
}
389+
390+
tasks.withType(JavaCompile).configureEach {
391+
options.compilerArgs << "-Werror"
392+
options.errorprone.disableWarningsInGeneratedCode = false
393+
// options.errorprone.enabled = false
394+
}

src/jmh/java/com/dynatrace/hash4j/distinctcount/HyperLogLogPerformanceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void distinctCountAddWithMartingaleEstimator(AddState addState, Blackhole
7171
blackhole.consume(martingaleEstimator.getDistinctCountEstimate());
7272
}
7373

74+
@SuppressWarnings("ImmutableEnumChecker")
7475
public enum Estimator {
7576
MAXIMUM_LIKELIHOOD_ESTIMATOR(HyperLogLog.MAXIMUM_LIKELIHOOD_ESTIMATOR),
7677
CORRECTED_RAW_ESTIMATOR(HyperLogLog.CORRECTED_RAW_ESTIMATOR);

src/jmh/java/com/dynatrace/hash4j/distinctcount/UltraLogLogPerformanceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void distinctCountAddWithMartingaleEstimator(AddState addState, Blackhole
7171
blackhole.consume(martingaleEstimator.getDistinctCountEstimate());
7272
}
7373

74+
@SuppressWarnings("ImmutableEnumChecker")
7475
public enum Estimator {
7576
MAXIMUM_LIKELIHOOD_ESTIMATOR(UltraLogLog.MAXIMUM_LIKELIHOOD_ESTIMATOR),
7677
OPTIMAL_FGRA_ESTIMATOR(UltraLogLog.OPTIMAL_FGRA_ESTIMATOR);

src/jmh/java/com/dynatrace/hash4j/hashing/AbstractPerformanceTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Dynatrace LLC
2+
* Copyright 2022-2023 Dynatrace LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -281,8 +281,7 @@ private static String createRandomString(int minLen, int maxLen, SplittableRando
281281
int len = random.nextInt(minLen, maxLen + 1);
282282
StringBuilder sb = new StringBuilder();
283283
for (int i = 0; i < len; ++i) {
284-
char c = 'a';
285-
c += random.nextInt(0, 26);
284+
char c = (char) ('a' + random.nextInt(0, 26));
286285
sb.append(c);
287286
}
288287
return sb.toString();
@@ -292,9 +291,8 @@ private static String createRandomGreekString(int minLen, int maxLen, Splittable
292291
int len = random.nextInt(minLen, maxLen + 1);
293292
StringBuilder sb = new StringBuilder();
294293
for (int i = 0; i < len; ++i) {
295-
char c = 0x03B1;
296-
c += random.nextInt(0, 24);
297-
if (c >= 0x03C2) c += 1;
294+
char c = (char) (0x03B1 + random.nextInt(0, 24));
295+
if (c >= 0x03C2) c = (char) (c + 1);
298296
sb.append(c);
299297
}
300298
return sb.toString();
@@ -311,8 +309,6 @@ private static TestObject[] createTestObjects(
311309
}
312310

313311
static {
314-
final SplittableRandom random = new SplittableRandom(0);
315-
316312
BYTE_ARRAYS_1 = createRandomByteArrays(NUM_OBJECTS, 1, 1, 0x035348bcb49493a4L);
317313
BYTE_ARRAYS_4 = createRandomByteArrays(NUM_OBJECTS, 1, 4, 0xcc6444ca02edfbd0L);
318314
BYTE_ARRAYS_16 = createRandomByteArrays(NUM_OBJECTS, 1, 16, 0x187c616cabc3e0a7L);
@@ -346,7 +342,7 @@ private static TestObject[] createTestObjects(
346342
TEST_OBJECTS1 = createTestObjects(NUM_OBJECTS, TestObject1::new, 0x37569b3107539e19L);
347343
TEST_OBJECTS2 = createTestObjects(NUM_OBJECTS, TestObject2::new, 0x892da841ae127839L);
348344
TEST_OBJECTS3 = createTestObjects(NUM_OBJECTS, TestObject3::new, 0xb443e2873a03f397L);
349-
TEST_OBJECTS4 = createTestObjects(NUM_OBJECTS, TestObject3::new, 0x49952ea071f1cc0aL);
345+
TEST_OBJECTS4 = createTestObjects(NUM_OBJECTS, TestObject4::new, 0x49952ea071f1cc0aL);
350346
}
351347

352348
private void directBytesTest(byte[][] data, Blackhole blackhole) {

src/jmh/java/com/dynatrace/hash4j/hashing/AbstractZeroAllocationHashing64BitPerformanceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Dynatrace LLC
2+
* Copyright 2022-2023 Dynatrace LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ protected long hashCharsIndirect(String s) {
4545

4646
protected abstract LongHashFunction createHashFunction();
4747

48+
@Override
4849
protected long hashObject(TestObject testObject) {
4950
try {
5051
ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();

src/jmh/java/com/dynatrace/hash4j/hashing/UnorderedHashTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Dynatrace LLC
2+
* Copyright 2022-2023 Dynatrace LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.util.List;
2020
import java.util.SplittableRandom;
2121
import java.util.concurrent.ThreadLocalRandom;
22-
import java.util.function.ToLongFunction;
2322
import java.util.stream.Collectors;
2423
import org.openjdk.jmh.annotations.Benchmark;
2524
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -40,10 +39,13 @@ public class UnorderedHashTest {
4039
}
4140

4241
private static final HashFunnel<Long> LONG_FUNNEL = (l, h) -> h.putLong(l);
43-
private static final ToLongFunction<Long> LONG_2_HASH =
44-
x -> Hashing.murmur3_128().hashToLong(x, LONG_FUNNEL);
42+
43+
private static final long long2Hash(long x) {
44+
return Hashing.murmur3_128().hashToLong(x, LONG_FUNNEL);
45+
}
46+
4547
private static final HashFunnel<List<Long>> LIST_LONG_FUNNEL =
46-
(l, h) -> h.putUnorderedIterable(l, LONG_2_HASH);
48+
(l, h) -> h.putUnorderedIterable(l, UnorderedHashTest::long2Hash);
4749

4850
@Benchmark
4951
@BenchmarkMode(Mode.AverageTime)

src/main/java/com/dynatrace/hash4j/distinctcount/HyperLogLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public static int computeToken(long hashValue) {
322322
*/
323323
@Override
324324
public HyperLogLog add(long hashValue, StateChangeObserver stateChangeObserver) {
325-
int idx = (int) (hashValue >>> (-p));
325+
int idx = (int) (hashValue >>> -p);
326326
int newValue = Long.numberOfLeadingZeros(~(~hashValue << p)) + 1;
327327
int oldValue = (int) ARRAY_HANDLER.update(state, idx, newValue, Math::max);
328328
if (stateChangeObserver != null && newValue > oldValue) {
@@ -601,7 +601,7 @@ private static final class MaximumLikelihoodEstimator implements Estimator {
601601
//
602602
// for a numerical evaluation see
603603
// https://www.wolframalpha.com/input?i=sqrt%28ln%282%29%2Fzeta%282%2C2%29%29
604-
private static final double INV_SQRT_FISHER_INFORMATION = 1.0367047097785011;
604+
private static final double INV_SQRT_FISHER_INFORMATION = 1.0367047097785012;
605605
private static final double ML_EQUATION_SOLVER_EPS =
606606
0.001 * INV_SQRT_FISHER_INFORMATION; // 0.1% of theoretical relative error
607607

src/main/java/com/dynatrace/hash4j/distinctcount/UltraLogLog.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ public static int computeToken(long hashValue) {
245245
public UltraLogLog add(long hashValue, StateChangeObserver stateChangeObserver) {
246246
int q = Long.numberOfLeadingZeros(state.length - 1L); // q = 64 - p
247247
int idx = (int) (hashValue >>> q);
248-
int nlz = Long.numberOfLeadingZeros(~(~hashValue << (-q))); // nlz in {0, 1, ..., 64-p}
248+
int nlz = Long.numberOfLeadingZeros(~(~hashValue << -q)); // nlz in {0, 1, ..., 64-p}
249249
byte oldState = state[idx];
250250
long hashPrefix = unpack(oldState);
251-
hashPrefix |= 1L << (nlz + (~q)); // (nlz + (~q)) = (nlz + p - 1) in {p-1, ... 63}
251+
hashPrefix |= 1L << (nlz + ~q); // (nlz + (~q)) = (nlz + p - 1) in {p-1, ... 63}
252252
byte newState = pack(hashPrefix);
253253
state[idx] = newState;
254254
if (stateChangeObserver != null && newState != oldState) {
@@ -323,7 +323,7 @@ static long unpack(byte register) {
323323
// visible for testing
324324
static byte pack(long hashPrefix) {
325325
int nlz = Long.numberOfLeadingZeros(hashPrefix) + 1;
326-
return (byte) (((-nlz) << 2) | ((hashPrefix << nlz) >>> 62));
326+
return (byte) ((-nlz << 2) | ((hashPrefix << nlz) >>> 62));
327327
}
328328

329329
/**
@@ -410,7 +410,7 @@ private static final class MaximumLikelihoodEstimator implements Estimator {
410410
//
411411
// for a numerical evaluation see
412412
// https://www.wolframalpha.com/input?i=3%2F2+*+ln%282%29+*+zeta%283%2C5%2F4%29%2F%28zeta%282%2C5%2F4%29%29%5E2
413-
private static final double ML_BIAS_CORRECTION_CONSTANT = 0.48147376527720066;
413+
private static final double ML_BIAS_CORRECTION_CONSTANT = 0.48147376527720065;
414414

415415
// returns contribution to alpha, scaled by 2^64
416416
private static long contribute(int r, int[] b, int p) {
@@ -772,7 +772,7 @@ static double smallRangeEstimate(long c0, long c4, long c8, long c10, long m) {
772772
long alpha = m + 3 * (c0 + c4 + c8 + c10);
773773
long beta = m - c0 - c4;
774774
long gamma = 4 * c0 + 2 * c4 + 3 * c8 + c10;
775-
double quadRootZ = (sqrt(beta * beta + 4 * alpha * gamma) - beta) / (2 * alpha);
775+
double quadRootZ = (sqrt((double) (beta * beta + 4 * alpha * gamma)) - beta) / (2 * alpha);
776776
double rootZ = quadRootZ * quadRootZ;
777777
return rootZ * rootZ;
778778
}
@@ -781,7 +781,7 @@ static double largeRangeEstimate(long c4w0, long c4w1, long c4w2, long c4w3, lon
781781
long alpha = m + 3 * (c4w0 + c4w1 + c4w2 + c4w3);
782782
long beta = c4w0 + c4w1 + 2 * (c4w2 + c4w3);
783783
long gamma = m + 2 * c4w0 + c4w2 - c4w3;
784-
return sqrt((sqrt(beta * beta + 4 * alpha * gamma) - beta) / (2 * alpha));
784+
return sqrt((sqrt((double) (beta * beta + 4 * alpha * gamma)) - beta) / (2 * alpha));
785785
}
786786

787787
// this is psi as defined in the paper divided by ETA_X

src/main/java/com/dynatrace/hash4j/hashing/AbstractHashStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public HashStream putInts(int[] x, int off, int len) {
159159

160160
@Override
161161
public HashStream putLong(long v) {
162-
putInt((int) (v));
162+
putInt((int) v);
163163
putInt((int) (v >> 32));
164164
return this;
165165
}

src/main/java/com/dynatrace/hash4j/hashing/AbstractKomihash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public HashStream64 putChars(CharSequence s) {
257257
remainingChars -= 32;
258258
off += 32;
259259
} while (remainingChars >= 32);
260-
buffer[0] = (byte) (z);
260+
buffer[0] = (byte) z;
261261
}
262262
}
263263
}

src/main/java/com/dynatrace/hash4j/hashing/AbstractWyhashFinal.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected static long[] makeSecret(long seed) {
222222
ok = true;
223223
seed += 0xa0761d6478bd642fL;
224224
secret[i] =
225-
(c[(int) (Long.remainderUnsigned(wymix(seed, seed ^ 0xe7037ed1a0b428dbL), c.length))]
225+
(c[(int) Long.remainderUnsigned(wymix(seed, seed ^ 0xe7037ed1a0b428dbL), c.length)]
226226
& 0xFFL);
227227
if ((secret[i] & 1) == 0) {
228228
seed += 0x633acdbf4d2dbd49L; // = 7 * 0xa0761d6478bd642fL
@@ -232,7 +232,7 @@ protected static long[] makeSecret(long seed) {
232232
for (int j = 8; j < 64; j += 8) {
233233
seed += 0xa0761d6478bd642fL;
234234
secret[i] |=
235-
(c[(int) (Long.remainderUnsigned(wymix(seed, seed ^ 0xe7037ed1a0b428dbL), c.length))]
235+
(c[(int) Long.remainderUnsigned(wymix(seed, seed ^ 0xe7037ed1a0b428dbL), c.length)]
236236
& 0xFFL)
237237
<< j;
238238
}
@@ -438,7 +438,7 @@ public HashStream64 putChars(CharSequence s) {
438438
setLong(buffer, 32, b4);
439439
setLong(buffer, 40, b5);
440440
}
441-
buffer[0] = (byte) (z);
441+
buffer[0] = (byte) z;
442442
}
443443
}
444444
while (remainingChars >= 4) {

src/main/java/com/dynatrace/hash4j/hashing/FarmHashNa.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ class FarmHashNa extends AbstractHasher64 {
6666
private static final long K1 = 0xb492b66fbe98f273L;
6767
protected static final long K2 = 0x9ae16a3b2f90404fL;
6868
private static final long K_MUL = 0x9ddfea08eb382d69L;
69-
private static final long SEED = 81;
70-
private static final long START_X = SEED * K2;
71-
private static final long START_Y = SEED * K1 + 113;
72-
private static final long START_Z = shiftMix(START_Y * K2 + 113) * K2;
69+
private static final long START_X = 0x1529cba0ca458ffL;
70+
private static final long START_Y = 0x226bb95b4e64b6d4L;
71+
private static final long START_Z = 0x134a747f856d0526L;
7372

7473
private static final FarmHashNa INSTANCE = new FarmHashNa();
7574

@@ -733,6 +732,7 @@ private long naHashLen33To64(int bufferCount) {
733732
rotateRight(e + f, 43) + rotateRight(g, 30) + h, e + rotateRight(f + a, 18) + g, mul);
734733
}
735734

735+
@Override
736736
public long getAsLong() {
737737
return finalizeHash(processRemaining());
738738
}

src/main/java/com/dynatrace/hash4j/hashing/HashStream128.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Dynatrace LLC
2+
* Copyright 2022-2023 Dynatrace LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,5 +171,6 @@ <T> HashStream128 putUnorderedIterable(
171171
*
172172
* @return this
173173
*/
174+
@Override
174175
HashStream128 reset();
175176
}

src/main/java/com/dynatrace/hash4j/hashing/HashStream64.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Dynatrace LLC
2+
* Copyright 2022-2023 Dynatrace LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,5 +171,6 @@ <T> HashStream64 putUnorderedIterable(
171171
*
172172
* @return this
173173
*/
174+
@Override
174175
HashStream64 reset();
175176
}

src/main/java/com/dynatrace/hash4j/hashing/Komihash4_3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public long hashBytesToLong(byte[] input, int off, int len) {
152152
} else if (len > 3) {
153153
long fb = getInt(input, off) & 0xFFFFFFFFL;
154154
long y = getInt(input, off + len - 4);
155-
fb |= (y << 32) >>> (-ml8);
155+
fb |= (y << 32) >>> -ml8;
156156
fb |= 1L << ml8 << (y >>> 63);
157157
r2l ^= fb;
158158
} else if (len > 0) {
@@ -267,7 +267,7 @@ public long hashCharsToLong(CharSequence input) {
267267
} else if (len > 1) {
268268
long fb = getInt(input, off) & 0xFFFFFFFFL;
269269
long y = getInt(input, off + len - 2);
270-
fb |= (y << 32) >>> (-ml8);
270+
fb |= (y << 32) >>> -ml8;
271271
fb |= 1L << ml8 << (y >>> 63);
272272
r2l ^= fb;
273273
} else if (len > 0) {

src/main/java/com/dynatrace/hash4j/hashing/Komihash5_0.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ public long hashBytesToLong(byte[] input, int off, int len) {
146146
if (len > 7) {
147147
r2l ^= getLong(input, off);
148148
long y = getLong(input, off + len - 8);
149-
r2h ^= (1L << ml8) | (y >>> 1 >>> (~ml8));
149+
r2h ^= (1L << ml8) | (y >>> 1 >>> ~ml8);
150150
} else if (len > 3) {
151151
long mh = getInt(input, off + len - 4);
152152
long ml = getInt(input, off) & 0xFFFFFFFFL;
153-
r2l ^= (1L << ml8) | ml | (mh << 32 >>> (-ml8));
153+
r2l ^= (1L << ml8) | ml | (mh << 32 >>> -ml8);
154154
} else if (len > 0) {
155155
long m = (1L << ml8) | (input[off] & 0xFFL);
156156
if (len > 1) m |= (input[off + 1] & 0xFFL) << 8;
@@ -256,11 +256,11 @@ public long hashCharsToLong(CharSequence input) {
256256
if (len > 3) {
257257
r2l ^= getLong(input, off);
258258
long y = getLong(input, off + len - 4);
259-
r2h ^= (1L << ml8) | (y >>> 1 >>> (~ml8));
259+
r2h ^= (1L << ml8) | (y >>> 1 >>> ~ml8);
260260
} else if (len > 1) {
261261
long mh = getInt(input, off + len - 2);
262262
long ml = getInt(input, off) & 0xFFFFFFFFL;
263-
r2l ^= (1L << ml8) | ml | (mh << 32 >>> (-ml8));
263+
r2l ^= (1L << ml8) | ml | (mh << 32 >>> -ml8);
264264
} else if (len > 0) {
265265
long m = (1L << ml8) | input.charAt(off);
266266
r2l ^= m;

0 commit comments

Comments
 (0)