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 only one shard with a single thread #111755

Merged
merged 3 commits into from
May 28, 2023
Merged

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented May 19, 2023

This changes Sharded to only access a single shard using a mask set to 0 when a single thread is used, which leads to cache utilization improvements.

Performance improvement with 1 thread and cfg(parallel_compiler):

BenchmarkBeforeAfter
TimeTime%
🟣 clap:check1.7402s1.7004s💚 -2.29%
🟣 hyper:check0.2633s0.2550s💚 -3.12%
🟣 regex:check0.9716s0.9482s💚 -2.41%
🟣 syn:check1.5679s1.5358s💚 -2.05%
🟣 syntex_syntax:check6.0569s5.9272s💚 -2.14%
Total10.5999s10.3666s💚 -2.20%
Summary1.0000s0.9760s💚 -2.40%

cc @SparrowLii

@rustbot
Copy link
Collaborator

rustbot commented May 19, 2023

r? @petrochenkov

(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 May 19, 2023
@SparrowLii
Copy link
Member

SparrowLii commented May 19, 2023

Sharded uses Lock internal. I'm thinking if it uses Mutex when is_dyn_thread_safe() == true and RefCell otherwise directly, will it be faster?

This needs to be considered in conjunction with #111713.

}

/// The shard is selected by hashing `val` with `FxHasher`.
#[inline]
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
if SHARDS == 1 { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
self.get_shard_by_hash(if SHARDS == 1 { 0 } else { make_hash(val) })
Copy link
Member

Choose a reason for hiding this comment

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

Should we return &self.shards.get_unchecked(0).0 directly if SHARDS == 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It gets masked by 0 later, so it doesn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you make this a branch over cfg(debug_assertions)? I'd rather avoid having some logic over the cfg and some other over the value of SHARDS, even if they are equivalent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Assuming you mean cfg(parallel_compiler) here, it's still useful to disable just sharding by setting SHARDS to 1 to evaluate how effective sharding is.

@petrochenkov
Copy link
Contributor

r? @SparrowLii

@rustbot rustbot assigned SparrowLii and unassigned petrochenkov May 22, 2023
@SparrowLii
Copy link
Member

It looks good to me. I have no privileges so r? @cjgillot

@rustbot rustbot assigned cjgillot and unassigned SparrowLii May 23, 2023
Sharded { shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))) }
Sharded {
#[cfg(parallel_compiler)]
mask: if is_dyn_thread_safe() { SHARDS - 1 } else { 0 },
Copy link
Contributor

Choose a reason for hiding this comment

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

mask needs a comment explaining what it's for.

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

What is the reason for the perf improvement? Is it just a cache effect, or do we gain from skipping some key hashing?


#[inline(always)]
fn count(&self) -> usize {
self.mask() + 1
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs a reminder that mask is 2^n - 1, so adding 1 is correct.

}

/// The shard is selected by hashing `val` with `FxHasher`.
#[inline]
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
if SHARDS == 1 { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
self.get_shard_by_hash(if SHARDS == 1 { 0 } else { make_hash(val) })
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you make this a branch over cfg(debug_assertions)? I'd rather avoid having some logic over the cfg and some other over the value of SHARDS, even if they are equivalent.

@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 May 24, 2023
@bors
Copy link
Contributor

bors commented May 24, 2023

⌛ Trying commit 4d0150d81e6bd2b63caec8aa801ce8098c7d66eb with merge 0224514e56967ce45ee3658e8baf806c75f63c53...

@bors
Copy link
Contributor

bors commented May 24, 2023

☀️ Try build successful - checks-actions
Build commit: 0224514e56967ce45ee3658e8baf806c75f63c53 (0224514e56967ce45ee3658e8baf806c75f63c53)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0224514e56967ce45ee3658e8baf806c75f63c53): comparison URL.

Overall result: no relevant changes - no 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.

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

Instruction count

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

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.2% [-5.2%, -5.2%] 1
All ❌✅ (primary) - - 0

Cycles

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.9%, -2.3%] 4
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 645.989s -> 646.069s (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 24, 2023
@Zoxc
Copy link
Contributor Author

Zoxc commented May 25, 2023

What is the reason for the perf improvement? Is it just a cache effect, or do we gain from skipping some key hashing?

It probably just the cache effect and the replacement of the bounds check by a masking operation. Not sure there's any place where hashing can be skipped atm.

@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 28, 2023

📌 Commit 8abafd0 has been approved by cjgillot

It is now in the queue for this repository.

@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 May 28, 2023
@bors
Copy link
Contributor

bors commented May 28, 2023

⌛ Testing commit 8abafd0 with merge 3fae1b9...

@bors
Copy link
Contributor

bors commented May 28, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 3fae1b9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 28, 2023
@bors bors merged commit 3fae1b9 into rust-lang:master May 28, 2023
@rustbot rustbot added this to the 1.72.0 milestone May 28, 2023
@Zoxc Zoxc deleted the sharded-switch branch May 28, 2023 18:14
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3fae1b9): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) - - 0

Cycles

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

Binary size

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

Bootstrap: 646.068s -> 646.98s (0.14%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

8 participants