|
| 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