Skip to content

Commit

Permalink
Handle usize overflow on big cache capacity
Browse files Browse the repository at this point in the history
- Not a bug but for clarity, avoid a lossy cast from `u64` to `u32` before
  applying the table mask.
  • Loading branch information
tatsuya6502 committed Aug 29, 2021
1 parent 766f804 commit 950ec1b
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/common/frequency_sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// and an aging process periodically halves the popularity of all elements.
pub(crate) struct FrequencySketch {
sample_size: u32,
table_mask: u32,
table_mask: u64,
table: Box<[u64]>,
size: u32,
}
Expand Down Expand Up @@ -92,7 +92,7 @@ impl FrequencySketch {
maximum.next_power_of_two()
};
let table = vec![0; table_size as usize].into_boxed_slice();
let table_mask = 0.max(table_size - 1);
let table_mask = 0.max(table_size - 1) as u64;
let sample_size = if cap == 0 {
10
} else {
Expand Down Expand Up @@ -170,7 +170,7 @@ impl FrequencySketch {
let i = depth as usize;
let mut hash = hash.wrapping_add(SEED[i]).wrapping_mul(SEED[i]);
hash += hash >> 32;
(hash as u32 & self.table_mask) as usize
(hash & self.table_mask) as usize
}
}

Expand Down

0 comments on commit 950ec1b

Please sign in to comment.