Skip to main content

flowlog_runtime/
intern.rs

1//! Thread-safe string interning via `lasso::ThreadedRodeo`.
2
3use lasso::{Spur, ThreadedRodeo};
4use std::sync::LazyLock;
5
6/// Global string interner shared across all FlowLog engines in the process.
7///
8/// **Limitation**: this is a process-local pool. In a distributed DD
9/// deployment (multiple machines), each process gets its own independent
10/// `INTERNER`, so `Spur` values are NOT comparable across machines.
11/// Distributed support would require a global interning protocol or
12/// switching back to `String`-keyed collections.
13pub static INTERNER: LazyLock<ThreadedRodeo> = LazyLock::new(ThreadedRodeo::default);
14
15const MAX_RETRIES: usize = 1024;
16
17/// Intern a string, returning its [`Spur`] handle.
18#[inline(always)]
19pub fn intern(s: &str) -> Spur {
20    for _ in 0..MAX_RETRIES {
21        match INTERNER.try_get_or_intern(s) {
22            Ok(key) => return key,
23            Err(_) => std::thread::yield_now(),
24        }
25    }
26    panic!("string interner failed after {MAX_RETRIES} attempts for {s:?}");
27}
28
29/// Resolve a [`Spur`] handle back to a `&'static str`.
30#[inline(always)]
31pub fn resolve(key: Spur) -> &'static str {
32    INTERNER.resolve(&key)
33}