Skip to content

Commit

Permalink
Fix fixed map to allocate full size
Browse files Browse the repository at this point in the history
Previously we always allocated just one slot.
  • Loading branch information
Mark-Simulacrum committed Oct 1, 2024
1 parent 81f1cd8 commit 09436e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dc/s2n-quic-dc/src/fixed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ where
S: BuildHasher,
{
pub fn with_capacity(entries: usize, hasher: S) -> Self {
let slots = std::cmp::max(1, (entries + SLOT_CAPACITY) / SLOT_CAPACITY).next_power_of_two();
let map = Map {
slots: (0..std::cmp::min(1, (entries + SLOT_CAPACITY) / SLOT_CAPACITY))
slots: (0..slots)
.map(|_| Slot::new())
.collect::<Vec<_>>()
.into_boxed_slice(),
Expand Down
9 changes: 9 additions & 0 deletions dc/s2n-quic-dc/src/fixed_map/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ fn slot_clear() {

assert_eq!(slot.len(), 0);
}

#[test]
fn capacity_size() {
let map: Map<u32, ()> = Map::with_capacity(500_000, Default::default());
for idx in 0..500_000 {
map.insert(idx, ());
}
assert!(map.len() >= 400_000, "{}", map.len());
}
4 changes: 3 additions & 1 deletion dc/s2n-quic-dc/src/path/secret/map/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ fn check_invariants_no_overflow() {
fn no_memory_growth() {
let signer = stateless_reset::Signer::new(b"secret");
let map = Map::new(signer);
for idx in 0..500_000_000 {
map.state.cleaner.stop();
for idx in 0..500_000 {
// FIXME: this ends up 2**16 peers in the `peers` map
map.insert(fake_entry(idx as u16));
}
}
Expand Down

0 comments on commit 09436e2

Please sign in to comment.