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 IStr
s,
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:
ahash::RandomState
.rustc_hash::FxHasher
wrapped by aBuildHasherDefault
.
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>
impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>
Sourcepub fn new(arena_count: NonZeroU8, random_state: RS) -> ConcurrentInterner<RS>
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>
impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>
Sourcepub fn get_member(&self) -> ConcurrentInternerMember<'_, RS>
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.
Sourcepub fn get_string(&self, istr: IStr) -> String
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>
impl<RS: Send + Sync + Clone + BuildHasher> ConcurrentInterner<RS>
Sourcepub fn freeze(self) -> FrozenInterner<RS>
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>
impl<RS: Send + Sync + Clone + BuildHasher> Debug for ConcurrentInterner<RS>
Source§impl<RS: Send + Sync + Clone + BuildHasher> From<ConcurrentInterner<RS>> for FrozenInterner<RS>
impl<RS: Send + Sync + Clone + BuildHasher> From<ConcurrentInterner<RS>> for FrozenInterner<RS>
Source§fn from(interner: ConcurrentInterner<RS>) -> FrozenInterner<RS>
fn from(interner: ConcurrentInterner<RS>) -> FrozenInterner<RS>
Identical to ConcurrentInterner::freeze
.