Skip to content

Commit

Permalink
refactor frequency sampling
Browse files Browse the repository at this point in the history
avoid the cost of `entry()` in the HashMap when we know the key exists. The `entry()` API has to handle both cases (exists/doesn't exist) which makes it slightly more expensive than a direct `get_mut()`
  • Loading branch information
jqnatividad committed Dec 7, 2024
1 parent 3f31e51 commit a174f03
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ impl<T: Eq + Hash> Frequencies<T> {
Default::default()
}

/// Add a sample to the frequency table.
/// Fast path for incrementing existing value
/// Returns true if the value existed and was incremented,
/// false if the value wasn't found
#[inline]
pub fn increment(&mut self, v: &T) -> bool {
if let Some(count) = self.data.get_mut(v) {
*count += 1;
true
} else {
false
}
}

/// Optimized add that uses increment as a fast path
#[inline]
pub fn add(&mut self, v: T) {
match self.data.entry(v) {
Entry::Vacant(count) => {
count.insert(1);
}
Entry::Occupied(mut count) => {
*count.get_mut() += 1;
}
if !self.increment(&v) {
self.data.insert(v, 1);
}
}

Expand Down

0 comments on commit a174f03

Please sign in to comment.