Skip to main content

Crate sui_intern

Crate sui_intern 

Source
Expand description

String interning for attribute names and identifiers.

Converts heap-allocated String comparisons into cheap u32 comparisons. Every unique string is stored exactly once; lookups and comparisons use the Symbol handle (a u32 index).

§Performance Impact

Attrset key operations (GetAttr, HasAttr, MakeAttrs, UpdateAttrs) go from O(n) string comparison to O(1) integer comparison. This is the single highest-ROI optimization for Nix evaluation because nixpkgs is dominated by attrset operations.

§Storage

Strings are stored as Rc<str>. The forward map’s key and the reverse strings vector share the same allocation — no double-allocation on intern, and resolve_rc returns a cheap Rc::clone instead of a full String::from copy. Hashing uses FxHashMap (rustc-hash) — ~2x faster than SipHash for the short strings typical of Nix keys and identifiers.

§Thread-Local Helpers

For convenience in single-threaded evaluation, this crate provides intern() and resolve() free functions that operate on a thread-local Interner instance. prewarm() pre-interns a curated set of hot nixpkgs symbols so common names get low indices and the hashmap’s initial resize cost is paid upfront.

Structs§

Interner
A string interner that maps strings to Symbol handles.
Symbol
An interned string handle — a cheap, copyable, comparable token.

Functions§

intern
Intern a string using the thread-local interner.
lookup
Look up a symbol for a string in the thread-local interner without interning it.
prewarm
Intern the hot nixpkgs/flake/stdenv symbol set so they get low Symbol indices and the thread-local hashmap pays its first few resizes upfront instead of on the eval hot path.
resolve
Resolve a symbol using the thread-local interner.
resolve_rc
Resolve a symbol to a shared Rc<str> handle. Zero-copy.
with_resolved
Borrow the resolved string inside a closure without allocating. The thread-local interner stays locked for the duration of f.