Skip to main content

Interner

Struct Interner 

Source
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

Source

pub fn with_capacity(names: usize, bytes: usize) -> Self

Creates an interner with capacity for roughly names entries and bytes of text.

Source

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.

Source

pub fn get(&self, s: &str) -> Option<NameId>

Returns the handle for s if it has been interned, without interning it.

Source

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.

Source

pub fn len(&self) -> usize

Number of distinct interned strings.

Source

pub fn is_empty(&self) -> bool

Whether nothing has been interned yet.

Source

pub fn bytes(&self) -> usize

Total bytes of unique string data held.

Trait Implementations§

Source§

impl Debug for Interner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Interner

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.