summaryrefslogtreecommitdiffstats
path: root/third_party/rust/once_cell/examples/bench_acquire.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /third_party/rust/once_cell/examples/bench_acquire.rs
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/once_cell/examples/bench_acquire.rs')
-rw-r--r--third_party/rust/once_cell/examples/bench_acquire.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/third_party/rust/once_cell/examples/bench_acquire.rs b/third_party/rust/once_cell/examples/bench_acquire.rs
new file mode 100644
index 0000000000..1518047460
--- /dev/null
+++ b/third_party/rust/once_cell/examples/bench_acquire.rs
@@ -0,0 +1,39 @@
+//! Benchmark the overhead that the synchronization of `OnceCell::get` causes.
+//! We do some other operations that write to memory to get an imprecise but somewhat realistic
+//! measurement.
+
+use once_cell::sync::OnceCell;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+const N_THREADS: usize = 16;
+const N_ROUNDS: usize = 1_000_000;
+
+static CELL: OnceCell<usize> = OnceCell::new();
+static OTHER: AtomicUsize = AtomicUsize::new(0);
+
+fn main() {
+ let start = std::time::Instant::now();
+ let threads =
+ (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>();
+ for thread in threads {
+ thread.join().unwrap();
+ }
+ println!("{:?}", start.elapsed());
+ println!("{:?}", OTHER.load(Ordering::Relaxed));
+}
+
+#[inline(never)]
+fn thread_main(i: usize) {
+ // The operations we do here don't really matter, as long as we do multiple writes, and
+ // everything is messy enough to prevent the compiler from optimizing the loop away.
+ let mut data = [i; 128];
+ let mut accum = 0usize;
+ for _ in 0..N_ROUNDS {
+ let _value = CELL.get_or_init(|| i + 1);
+ let k = OTHER.fetch_add(data[accum & 0x7F] as usize, Ordering::Relaxed);
+ for j in data.iter_mut() {
+ *j = (*j).wrapping_add(accum);
+ accum = accum.wrapping_add(k);
+ }
+ }
+}