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

use RefLock in GatedSpans of Session #107439

Closed
wants to merge 2 commits into from

Conversation

SparrowLii
Copy link
Member

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 using RefCell instead of Lock.

@rustbot
Copy link
Collaborator

rustbot commented Jan 29, 2023

r? @estebank

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 29, 2023
@@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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")

@cjgillot
Copy link
Contributor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 29, 2023
@bors
Copy link
Contributor

bors commented Jan 29, 2023

⌛ Trying commit 67689b3 with merge 1eb77231c061a947484dbf4d8393427fd2d23ac0...

Comment on lines +476 to +477
// `RefLock` is a thread-safe data structure because it can
// only be used within the thread in which it was created.
Copy link
Contributor

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> {
Copy link
Contributor

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)
}
Copy link
Contributor

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?

Copy link
Member Author

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);
Copy link
Contributor

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>>>,
Copy link
Contributor

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?

@Zoxc
Copy link
Contributor

Zoxc commented Jan 29, 2023

This seems to overlap with my OneThread type, basically being OneThread<RefCell<T>>. That's missing the check on drop though.

@bors
Copy link
Contributor

bors commented Jan 29, 2023

☀️ Try build successful - checks-actions
Build commit: 1eb77231c061a947484dbf4d8393427fd2d23ac0 (1eb77231c061a947484dbf4d8393427fd2d23ac0)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1eb77231c061a947484dbf4d8393427fd2d23ac0): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking 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 @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.8% [0.3%, 1.2%] 12
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.8% [0.3%, 1.2%] 12

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [1.2%, 4.5%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 29, 2023
@SparrowLii
Copy link
Member Author

SparrowLii commented Jan 30, 2023

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 SparrowLii marked this pull request as draft January 30, 2023 00:46
@Dylan-DPC Dylan-DPC added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 19, 2023
@Dylan-DPC
Copy link
Member

@SparrowLii any updates on this? thanks

@SparrowLii
Copy link
Member Author

SparrowLii commented Aug 3, 2023

Yea I think this is an out-of-date strategy now. RefLock is almost same with OneThread, and we prefer to use Lock which is more efficient in #111713. As we solved the issue about performance under multi-threads recently, I think we would address this soon.

So I think this draft can be closed now. Thanks for your reminding!

@SparrowLii SparrowLii closed this Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants