Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize bloomfilter issue 7346 #7494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions guava-tests/test/com/google/common/hash/BloomFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,16 @@ public void testNullPointers() {
/** Tests that we never get an optimal hashes number of zero. */
public void testOptimalHashes() {
for (int n = 1; n < 1000; n++) {
for (int m = 0; m < 1000; m++) {
assertTrue(BloomFilter.optimalNumOfHashFunctions(n, m) > 0);
for (double p = 0.1; p > 1e-10; p/=10) {
assertTrue(BloomFilter.optimalNumOfHashFunctions(p) > 0);
}
}
}

// https://github.com/google/guava/issues/1781
public void testOptimalNumOfHashFunctionsRounding() {
assertEquals(7, BloomFilter.optimalNumOfHashFunctions(319, 3072));
double p = 0.03;
assertEquals(5, BloomFilter.optimalNumOfHashFunctions(p));
}

/** Tests that we always get a non-negative optimal size. */
Expand Down
22 changes: 13 additions & 9 deletions guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ interface Strategy extends java.io.Serializable {
/** The strategy we employ to map an element T to {@code numHashFunctions} bit indexes. */
private final Strategy strategy;

/** Natural logarithm of 2, used to optimize calculations in Bloom filter sizing. */
private static final double LOG_TWO = Math.log(2);

/** Square of the natural logarithm of 2, reused to optimize the bit size calculation. */
private static final double SQUARED_LOG_TWO = LOG_TWO * LOG_TWO;

/** Creates a BloomFilter. */
private BloomFilter(
LockFreeBitArray bits, int numHashFunctions, Funnel<? super T> funnel, Strategy strategy) {
Expand Down Expand Up @@ -435,7 +441,7 @@ public int hashCode() {
* optimalM(1000, 0.0000000000000001) = 76680 which is less than 10kb. Who cares!
*/
long numBits = optimalNumOfBits(expectedInsertions, fpp);
int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);
int numHashFunctions = optimalNumOfHashFunctions(fpp);
try {
return new BloomFilter<>(new LockFreeBitArray(numBits), numHashFunctions, funnel, strategy);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -505,18 +511,16 @@ public int hashCode() {
// 4) For optimal k: m = -nlnp / ((ln2) ^ 2)

/**
* Computes the optimal k (number of hashes per element inserted in Bloom filter), given the
* expected insertions and total number of bits in the Bloom filter.
* Computes the optimal number of hash functions (k) for a given false positive probability (p).
*
* <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula.
*
* @param n expected insertions (must be positive)
* @param m total number of bits in Bloom filter (must be positive)
* @param p desired false positive probability (must be between 0 and 1, exclusive)
*/
@VisibleForTesting
static int optimalNumOfHashFunctions(long n, long m) {
// (m / n) * log(2), but avoid truncation due to division!
return max(1, (int) Math.round((double) m / n * Math.log(2)));
static int optimalNumOfHashFunctions(double p) {
// -log(p) / log(2), ensuring the result is rounded to avoid truncation.
return max(1, (int) Math.round( - Math.log(p) / LOG_TWO));
}

/**
Expand All @@ -534,7 +538,7 @@ static long optimalNumOfBits(long n, double p) {
if (p == 0) {
p = Double.MIN_VALUE;
}
return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2)));
return (long) (-n * Math.log(p) / SQUARED_LOG_TWO);
}

private Object writeReplace() {
Expand Down