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

std: use futex in Once #99505

Merged
merged 1 commit into from
Oct 8, 2022
Merged

std: use futex in Once #99505

merged 1 commit into from
Oct 8, 2022

Conversation

joboet
Copy link
Contributor

@joboet joboet commented Jul 20, 2022

Now that we have efficient locks, let's optimize the rest of sync as well. This PR adds a futex-based implementation for Once, which drastically simplifies the implementation compared to the generic version, which is provided as fallback for platforms without futex (Windows only supports them on newer versions, so it uses the fallback for now).

Instead of storing a linked list of waiters, the new implementation adds another state (QUEUED), which is set when there are waiting threads. These now use futex_wait on that state and are woken by the running thread when it finishes and notices the QUEUED state, thereby avoiding unnecessary calls to futex_wake_all.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 20, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @kennytm

(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 Jul 20, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@joboet
Copy link
Contributor Author

joboet commented Jul 20, 2022

I heard you have little time, @kennytm, please reassign if I'm wrong!

@joboet
Copy link
Contributor Author

joboet commented Jul 23, 2022

r? rust-lang/libs

@Mark-Simulacrum
Copy link
Member

r? @m-ou-se

@joboet joboet force-pushed the futex_once branch 2 times, most recently from c79c240 to 198e05b Compare September 7, 2022 17:12
@thomcc
Copy link
Member

thomcc commented Sep 27, 2022

r? @thomcc

@rust-highfive rust-highfive assigned thomcc and unassigned m-ou-se Sep 27, 2022
Comment on lines +137 to +124
// This is a non-generic function to reduce the monomorphization cost of
// using `call_once` (this isn't exactly a trivial or small implementation).
Copy link
Contributor Author

@joboet joboet Sep 28, 2022

Choose a reason for hiding this comment

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

I've copied this from the old implementation, but I'm not sure whether it's really necessary?

@joboet
Copy link
Contributor Author

joboet commented Sep 28, 2022

There was a bug in the futex implementation which I've now resolved (changing the state from RUNNING to QUEUED did not update the local state variable). I added a small optimization to the generic implementation as well, eliminating one load. (In hindsight, maybe that should have been a second commit. Oh well...)

@thomcc
Copy link
Member

thomcc commented Sep 29, 2022

Is generic.rs equivalent to the old code, or does it need close scrutiny?

@joboet
Copy link
Contributor Author

joboet commented Sep 29, 2022

It's basically the same, except for some style changes (using convenience functions for masking the pointers, making the loop in wait smaller) and the usage of UnsafeCell<ManuallyDrop<Thread>> instead of Cell<Option<Thread>> in Waiter (since using Cell in non-sync environments is unidiomatic).

@thomcc
Copy link
Member

thomcc commented Sep 30, 2022

Can you put the changes into a separate commit from the file move? I really don't want to review the entire generic.rs, especially given that the current impl has been proven correct (IIRC).

Copy link
Member

@thomcc thomcc left a comment

Choose a reason for hiding this comment

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

Not done reviewing futex.rs, only nits so far, but I have to run for a bit.

A bit of a meta-comment though, does this actually improve perf here? Our current Once code is actually quite good IMO.

library/std/src/sys_common/once/futex.rs Outdated Show resolved Hide resolved
library/std/src/sys_common/once/futex.rs Outdated Show resolved Hide resolved
@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 1, 2022
@joboet
Copy link
Contributor Author

joboet commented Oct 1, 2022

Cool, this really looks like a small but noticeable improvement (println! uses OnceLock so it makes sense that that benchmark should improve)!

Copy link
Member

@thomcc thomcc left a comment

Choose a reason for hiding this comment

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

Thank you, and sorry for the delay (didn't want to halfass the review of a synchronization primitive). The new impl is very clean, it sure is nice when we don't have to manage the linked list ourselves...

@thomcc
Copy link
Member

thomcc commented Oct 6, 2022

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Oct 6, 2022

📌 Commit 66278fd73072e0c449a245efcf22d48485d689c7 has been approved by thomcc

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 Oct 6, 2022
@joboet
Copy link
Contributor Author

joboet commented Oct 7, 2022

@bors r-
I want to squash that revert commit first...

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 7, 2022
@joboet
Copy link
Contributor Author

joboet commented Oct 7, 2022

@rustbot ready
@thomcc done 🎉

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 7, 2022
@thomcc
Copy link
Member

thomcc commented Oct 8, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Oct 8, 2022

📌 Commit 5d0211d has been approved by thomcc

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 Oct 8, 2022
@bors
Copy link
Contributor

bors commented Oct 8, 2022

⌛ Testing commit 5d0211d with merge a688a03...

@bors
Copy link
Contributor

bors commented Oct 8, 2022

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing a688a03 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 8, 2022
@bors bors merged commit a688a03 into rust-lang:master Oct 8, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 8, 2022
@joboet joboet deleted the futex_once branch October 8, 2022 07:13
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a688a03): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

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

mean1 range count2
Regressions ❌
(primary)
0.5% [0.5%, 0.5%] 1
Regressions ❌
(secondary)
1.7% [1.0%, 3.3%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 9
All ❌✅ (primary) 0.5% [0.5%, 0.5%] 1

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.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [1.1%, 2.8%] 4
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
-4.4% [-5.6%, -2.6%] 3
All ❌✅ (primary) -0.6% [-0.6%, -0.6%] 1

Cycles

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

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@rustbot rustbot added the perf-regression Performance regression. label Oct 8, 2022
@rylev
Copy link
Member

rylev commented Oct 11, 2022

Looks like this ended up being a small regression. I think the regression results are small and neutral enough that we don't need to investigate.

@rustbot labels: perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Oct 11, 2022
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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.