Skip to content

Commit d1f000a

Browse files
committed
Add basic lookup benchmark
Based on the benchmark in the thread_local crate. Also benchmarks thread_local + Arc overhead. On my M1 Mac, Arc::clone dwarfs the overhead of all lookups. This is unfortunate, because Arc::clone is required for our crate.
1 parent cba32cb commit d1f000a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ foldhash = "0.1"
1818
# persistent collections
1919
imbl = "5"
2020

21+
[dev-dependencies]
22+
thread_local = "1"
23+
criterion = { version = "0.7" }
24+
2125
[features]
2226
nightly = [
2327
"parking_lot/nightly",
2428
]
2529

30+
[[bench]]
31+
name = "lookup"
32+
harness = false
33+
2634
[lints.rust]
2735
missing-docs = "deny"
2836
rust-2024-compatibility = "warn"

benches/lookup.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![allow(missing_docs)]
2+
use std::hint::black_box;
3+
use std::sync::Arc;
4+
5+
use dropping_thread_local::DroppingThreadLocal;
6+
use thread_local::ThreadLocal;
7+
8+
fn main() {
9+
let mut c = criterion::Criterion::default().configure_from_args();
10+
11+
c.bench_function("std::get", |b| {
12+
std::thread_local!(static LOCAL: Box<i32> = Box::new(0));
13+
LOCAL.with(|x| black_box(**x));
14+
b.iter(|| {
15+
black_box(LOCAL.with(|x| **x));
16+
});
17+
});
18+
19+
c.bench_function("std::get_arc", |b| {
20+
std::thread_local!(static LOCAL: Arc<i32> = Arc::new(0));
21+
LOCAL.with(|x| black_box(**x));
22+
b.iter_with_large_drop(|| {
23+
black_box(LOCAL.with(|x| Arc::clone(x)));
24+
});
25+
});
26+
27+
c.bench_function("thread_local::get", |b| {
28+
let local = ThreadLocal::new();
29+
local.get_or(|| Box::new(0));
30+
b.iter(|| {
31+
black_box(local.get());
32+
});
33+
});
34+
35+
c.bench_function("thread_local::get_arc", |b| {
36+
let local = ThreadLocal::new();
37+
local.get_or(|| Arc::new(0));
38+
b.iter_with_large_drop(|| {
39+
black_box(Arc::clone(local.get().unwrap()));
40+
});
41+
});
42+
43+
c.bench_function("dropping_thread_local::get", |b| {
44+
let local = DroppingThreadLocal::new();
45+
local.get_or_init(|| Box::new(0));
46+
b.iter_with_large_drop(|| {
47+
black_box(local.get());
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)