Skip to content

Commit

Permalink
add utility for computing 64-bit hash (#1058)
Browse files Browse the repository at this point in the history
Currently based on XXH64. Allows for data to be incrementally
fed to the hash accumulator to make it more convenient to use
and avoid allocations creating the structure needed to pass
into the hash. Though in some cases the incremental processing
is slower than just passing in something like a precomputed
string.
  • Loading branch information
brharrington committed May 11, 2023
1 parent e796bbc commit 1d750ea
Show file tree
Hide file tree
Showing 10 changed files with 884 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ subprojects {
toolVersion = '9.3'
ignoreFailures = false
configFile = rootProject.file('codequality/checkstyle.xml')
configDirectory = rootProject.file("codequality")
sourceSets = [sourceSets.main]
}

Expand Down
9 changes: 9 additions & 0 deletions codequality/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
<suppress checks="IllegalImport" files="UnsafeUtils.java"/>
</suppressions>
3 changes: 3 additions & 0 deletions codequality/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,7 @@
<module name="SuppressWarningsHolder"/>
</module>
<module name="SuppressWarningsFilter"/>
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>
</module>
</module>
6 changes: 6 additions & 0 deletions codequality/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<Bug pattern="EQ_COMPARETO_USE_OBJECT_EQUALS"/>
</And>
</Match>
<Match>
<And>
<Class name="com.netflix.spectator.impl.UnsafeUtils"/>
<Bug pattern="REC_CATCH_EXCEPTION"/>
</And>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
Expand Down
1 change: 1 addition & 0 deletions dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ com.typesafe:config = 1.4.2
io.dropwizard.metrics5:metrics-core = 5.0.0-rc15 # 5.0.0 is a bad version do not use
io.micrometer:micrometer-core = 1.10.6
javax.inject:javax.inject = 1
net.openhft:zero-allocation-hashing = 0.16
nl.jqno.equalsverifier:equalsverifier = 3.14.1
org.apache.logging.log4j:log4j-api = 2.20.0
org.apache.logging.log4j:log4j-core = 2.20.0
Expand Down
2 changes: 2 additions & 0 deletions spectator-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
dependencies {
testImplementation files("$projectDir/src/test/lib/compatibility-0.68.0.jar")
testImplementation "com.google.re2j:re2j"
testImplementation "net.openhft:zero-allocation-hashing"
testImplementation "org.hsqldb:hsqldb"
jmh "com.google.re2j:re2j"
jmh "com.github.ben-manes.caffeine:caffeine"
jmh "net.openhft:zero-allocation-hashing"
}

javadoc {
Expand Down
78 changes: 78 additions & 0 deletions spectator-api/src/jmh/java/com/netflix/spectator/perf/IdHash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2014-2023 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spectator.perf;

import com.netflix.spectator.api.Id;
import com.netflix.spectator.impl.Hash64;
import net.openhft.hashing.LongHashFunction;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

/**
* Benchmark Mode Cnt Score Error Units
* IdHash.hash64 thrpt 5 2379245.474 ± 7379.034 ops/s
* IdHash.hash64_2 thrpt 5 4817014.307 ± 5704.027 ops/s
* IdHash.openhft thrpt 5 4910133.221 ± 100296.076 ops/s
*
* Benchmark Mode Cnt Score Error Units
* IdHash.hash64:·gc.alloc.rate.norm thrpt 5 0.188 ± 0.002 B/op
* IdHash.hash64_2:·gc.alloc.rate.norm thrpt 5 1152.095 ± 0.001 B/op
* IdHash.openhft:·gc.alloc.rate.norm thrpt 5 1128.095 ± 0.002 B/op
*/
@State(Scope.Thread)
public class IdHash {

private final Id id = Id.create("http.req.complete")
.withTag( "nf.app", "test_app")
.withTag("nf.cluster", "test_app-main")
.withTag( "nf.asg", "test_app-main-v042")
.withTag( "nf.stack", "main")
.withTag( "nf.ami", "ami-0987654321")
.withTag( "nf.region", "us-east-1")
.withTag( "nf.zone", "us-east-1e")
.withTag( "nf.node", "i-1234567890")
.withTag( "country", "US")
.withTag( "device", "xbox")
.withTag( "status", "200")
.withTag( "client", "ab");

private final Hash64 h64 = new Hash64();
private final LongHashFunction xx = LongHashFunction.xx();

@Benchmark
public void hash64(Blackhole bh) {
h64.updateString(id.name());
for (int i = 1; i < id.size(); ++i) {
h64.updateChar(':');
h64.updateString(id.getKey(i));
h64.updateChar('=');
h64.updateString(id.getValue(i));
}
bh.consume(h64.computeAndReset());
}

@Benchmark
public void hash64_2(Blackhole bh) {
bh.consume(h64.updateString(id.toString()).computeAndReset());
}

@Benchmark
public void openhft(Blackhole bh) {
bh.consume(xx.hashChars(id.toString()));
}
}
Loading

0 comments on commit 1d750ea

Please sign in to comment.