Skip to content

Commit 12e4907

Browse files
authored
Rollup merge of #92139 - dtolnay:backtrace, r=m-ou-se
Change Backtrace::enabled atomic from SeqCst to Relaxed This atomic is not synchronizing anything outside of its own value, so we don't need the `Acquire`/`Release` guarantee that all memory operations prior to the store are visible after the subsequent load, nor the `SeqCst` guarantee of all threads seeing all of the sequentially consistent operations in the same order. Using `Relaxed` reduces the overhead of `Backtrace::capture()` in the case that backtraces are not enabled. ## Benchmark ```rust #![feature(backtrace)] use std::backtrace::Backtrace; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread; use std::time::Instant; fn main() { let begin = Instant::now(); let mut threads = Vec::new(); for _ in 0..64 { threads.push(thread::spawn(|| { for _ in 0..10_000_000 { let _ = Backtrace::capture(); static LOL: AtomicUsize = AtomicUsize::new(0); LOL.store(1, Ordering::Release); } })); } for thread in threads { let _ = thread.join(); } println!("{:?}", begin.elapsed()); } ``` **Before:** 6.73 seconds **After:** 5.18 seconds
2 parents 554ad50 + 984b10d commit 12e4907

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

library/std/src/backtrace.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ use crate::cell::UnsafeCell;
9999
use crate::env;
100100
use crate::ffi::c_void;
101101
use crate::fmt;
102-
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
102+
use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
103103
use crate::sync::Once;
104104
use crate::sys_common::backtrace::{lock, output_filename};
105105
use crate::vec::Vec;
@@ -256,7 +256,7 @@ impl Backtrace {
256256
// backtrace captures speedy, because otherwise reading environment
257257
// variables every time can be somewhat slow.
258258
static ENABLED: AtomicUsize = AtomicUsize::new(0);
259-
match ENABLED.load(SeqCst) {
259+
match ENABLED.load(Relaxed) {
260260
0 => {}
261261
1 => return false,
262262
_ => return true,
@@ -268,7 +268,7 @@ impl Backtrace {
268268
Err(_) => false,
269269
},
270270
};
271-
ENABLED.store(enabled as usize + 1, SeqCst);
271+
ENABLED.store(enabled as usize + 1, Relaxed);
272272
enabled
273273
}
274274

0 commit comments

Comments
 (0)