pub fn intern_string(s: &str) -> Arc<str>Expand description
Intern a string, returning an Arc<str>
If the string has been interned before in this thread, returns the existing Arc (zero allocation). Otherwise, creates a new Arc and caches it for future use.
§Performance
- First call: O(n) allocation + O(1) hash insert
- Subsequent calls: O(1) hash lookup +
Arc::clone - Cache lookup overhead: ~1-2ns per call
§Thread Safety
Each thread has its own cache. This avoids synchronization overhead but means the same string may be allocated multiple times across threads. This is acceptable since:
- JSON parsing is typically single-threaded per document
- Thread-local caches are warmed up independently
- No contention between threads
§Example
use hedl_json::string_cache::intern_string;
use std::sync::Arc;
let s1 = intern_string("user_id");
let s2 = intern_string("user_id");
// Same pointer (no allocation on second call)
assert!(Arc::ptr_eq(&s1, &s2));