-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from dynatrace-oss/simhash
Simhash
- Loading branch information
Showing
13 changed files
with
400 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/jmh/java/com/dynatrace/hash4j/similarity/SimHash_v1PerformanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2022-2023 Dynatrace LLC | ||
* | ||
* 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.dynatrace.hash4j.similarity; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class SimHash_v1PerformanceTest extends SimilarityHashingPerformanceTest { | ||
|
||
public static class State_64 extends StateBase { | ||
public State_64() { | ||
super(new SimHashPolicy_v1(64, getPseudoRandomGeneratorProvider()).createHasher()); | ||
} | ||
} | ||
|
||
public static class State_256 extends StateBase { | ||
public State_256() { | ||
super(new SimHashPolicy_v1(256, getPseudoRandomGeneratorProvider()).createHasher()); | ||
} | ||
} | ||
|
||
public static class State_1024 extends StateBase { | ||
public State_1024() { | ||
super(new SimHashPolicy_v1(1024, getPseudoRandomGeneratorProvider()).createHasher()); | ||
} | ||
} | ||
|
||
public static class State_4096 extends StateBase { | ||
public State_4096() { | ||
super(new SimHashPolicy_v1(4096, getPseudoRandomGeneratorProvider()).createHasher()); | ||
} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void testSimilarityHashing_64_1(State_64 state, Blackhole blackhole) { | ||
testSimilarityHashing(state, blackhole); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void testSimilarityHashing_256_1(State_256 state, Blackhole blackhole) { | ||
testSimilarityHashing(state, blackhole); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void testSimilarityHashing_1024_1(State_1024 state, Blackhole blackhole) { | ||
testSimilarityHashing(state, blackhole); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void testSimilarityHashing_4096_1(State_4096 state, Blackhole blackhole) { | ||
testSimilarityHashing(state, blackhole); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/com/dynatrace/hash4j/similarity/SimHashPolicy_v1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2022-2023 Dynatrace LLC | ||
* | ||
* 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.dynatrace.hash4j.similarity; | ||
|
||
import static com.dynatrace.hash4j.util.Preconditions.checkArgument; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGenerator; | ||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
import java.util.Arrays; | ||
|
||
final class SimHashPolicy_v1 extends AbstractSimilarityHashPolicy { | ||
|
||
public SimHashPolicy_v1( | ||
int numberOfComponents, PseudoRandomGeneratorProvider pseudoRandomGeneratorProvider) { | ||
super(numberOfComponents, 1, pseudoRandomGeneratorProvider); | ||
} | ||
|
||
@Override | ||
public SimilarityHasher createHasher() { | ||
return new Hasher(); | ||
} | ||
|
||
private class Hasher implements SimilarityHasher { | ||
|
||
private final int[] counts = new int[numberOfComponents]; | ||
|
||
private final PseudoRandomGenerator pseudoRandomGenerator = | ||
pseudoRandomGeneratorProvider.create(); | ||
|
||
public byte[] compute(ElementHashProvider elementHashProvider) { | ||
|
||
requireNonNull(elementHashProvider); | ||
int numberOfElements = elementHashProvider.getNumberOfElements(); | ||
checkArgument(numberOfElements > 0, "Number of elements must be positive!"); | ||
|
||
Arrays.fill(counts, 0); | ||
|
||
int numChunks = numberOfComponents >>> 6; | ||
int numRemaining = numberOfComponents & 0x3F; | ||
|
||
for (int k = 0; k < numberOfElements; ++k) { | ||
|
||
long elementHash = elementHashProvider.getElementHash(k); | ||
pseudoRandomGenerator.reset(elementHash); | ||
|
||
for (int j = 0; j < numChunks; j++) { | ||
long randomValue = pseudoRandomGenerator.nextLong(); | ||
int off = j << 6; | ||
for (int h = 0; h < 64; ++h) { | ||
counts[off + h] += (((int) (randomValue >>> h)) & 1); | ||
} | ||
} | ||
|
||
if (numRemaining > 0) { | ||
long randomValue = pseudoRandomGenerator.nextLong(); | ||
int off = numChunks << 6; | ||
for (int h = 0; h < numRemaining; ++h) { | ||
counts[off + h] += (((int) (randomValue >>> h)) & 1); | ||
} | ||
} | ||
} | ||
|
||
final long limit = numberOfElements >>> 1; | ||
return packedArrayHandler.create( | ||
i -> (counts[i] + (i & (~numberOfElements & 1)) > limit) ? 1L : 0L, numberOfComponents); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/dynatrace/hash4j/similarity/SimHashVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2022-2023 Dynatrace LLC | ||
* | ||
* 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.dynatrace.hash4j.similarity; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
|
||
/** Versions of FastSimHash implementations. */ | ||
public enum SimHashVersion { | ||
/** | ||
* Default version. | ||
* | ||
* <p>Not stable! Use concrete version if compatibility is important, if for example hash | ||
* signatures are persisted. | ||
*/ | ||
DEFAULT { | ||
@Override | ||
SimilarityHashPolicy create(int numberOfComponents) { | ||
return new SimHashPolicy_v1( | ||
numberOfComponents, PseudoRandomGeneratorProvider.splitMix64_V1()); | ||
} | ||
}, | ||
/** Version 1. */ | ||
V1 { | ||
@Override | ||
SimilarityHashPolicy create(int numberOfComponents) { | ||
return new SimHashPolicy_v1( | ||
numberOfComponents, PseudoRandomGeneratorProvider.splitMix64_V1()); | ||
} | ||
}; | ||
|
||
abstract SimilarityHashPolicy create(int numberOfComponents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/com/dynatrace/hash4j/similarity/AbstractSimHashPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2022-2023 Dynatrace LLC | ||
* | ||
* 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.dynatrace.hash4j.similarity; | ||
|
||
abstract class AbstractSimHashPolicyTest extends AbstractSimilarityHasherPolicyTest { | ||
|
||
protected static double calculateComponentCollisionProbability(double cosineSimilarity) { | ||
return Math.min(1., Math.max(0.5, Math.acos(-cosineSimilarity) / Math.PI)); | ||
} | ||
|
||
@Override | ||
protected double calculateExpectedMatchProbability( | ||
long intersectionSize, long difference1Size, long difference2Size) { | ||
|
||
double expectedCosineSimilarity = | ||
intersectionSize | ||
/ Math.sqrt( | ||
(intersectionSize + difference1Size) | ||
* (double) (intersectionSize + difference2Size)); | ||
|
||
return calculateComponentCollisionProbability(expectedCosineSimilarity); | ||
} | ||
|
||
@Override | ||
protected int getMaxSizeForCheckSumTest() { | ||
return 300; | ||
} | ||
} |
Oops, something went wrong.