pub struct StringInterner { /* private fields */ }Expand description
A string interner that deduplicates strings to reduce memory usage
This is particularly useful for RDF parsing where:
- Common predicates (rdf:type, rdfs:label, etc.) appear many times
- Namespace URIs are repeated frequently
- Language tags and datatypes are reused
§Example
use oxirs_ttl::toolkit::StringInterner;
use std::sync::Arc;
let mut interner = StringInterner::new();
// These will all point to the same underlying string
let s1 = interner.intern("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
let s2 = interner.intern("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
// The Arc pointers should be the same (cheap to clone)
assert!(Arc::ptr_eq(&s1, &s2));Implementations§
Source§impl StringInterner
impl StringInterner
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new string interner with pre-allocated capacity
Sourcepub fn with_common_namespaces() -> Self
pub fn with_common_namespaces() -> Self
Create a new interner pre-populated with common RDF namespaces
Sourcepub fn intern(&mut self, s: &str) -> Arc<String>
pub fn intern(&mut self, s: &str) -> Arc<String>
Intern a string, returning an Arc to the deduplicated version
If the string has been seen before, returns the existing Arc. Otherwise, allocates a new String and stores it.
Sourcepub fn intern_if_beneficial<'a>(
&mut self,
s: &'a str,
min_length: usize,
) -> Cow<'a, str>
pub fn intern_if_beneficial<'a>( &mut self, s: &'a str, min_length: usize, ) -> Cow<'a, str>
Intern a string only if it’s likely to be repeated
Uses heuristics to decide whether to intern:
- Always intern if it contains common namespace patterns
- Always intern if it’s a common language tag
- Otherwise, intern if length > threshold
Sourcepub fn stats(&self) -> &InternerStats
pub fn stats(&self) -> &InternerStats
Get interning statistics
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Estimate memory usage in bytes
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the hash map to fit the current number of entries
Trait Implementations§
Source§impl Clone for StringInterner
impl Clone for StringInterner
Source§fn clone(&self) -> StringInterner
fn clone(&self) -> StringInterner
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for StringInterner
impl Debug for StringInterner
Auto Trait Implementations§
impl Freeze for StringInterner
impl RefUnwindSafe for StringInterner
impl Send for StringInterner
impl Sync for StringInterner
impl Unpin for StringInterner
impl UnsafeUnpin for StringInterner
impl UnwindSafe for StringInterner
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more