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

A fairly easy optimization to BloomFilter::add ... #47

Merged
merged 1 commit into from
Jul 27, 2023
Merged
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
21 changes: 8 additions & 13 deletions src/bloomfilter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@ bool BloomFilter::contains(const std::string& item) {
uint64_t hashes[k_];
hash(item, hashes);
for (size_t i = 0; i < k_; i++) {
uint64_t pos { mpow2_ ? hashes[i] & mask_ : hashes[i] % m_ };
if (!bitset->test(pos)) {
return false;
}
if (!bitset->test(hashes[i])) { return false; }
}
return true;
}

// Add the item; return false if it was already present otherwise true.
bool BloomFilter::add(const std::string& item) {
bool added { false };
uint64_t hashes[k_];
hash(item, hashes);
for (size_t i = 0; i < k_; i++) {
uint64_t pos { mpow2_ ? hashes[i] & mask_ : hashes[i] % m_ };
if (!bitset->test(pos)) {
bitset->set(pos);
added = true;
}
if (!bitset->test(hashes[i])) { goto add_the_item; }
}
return added;
return false;
add_the_item:
for (size_t i = 0; i < k_; i++) { bitset->set(hashes[i]); }
return true;
}

// Return the estimated number of items stored.
Expand Down Expand Up @@ -106,7 +101,7 @@ void BloomFilter::initialize(){
mpow2_ ? "bit mask" : "modulus");
}

// Generate k hash values for the item.
// Generate hash values and map onto k bitarray offsets.
//
// k linear combinations of just 2 independent hashes ("double hashing") has
// the same asymptotic behaviour as k independent hashes.
Expand All @@ -118,7 +113,7 @@ void BloomFilter::hash(const std::string& item, uint64_t* buf) {
auto a { XXH3_64bits_withSeed(cstr, len, seed1_) };
auto b { XXH3_64bits_withSeed(cstr, len, seed2_) };
for (size_t i = 0; i < k_; i++) {
buf[i] = a;
buf[i] = mpow2_ ? a & mask_ : a % m_;
a += b;
b += i;
}
Expand Down