Skip to content

Commit

Permalink
perf: Simplify hashing for BloomFilter
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Jan 4, 2025
1 parent d3ed021 commit 7ae94e0
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions css-inline/src/html/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl BloomFilter {

#[inline]
fn insert_hash(&mut self, hash: u64) {
self.0 |= 1 << (hash1(hash) % 64);
self.0 |= 1 << (hash2(hash) % 64);
self.0 |= (1u64 << (hash & 63)) | (1u64 << ((hash >> KEY_SIZE) & 63));
}

#[inline]
fn might_contain_hash(self, hash: u64) -> bool {
self.0 & (1 << (hash1(hash) % 64)) != 0 && self.0 & (1 << (hash2(hash) % 64)) != 0
let bits = (1u64 << (hash & 63)) | (1u64 << ((hash >> KEY_SIZE) & 63));
(self.0 & bits) == bits
}

/// Check whether this element MAY have the given class.
Expand All @@ -45,17 +45,6 @@ impl BloomFilter {
}

const KEY_SIZE: usize = 32;
const KEY_MASK: u64 = u32::MAX as u64;

#[inline]
fn hash1(hash: u64) -> u64 {
hash & KEY_MASK
}

#[inline]
fn hash2(hash: u64) -> u64 {
(hash >> KEY_SIZE) & KEY_MASK
}

#[derive(Debug)]
pub(crate) enum Cache {
Expand Down

0 comments on commit 7ae94e0

Please sign in to comment.