diff --git a/Cargo.toml b/Cargo.toml index 742b4a4..2309c76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ ahash = { version = "0.8.3", default-features = false } criterion = "0.4" crossbeam-channel = "0.5" crossbeam-utils = "0.8" +hash_str = { version = "0.2.0", features = [ "cache", "global" ] } libc = "0.2" serde_json = "1" string-interner = "0.13" diff --git a/benches/creation.rs b/benches/creation.rs index 3e59aa1..b82d8c9 100644 --- a/benches/creation.rs +++ b/benches/creation.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use string_cache::DefaultAtom; use string_interner::StringInterner; -use ustr::*; +use ustr::ustr; use parking_lot::Mutex; @@ -42,6 +42,58 @@ fn criterion_benchmark(c: &mut Criterion) { }); }); + let hash_str_cache = hash_str::get_cache(); + + let s = raft.clone(); + c.bench_function("single raft hash_str global", move |b| { + b.iter(|| { + unsafe { hash_str::_clear_cache() }; + for s in s.iter().cycle().take(100_000) { + black_box(hash_str_cache.intern(s)); + } + }); + }); + + let s = raft.clone(); + c.bench_function("single raft hash_str local", move |b| { + b.iter(|| { + let host = hash_str::HashStrHost::new(); + let mut cache = hash_str::HashStrCache::new(); + for s in s.iter().cycle().take(100_000) { + black_box(cache.intern_with(&host, s)); + } + }); + }); + + let s = raft.clone(); + let precomputed: Vec<_> = s.iter().map(|s| ustr(s)).collect(); + c.bench_function("single raft ustr precomputed", move |b| { + let mut cache = ustr::UstrMap::default(); + b.iter(|| { + cache.clear(); + for &s in precomputed.iter().cycle().take(100_000) { + black_box(cache.entry(s).or_insert(s)); + } + }); + }); + + let s = raft.clone(); + let precomputed_host = hash_str::HashStrHost::new(); + let mut precomputed_cache = hash_str::HashStrCache::new(); + let precomputed: Vec<_> = s + .iter() + .map(|s| precomputed_cache.intern_with(&precomputed_host, s)) + .collect(); + c.bench_function("single raft hash_str local precomputed", move |b| { + let mut cache = hash_str::HashStrCache::new(); + b.iter(|| { + cache.clear(); + for s in precomputed.iter().cycle().take(100_000) { + black_box(cache.cache(s)); + } + }); + }); + let s = raft.clone(); c.bench_function("single raft string-interner", move |b| { b.iter(|| { diff --git a/src/stringcache.rs b/src/stringcache.rs index fca1d7f..6fdd297 100644 --- a/src/stringcache.rs +++ b/src/stringcache.rs @@ -407,4 +407,4 @@ impl StringCacheEntry { std::mem::align_of::(), )) } -} \ No newline at end of file +}