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

Optimize TLS on Windows #102655

Merged
merged 1 commit into from
Oct 13, 2022
Merged

Optimize TLS on Windows #102655

merged 1 commit into from
Oct 13, 2022

Conversation

joboet
Copy link
Contributor

@joboet joboet commented Oct 4, 2022

This implements the suggestion in the current TLS code to embed the linked list of destructors in the StaticKey structure to save allocations. Additionally, locking is avoided when no destructor needs to be run. By using one Windows-provided Once per key instead of a global lock, locking is more finely-grained (this unblocks #100579).

@rustbot rustbot added T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 4, 2022
@rust-highfive
Copy link
Collaborator

r? @ChrisDenton

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

@rustbot
Copy link
Collaborator

rustbot commented Oct 4, 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 rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 4, 2022
Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

This approach seems reasonable to me. I just have a few tiny nits but nothing major.

library/std/src/sys/windows/thread_local_key.rs Outdated Show resolved Hide resolved
library/std/src/sys/windows/thread_local_key.rs Outdated Show resolved Hide resolved
@joboet
Copy link
Contributor Author

joboet commented Oct 7, 2022

@ChrisDenton Should I clean up the CI changes now? They obviously cannot be merged.

@ChrisDenton
Copy link
Member

Yes please. Could you also squish the commits?

Speaking of, I notice you've moved some functions around (run_dtors has been moved up and get/set have switched places). I don't mind that but could you put any moves into a commit separate from the changes so that history/diff tools can understand what happened.

@joboet
Copy link
Contributor Author

joboet commented Oct 8, 2022

Yes please. Could you also squish the commits?

Speaking of, I notice you've moved some functions around (run_dtors has been moved up and get/set have switched places). I don't mind that but could you put any moves into a commit separate from the changes so that history/diff tools can understand what happened.

Done 🎉

@ChrisDenton
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 10, 2022

📌 Commit d457801 has been approved by ChrisDenton

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 10, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 11, 2022
Optimize TLS on Windows

This implements the suggestion in the current TLS code to embed the linked list of destructors in the `StaticKey` structure to save allocations. Additionally, locking is avoided when no destructor needs to be run. By using one Windows-provided `Once` per key instead of a global lock, locking is more finely-grained (this unblocks rust-lang#100579).
@bors
Copy link
Contributor

bors commented Oct 13, 2022

⌛ Testing commit d457801 with merge fa0ca78...

@bors
Copy link
Contributor

bors commented Oct 13, 2022

☀️ Test successful - checks-actions
Approved by: ChrisDenton
Pushing fa0ca78 to master...

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

Finished benchmarking commit (fa0ca78): 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.

mean1 range count2
Regressions ❌
(primary)
0.9% [0.9%, 0.9%] 1
Regressions ❌
(secondary)
4.5% [2.1%, 8.2%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.9% [0.9%, 0.9%] 1

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.

mean1 range count2
Regressions ❌
(primary)
3.1% [2.1%, 4.2%] 3
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
-2.6% [-2.7%, -2.6%] 2
Improvements ✅
(secondary)
-2.6% [-3.2%, -1.9%] 3
All ❌✅ (primary) 0.8% [-2.7%, 4.2%] 5

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 14, 2022
…sDenton

Avoid dropping TLS Key on sgx

rust-lang#102655 reenabled dropping thread local `Key` on every platform ([library/std/src/sys_common/thread_local_key.rs](rust-lang-ci@fa0ca78#diff-5cb9acf9e243f35c975fa9fbac4885519dc104626bc03610dfa7a20bc79641ceL237-R215)). That's causing problems at least for sgx.

cc: `@jethrogb` `@ChrisDenton`
fmease added a commit to fmease/rust that referenced this pull request Apr 24, 2024
fix weak memory bug in TLS on Windows

We need to store the `key` *after* we register the dtor.

Now I hope there isn't also some other reason why we have to actually register the dtor last... `@joboet` is there a reason you picked this particular order in rust-lang#102655?

Fixes rust-lang#123583
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 24, 2024
Rollup merge of rust-lang#124281 - RalfJung:win-tls, r=joboet

fix weak memory bug in TLS on Windows

We need to store the `key` *after* we register the dtor.

Now I hope there isn't also some other reason why we have to actually register the dtor last... `@joboet` is there a reason you picked this particular order in rust-lang#102655?

Fixes rust-lang#123583
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Apr 25, 2024
fix weak memory bug in TLS on Windows

We need to store the `key` *after* we register the dtor.

Now I hope there isn't also some other reason why we have to actually register the dtor last... `@joboet` is there a reason you picked this particular order in rust-lang/rust#102655?

Fixes rust-lang/rust#123583
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-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. 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.

None yet

7 participants