-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
use RefLock
in GatedSpans
of Session
#107439
Conversation
r? @estebank (rustbot has picked a reviewer for you, use r? to override) |
@@ -460,6 +463,91 @@ impl<T: Clone> Clone for Lock<T> { | |||
} | |||
} | |||
|
|||
fn next() -> NonZeroUsize { | |||
static COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(1); | |||
NonZeroUsize::new(COUNTER.fetch_add(1, Ordering::SeqCst)).expect("more than usize::MAX threads") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NonZeroUsize::new(COUNTER.fetch_add(1, Ordering::SeqCst)).expect("more than usize::MAX threads") | |
NonZeroUsize::new(COUNTER.fetch_add(1, Ordering::Relaxed)).expect("more than usize::MAX threads") |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
⌛ Trying commit 67689b3 with merge 1eb77231c061a947484dbf4d8393427fd2d23ac0... |
// `RefLock` is a thread-safe data structure because it can | ||
// only be used within the thread in which it was created. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment probably deserves to be on the unsafe impl Sync
.
|
||
// `RefLock` is a thread-safe data structure because it can | ||
// only be used within the thread in which it was created. | ||
pub struct RefLock<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name is not very descriptive.
SingleThreadAccess
?
pub(crate) fn get_thread_id() -> NonZeroUsize { | ||
thread_local!(static THREAD_ID: NonZeroUsize = next()); | ||
THREAD_ID.with(|&x| x) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have something in std::thread
or in rayon
for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, it looks like we can use std::thread::ThreadId
here
|
||
#[inline(always)] | ||
fn assert_thread(&self) { | ||
assert_eq!(get_thread_id(), self.thread_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check, the drop check and the Sync
impl could be disabled when the parallel compiler is also disabled.
@@ -32,7 +32,7 @@ pub type CrateCheckConfig = CheckCfg<Symbol>; | |||
/// used and should be feature gated accordingly in `check_crate`. | |||
#[derive(Default)] | |||
pub struct GatedSpans { | |||
pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>, | |||
pub spans: RefLock<FxHashMap<Symbol, Vec<Span>>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be a probably for say, parsing files in parallel?
This seems to overlap with my |
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (1eb77231c061a947484dbf4d8393427fd2d23ac0): comparison URL. Overall result: ❌ regressions - ACTION NEEDEDBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. |
Thanks for reviews! It looks like this runtime check still comes with a large performance penalty. I'll try some other things and in the meantime turn this PR into a draft. We can still test the performance again by turning off the runtime checks as @Zoxc said. |
@SparrowLii any updates on this? thanks |
Yea I think this is an out-of-date strategy now. So I think this draft can be closed now. Thanks for your reminding! |
part of #101566
RefLock
is a thread-safe data structure because it can only be accessed within the thread in which it was created.The
GatedSpans
in the Session will only be accessed in a single thread, so we can reduce the access cost in a multi-threaded environment by usingRefCell
instead ofLock
.