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

Simplify proc macro bridge state #122939

Merged
merged 1 commit into from
Mar 26, 2024
Merged

Conversation

joboet
Copy link
Member

@joboet joboet commented Mar 23, 2024

Currently, proc_macro uses a ScopedCell to store the client-side proc-macro bridge. Unfortunately, this requires the Bridge, which has non-negligible size, to be copied out and back again on every access. In some cases, the optimizer might be able to elide these copies, but in general, this is suboptimal.

This PR removes ScopedCell and employs a similar trick as in scoped_tls, meaning that the only thing stored in TLS is a pointer to the state, which now is a RefCell. Access to the pointer is then scoped so that it is always within the lifetime of the reference to the state. Unfortunately, scoped_tls requires the referenced type to be 'static, which Bridge is not, therefore we cannot simply copy that macro but have to reimplement its TLS trick.

This removes the #[forbid(unsafe_code)] on the client module so that we do not have to export Bridge, which currently is private, to the whole crate. I can change that, if necessary.

@rustbot
Copy link
Collaborator

rustbot commented Mar 23, 2024

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2024
@joboet
Copy link
Member Author

joboet commented Mar 23, 2024

@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 Mar 23, 2024
@joboet joboet added the A-proc-macros Area: Procedural macros label Mar 23, 2024
@bors
Copy link
Contributor

bors commented Mar 23, 2024

⌛ Trying commit ac770f7 with merge b9b0f25...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 23, 2024
Simplify proc macro bridge state

Currently, `proc_macro` uses a `ScopedCell` to store the client-side proc-macro bridge. Unfortunately, this requires the `Bridge`, which has non-negligible size, to be copied out and back again on every access. In some cases, the optimizer might be able to elide these copies, but in general, this is suboptimal.

This PR removes `ScopedCell` and employs a similar trick as in [`scoped_tls`](https://crates.io/crates/scoped-tls), meaning that the only thing stored in TLS is a pointer to the state, which now is a `RefCell`. Access to the pointer is then scoped so that it is always within the lifetime of the reference to the state. Unfortunately, `scoped_tls` requires the referenced type to be `'static`, which `Bridge` is not, therefore we cannot simply copy that macro but have to reimplement its TLS trick.

This removes the `#[forbid(unsafe_code)]` on the `client` module so that we do not have to export `Bridge`, which currently is private, to the whole crate. I can change that, if necessary.
@bors
Copy link
Contributor

bors commented Mar 23, 2024

☀️ Try build successful - checks-actions
Build commit: b9b0f25 (b9b0f257f9615732562e3a469bfef0ca3379a935)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b9b0f25): comparison URL.

Overall result: ❌✅ regressions and improvements - 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
Regressions ❌
(secondary)
0.5% [0.4%, 0.6%] 2
Improvements ✅
(primary)
-0.4% [-0.5%, -0.3%] 3
Improvements ✅
(secondary)
-1.2% [-1.9%, -0.5%] 6
All ❌✅ (primary) -0.4% [-0.5%, -0.3%] 3

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

Cycles

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

Binary size

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)
2.3% [2.2%, 2.5%] 4
Regressions ❌
(secondary)
5.9% [5.9%, 5.9%] 3
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 3
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 3
All ❌✅ (primary) 1.0% [-0.6%, 2.5%] 7

Bootstrap: 669.588s -> 670.102s (0.08%)
Artifact size: 315.05 MiB -> 315.01 MiB (-0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 23, 2024
@joboet
Copy link
Member Author

joboet commented Mar 23, 2024

Okay, that's quite good, especially the token-stream-stress. I'll see if integrating the RefCell into the pointer helps tt-muncher.

@rustbot author

@rustbot rustbot 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 Mar 23, 2024
@joboet

This comment was marked as outdated.

@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 Mar 23, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 23, 2024
Simplify proc macro bridge state

Currently, `proc_macro` uses a `ScopedCell` to store the client-side proc-macro bridge. Unfortunately, this requires the `Bridge`, which has non-negligible size, to be copied out and back again on every access. In some cases, the optimizer might be able to elide these copies, but in general, this is suboptimal.

This PR removes `ScopedCell` and employs a similar trick as in [`scoped_tls`](https://crates.io/crates/scoped-tls), meaning that the only thing stored in TLS is a pointer to the state, which now is a `RefCell`. Access to the pointer is then scoped so that it is always within the lifetime of the reference to the state. Unfortunately, `scoped_tls` requires the referenced type to be `'static`, which `Bridge` is not, therefore we cannot simply copy that macro but have to reimplement its TLS trick.

This removes the `#[forbid(unsafe_code)]` on the `client` module so that we do not have to export `Bridge`, which currently is private, to the whole crate. I can change that, if necessary.
@bors

This comment was marked as outdated.

@bors

This comment was marked as outdated.

@rust-timer

This comment has been minimized.

@rust-timer

This comment was marked as outdated.

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 23, 2024
@joboet
Copy link
Member Author

joboet commented Mar 23, 2024

Yeah, that's worse. Let's just use RefCell.
@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 23, 2024
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2024
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Mar 25, 2024

📌 Commit ac770f7 has been approved by petrochenkov

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 Mar 25, 2024
@bors
Copy link
Contributor

bors commented Mar 26, 2024

⌛ Testing commit ac770f7 with merge 536606b...

@bors
Copy link
Contributor

bors commented Mar 26, 2024

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 536606b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 26, 2024
@bors bors merged commit 536606b into rust-lang:master Mar 26, 2024
23 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 26, 2024
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  PR_MESSAGE: Automation to keep dependencies in `Cargo.lock` current.
following is the output from `cargo update`:
  COMMIT_MESSAGE: cargo update 
##[endgroup]
Starting download for Cargo-lock
##[error]Unable to find any artifacts for the associated workflow

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (536606b): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
-0.3% [-0.4%, -0.3%] 2
Improvements ✅
(secondary)
-1.2% [-1.9%, -0.4%] 6
All ❌✅ (primary) -0.3% [-0.4%, -0.3%] 2

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)
1.6% [0.5%, 3.4%] 13
Regressions ❌
(secondary)
3.1% [2.6%, 4.0%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.6% [0.5%, 3.4%] 13

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)
1.1% [0.5%, 3.2%] 11
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.9% [-3.4%, -2.4%] 3
All ❌✅ (primary) 1.1% [0.5%, 3.2%] 11

Binary size

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)
2.3% [2.2%, 2.5%] 4
Regressions ❌
(secondary)
5.9% [5.9%, 5.9%] 3
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 3
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 3
All ❌✅ (primary) 1.0% [-0.6%, 2.5%] 7

Bootstrap: 672.189s -> 671.741s (-0.07%)
Artifact size: 315.72 MiB -> 315.68 MiB (-0.01%)

@rustbot rustbot removed the perf-regression Performance regression. label Mar 26, 2024
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  PR_MESSAGE: Automation to keep dependencies in `Cargo.lock` current.
following is the output from `cargo update`:
  COMMIT_MESSAGE: cargo update 
##[endgroup]
Starting download for Cargo-lock
##[error]Unable to find any artifacts for the associated workflow

@therealprof
Copy link
Contributor

Uhm, why does this have an impact on the binary size... This should not result in different code, should it?

@joboet
Copy link
Member Author

joboet commented Mar 28, 2024

All the changes are in proc-macro crates, so this makes sense. I'd wager that this change led to more proc_macro code being inlined, hence the increase. Given that this is only noticeable on development machines, I don't think this is bad, especially considering the performance improvements we get in return.

@joboet joboet deleted the proc_macro_bridge_state branch March 29, 2024 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants