bench_acquire.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Benchmark the overhead that the synchronization of `OnceCell::get` causes.
  2. //! We do some other operations that write to memory to get an imprecise but somewhat realistic
  3. //! measurement.
  4. use once_cell::sync::OnceCell;
  5. use std::sync::atomic::{AtomicUsize, Ordering};
  6. const N_THREADS: usize = 16;
  7. const N_ROUNDS: usize = 1_000_000;
  8. static CELL: OnceCell<usize> = OnceCell::new();
  9. static OTHER: AtomicUsize = AtomicUsize::new(0);
  10. fn main() {
  11. let start = std::time::Instant::now();
  12. let threads =
  13. (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>();
  14. for thread in threads {
  15. thread.join().unwrap();
  16. }
  17. println!("{:?}", start.elapsed());
  18. println!("{:?}", OTHER.load(Ordering::Relaxed));
  19. }
  20. #[inline(never)]
  21. fn thread_main(i: usize) {
  22. // The operations we do here don't really matter, as long as we do multiple writes, and
  23. // everything is messy enough to prevent the compiler from optimizing the loop away.
  24. let mut data = [i; 128];
  25. let mut accum = 0usize;
  26. for _ in 0..N_ROUNDS {
  27. let _value = CELL.get_or_init(|| i + 1);
  28. let k = OTHER.fetch_add(data[accum & 0x7F] as usize, Ordering::Relaxed);
  29. for j in data.iter_mut() {
  30. *j = (*j).wrapping_add(accum);
  31. accum = accum.wrapping_add(k);
  32. }
  33. }
  34. }