Struct ConcurrentInterner

Source
pub struct ConcurrentInterner<RS: Send + Sync + Clone + BuildHasher> { /* private fields */ }
Expand description

A thread-safe string interner.

The APIs are based on the assumption that you will create one ConcurrentInterner, intern some strings to get some IStrs, and then potentially try to obtain the strings back from the same interner. Using the IStr from one interner to obtain the data from another interner may lead to incorrect results or a panic. It will not lead to memory unsafety.

See also: ConcurrentInternerMember and ConcurrentInterner::get_member().

The RS generic parameter should be substituted with a RandomState type. For example, in the context of a compiler, you could use:

In some early testing, FxHash sometimes turns out to be a smidge faster for a workload involving code, but honestly it is hard to tell the difference.

Implementations§

Source§

impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>

Source

pub fn new(arena_count: NonZeroU8, random_state: RS) -> ConcurrentInterner<RS>

Create an interner with one or more arenas.

In most cases, the number of arenas will be equal to the number of threads simultaneously using the ConcurrentInterner. You should perform some benchmarking for the right arena_count value, instead of directly setting it to the number of CPU cores, since the backing DashMap does not scale very well under high contention. (See Performance numbers).

If you’re not sure what random_state to plug in, Default::default() is a reasonable choice.

Source§

impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>

Source

pub fn get_member(&self) -> ConcurrentInternerMember<'_, RS>

Get a member of the interner, which can be used to intern strings.

NOTE: This method acquires a lock, so you don’t want to call it willy-nilly (such as for interning a single string). Instead, call this when starting to do interning work from each thread.

Source

pub fn get_string(&self, istr: IStr) -> String

Read a string from a ConcurrentInterner.

This API is primarily present for convenience and debugging.

If you want to retrieve a large number of strings, you can call ConcurrentInterner::freeze followed by FrozenInterner::get_str to directly copy the data out, without an intermediate heap allocation.

Source§

impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>

Source

pub fn freeze(self) -> FrozenInterner<RS>

Make an interner read-only, allowing for faster string accesses.

Trait Implementations§

Source§

impl<RS: Send + Sync + Clone + BuildHasher> Debug for ConcurrentInterner<RS>

Source§

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

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

impl<RS: Send + Sync + Clone + BuildHasher> From<ConcurrentInterner<RS>> for FrozenInterner<RS>

Auto Trait Implementations§

§

impl<RS> !Freeze for ConcurrentInterner<RS>

§

impl<RS> !RefUnwindSafe for ConcurrentInterner<RS>

§

impl<RS> Send for ConcurrentInterner<RS>

§

impl<RS> Sync for ConcurrentInterner<RS>

§

impl<RS> Unpin for ConcurrentInterner<RS>
where RS: Unpin,

§

impl<RS> UnwindSafe for ConcurrentInterner<RS>
where RS: UnwindSafe,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.