Expand description
Key interning for memory-efficient repeated-key workloads. Key interning for memory-efficient repeated-key workloads.
YAML mappings frequently repeat the same key string across
many records — metadata, labels, name, version,
apiVersion in Kubernetes-shaped documents, level, time,
service in structured logs. Each fresh parse allocates a
brand-new String for every key. For a 10 000-record stream
that’s tens of thousands of identical heap allocations.
KeyInterner is the primitive
that lets a caller dedupe
those allocations. The intern table maps each unique key text
to a shared Arc<str>; subsequent calls with the same key
return a clone of the existing Arc instead of allocating.
Memory model: Arc<str> is two pointers (16 bytes on a 64-bit
target) plus the actual string bytes shared via the Arc.
For a key that occurs N times, the interner’s footprint is
O(unique_keys * key_bytes) + N * 16 versus the
non-interned baseline of O(N * key_bytes). For repeated keys
the win is substantial — 20-byte keys repeated 10 000× drop
from ~200 KB to ~20 bytes + 160 KB of Arc pointers.
§Example: deduplicating keys across a parsed stream
use noyalib::interner::KeyInterner;
let mut interner = KeyInterner::new();
let a = interner.intern("metadata");
let b = interner.intern("metadata");
// Same `Arc` — second call returns a clone of the cached
// entry. `Arc::ptr_eq` confirms the underlying allocation
// is shared.
assert!(std::sync::Arc::ptr_eq(&a, &b));
assert_eq!(&*a, "metadata");§Future direction
v0.0.1 ships the primitive without changing
crate::Mapping’s storage type — the Mapping’s public API
is String-keyed and that contract is preserved across the
initial release. A future major version may switch the
internal key representation to Arc<str> and use the
interner transparently during parse; the public surface would
continue to behave like a String-keyed map via deref
semantics.
Structs§
- KeyInterner
- Interner for
&str→Arc<str>deduplication.