Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Interner::with_capacity for default keys #13092

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions crates/circuit/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ where
/// `Interner::get_default` to reliably work correctly without a hash lookup (though ideally
/// we'd just use specialisation to do that).
pub fn new() -> Self {
let mut set = IndexSet::with_capacity_and_hasher(1, Default::default());
set.insert(Default::default());
Self(set)
Self::with_capacity(1)
}

/// Retrieve the key corresponding to the default store, without any hash or equality lookup.
Expand All @@ -126,11 +124,14 @@ where
}
}

/// Create an interner with enough space to hold `capacity` entries.
///
/// Note that the default item of the interner is always allocated and given a key immediately,
/// which will use one slot of the capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self(IndexSet::with_capacity_and_hasher(
capacity,
::ahash::RandomState::new(),
))
let mut set = IndexSet::with_capacity_and_hasher(capacity, ::ahash::RandomState::new());
set.insert(Default::default());
Self(set)
}
}

Expand Down Expand Up @@ -196,3 +197,21 @@ where
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn default_key_exists() {
let mut interner = Interner::<[u32]>::new();
assert_eq!(interner.get_default(), interner.get_default());
assert_eq!(interner.get(interner.get_default()), &[]);
assert_eq!(interner.insert_owned(Vec::new()), interner.get_default());
assert_eq!(interner.insert(&[]), interner.get_default());

let capacity = Interner::<str>::with_capacity(4);
assert_eq!(capacity.get_default(), capacity.get_default());
assert_eq!(capacity.get(capacity.get_default()), "");
}
}
Loading