pub struct Interner { /* private fields */ }Expand description
An append-only string arena that hands out NameIds.
Strings are concatenated into one String and addressed by (start, len), so N names
cost one amortised allocation rather than N.
§Why not HashMap<Box<str>, NameId>
The obvious implementation allocates a Box<str> for every unique name and stores the
text twice. Loading the 1.6 MB CTIM database interns about nineteen thousand names, so
that is nineteen thousand allocations and a few hundred kilobytes of duplication on a
path whose whole purpose is to be fast.
This is an open-addressing table whose entries are NameIds. A probe compares against
the text already in the arena, so nothing is stored twice and a hash collision resolves
correctly rather than merging two distinct names — which is the failure mode of the
tempting “just key the map on the hash” shortcut.
Implementations§
Source§impl Interner
impl Interner
Sourcepub fn with_capacity(names: usize, bytes: usize) -> Self
pub fn with_capacity(names: usize, bytes: usize) -> Self
Creates an interner with capacity for roughly names entries and bytes of text.
Sourcepub fn intern(&mut self, s: &str) -> NameId
pub fn intern(&mut self, s: &str) -> NameId
Interns s, returning its handle. Repeated calls with equal strings return the
same handle, and nothing is allocated per name.
Sourcepub fn get(&self, s: &str) -> Option<NameId>
pub fn get(&self, s: &str) -> Option<NameId>
Returns the handle for s if it has been interned, without interning it.
Sourcepub fn resolve(&self, id: NameId) -> &str
pub fn resolve(&self, id: NameId) -> &str
Resolves a handle back to its string.
Returns "" for a handle minted by a different interner, which cannot happen for
handles obtained through the public API of a single crate::XtceDb.