Skip to content

Commit

Permalink
Replace fixed maps with hashbrown::HashTable
Browse files Browse the repository at this point in the history
We still preserve the fixed-size nature, albeit at (potentially) greater memory cost,
since hashbrown will resize up to 2x the actual max entry count due to
having tombstones in the table. After that final growth we will incur
somewhat expensive table-wide rehashing periodically, but this is
sufficiently fast to be acceptable in practice.
  • Loading branch information
Mark-Simulacrum committed Feb 17, 2025
1 parent 00e3371 commit 8b5a90a
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 313 deletions.
1 change: 1 addition & 0 deletions dc/s2n-quic-dc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ s2n-codec = { version = "=0.53.0", path = "../../common/s2n-codec", default-feat
s2n-quic-core = { version = "=0.53.0", path = "../../quic/s2n-quic-core", default-features = false }
s2n-quic-platform = { version = "=0.53.0", path = "../../quic/s2n-quic-platform" }
slotmap = "1"
hashbrown = "0.15"
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["sync"] }
tracing = "0.1"
Expand Down
12 changes: 9 additions & 3 deletions dc/s2n-quic-dc/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ pub mod testing;
#[repr(C)]
pub struct Id([u8; 16]);

impl std::hash::Hash for Id {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
impl Id {
pub(crate) fn to_hash(self) -> u64 {
// The ID has very high quality entropy already, so write just one half of it to keep hash
// costs as low as possible. For the main use of the Hash impl in the fixed-size ID map
// this translates to just directly using these bytes for the indexing.
state.write_u64(u64::from_ne_bytes(self.0[..8].try_into().unwrap()));
u64::from_ne_bytes(self.0[..8].try_into().unwrap())
}
}

impl std::hash::Hash for Id {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u64(self.to_hash());
}
}

Expand Down
228 changes: 0 additions & 228 deletions dc/s2n-quic-dc/src/fixed_map.rs

This file was deleted.

1 change: 0 additions & 1 deletion dc/s2n-quic-dc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod credentials;
pub mod crypto;
pub mod datagram;
pub mod event;
mod fixed_map;
pub mod msg;
pub mod packet;
pub mod path;
Expand Down
4 changes: 2 additions & 2 deletions dc/s2n-quic-dc/src/path/secret/map/cleaner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Cleaner {
// For non-retired entries, if it's time for them to handshake again, request a
// handshake to happen. This handshake will currently happen on the next request for this
// particular peer.
state.ids.retain(|_, entry| {
state.ids.retain(|entry| {
id_entries_initial += 1;

let retained = if let Some(retired_at) = entry.retired_at() {
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Cleaner {
// Drop IP entries if we no longer have the path secret ID entry.
// FIXME: Don't require a loop to do this. This is likely somewhat slow since it takes a
// write lock + read lock essentially per-entry, but should be near-constant-time.
state.peers.retain(|_, entry| {
state.peers.retain(|entry| {
address_entries_initial += 1;

let retained = state.ids.contains_key(entry.id());
Expand Down
Loading

0 comments on commit 8b5a90a

Please sign in to comment.