Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Backtrace::enabled atomic from SeqCst to Relaxed #92139

Merged
merged 1 commit into from
Dec 23, 2021

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Dec 20, 2021

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

#![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

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 20, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Dec 21, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Dec 21, 2021

📌 Commit 984b10d has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 21, 2021
@bors
Copy link
Contributor

bors commented Dec 21, 2021

⌛ Testing commit 984b10d with merge b0dcf2c76b3159084138c63b18bd31373c9e679b...

@bors
Copy link
Contributor

bors commented Dec 21, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 21, 2021
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-msvc-1 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

failures:

---- process::tests::test_interior_nul_in_arg_is_error stdout ----
thread 'process::tests::test_interior_nul_in_arg_is_error' panicked at 'assertion failed: `(left == right)`
  left: `NotFound`,
 right: `InvalidInput`', library\std\src\process\tests.rs:309:19
---- process::tests::test_interior_nul_in_args_is_error stdout ----
thread 'process::tests::test_interior_nul_in_args_is_error' panicked at 'assertion failed: `(left == right)`
  left: `NotFound`,
  left: `NotFound`,
 right: `InvalidInput`', library\std\src\process\tests.rs:317:19
---- process::tests::test_interior_nul_in_current_dir_is_error stdout ----
thread 'process::tests::test_interior_nul_in_current_dir_is_error' panicked at 'assertion failed: `(left == right)`
  left: `NotFound`,
  left: `NotFound`,
 right: `InvalidInput`', library\std\src\process\tests.rs:325:19
---- sys::windows::process::tests::windows_exe_resolver stdout ----
---- sys::windows::process::tests::windows_exe_resolver stdout ----
thread 'sys::windows::process::tests::windows_exe_resolver' panicked at 'assertion failed: resolve_exe(OsStr::new(\"rustc\"), child_paths).is_ok()', library\std\src\sys\windows\process\tests.rs:174:5

failures:
    process::tests::test_interior_nul_in_arg_is_error
    process::tests::test_interior_nul_in_args_is_error
    process::tests::test_interior_nul_in_args_is_error
    process::tests::test_interior_nul_in_current_dir_is_error
    sys::windows::process::tests::windows_exe_resolver

test result: FAILED. 879 passed; 4 failed; 3 ignored; 0 measured; 0 filtered out; finished in 13.83s

error: test failed, to rerun pass '-p std --lib'


command did not execute successfully: "\\\\?\\D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage0\\bin\\cargo.exe" "test" "--target" "x86_64-pc-windows-msvc" "-Zbinary-dep-depinfo" "-j" "8" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace profiler compiler-builtins-c" "--manifest-path" "D:\\a\\rust\\rust\\library/test/Cargo.toml" "-p" "std" "--"


Build completed unsuccessfully in 0:40:00
Build completed unsuccessfully in 0:40:00
make: *** [Makefile:72: ci-subset-1] Error 1

@@ -268,7 +268,7 @@ impl Backtrace {
Err(_) => false,
},
};
ENABLED.store(enabled as usize + 1, SeqCst);
ENABLED.store(enabled as usize + 1, Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about storing boolean as is in atomic?
I managed to remove 1 branch and make code shorter by this:
godbolt

@ehuss
Copy link
Contributor

ehuss commented Dec 22, 2021

The CI error was due to some infrastructure problem. I would retry, but do you want to respond to the comment at #92139 (comment) first?

@dtolnay
Copy link
Member Author

dtolnay commented Dec 22, 2021

That comment seems orthogonal to the change in this PR.

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 22, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 23, 2021
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#88858 (Allow reverse iteration of lowercase'd/uppercase'd chars)
 - rust-lang#91544 (Fix duplicate derive clone suggestion)
 - rust-lang#92026 (Add some JSDoc comments to rustdoc JS)
 - rust-lang#92117 (kmc-solid: Add `std::sys::solid::fs::File::read_buf`)
 - rust-lang#92139 (Change Backtrace::enabled atomic from SeqCst to Relaxed)
 - rust-lang#92146 (Don't emit shared files when scraping examples from dependencies in Rustdoc)
 - rust-lang#92208 (Quote bat script command line)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 12e4907 into rust-lang:master Dec 23, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 23, 2021
@dtolnay dtolnay deleted the backtrace branch December 23, 2021 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants