Skip to content

proc_macro/bridge: use the cross-thread executor for nested proc-macros #101414

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

Merged
merged 1 commit into from
Sep 5, 2022

Conversation

mystor
Copy link
Contributor

@mystor mystor commented Sep 4, 2022

While working on some other changes in the bridge, I noticed that when
running a nested proc-macro (which is currently only possible using
the unstable TokenStream::expand_expr), any symbols held by the
proc-macro client would be invalidated, as the same thread would be used
for the nested macro by default, and the interner doesn't handle nested
use.

After discussing with @eddyb, we decided the best approach might be to
force the use of the cross-thread executor for nested invocations, as it
will never re-use thread-local storage, avoiding the issue. This
shouldn't impact performance, as expand_expr is still unstable, and
infrequently used.

This was chosen rather than making the client symbol interner handle
nested invocations, as that would require replacing the internal
interner Vec with a BTreeMap (as valid symbol id ranges could now be
disjoint), and the symbol interner is known to be fairly perf-sensitive.

This patch adds checks to the execution strategy to use the cross-thread
executor when doing nested invocations. An alternative implementation
strategy could be to track this information in the ExtCtxt, however a
thread-local in the proc_macro crate was chosen to add an assertion so
that rust-analyzer is aware of the issue if it implements
expand_expr in the future.

r? @eddyb

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 4, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 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

Some changes occurred in library/proc_macro/src/bridge

cc @rust-lang/wg-rls-2

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 4, 2022
Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

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

r=me with the nits fixed but make sure to also note this on the expand_expr tracking issue, as something to consider when stabilizing (assuming you don't want to block on a wider discussion now)

@mystor mystor force-pushed the pm_nested_cross_thread branch from dc28e1d to d5b5c91 Compare September 4, 2022 18:02
@rust-log-analyzer

This comment has been minimized.

While working on some other changes in the bridge, I noticed that when
running a nested proc-macro (which is currently only possible using
the unstable `TokenStream::expand_expr`), any symbols held by the
proc-macro client would be invalidated, as the same thread would be used
for the nested macro by default, and the interner doesn't handle nested
use.

After discussing with @eddyb, we decided the best approach might be to
force the use of the cross-thread executor for nested invocations, as it
will never re-use thread-local storage, avoiding the issue. This
shouldn't impact performance, as expand_expr is still unstable, and
infrequently used.

This was chosen rather than making the client symbol interner handle
nested invocations, as that would require replacing the internal
interner `Vec` with a `BTreeMap` (as valid symbol id ranges could now be
disjoint), and the symbol interner is known to be fairly perf-sensitive.

This patch adds checks to the execution strategy to use the cross-thread
executor when doing nested invocations. An alternative implementation
strategy could be to track this information in the `ExtCtxt`, however a
thread-local in the `proc_macro` crate was chosen to add an assertion so
that `rust-analyzer` is aware of the issue if it implements
`expand_expr` in the future.

r? @eddyb
@mystor mystor force-pushed the pm_nested_cross_thread branch from d5b5c91 to efda497 Compare September 4, 2022 18:06
@eddyb
Copy link
Member

eddyb commented Sep 4, 2022

@bors r+ rollup=never (new state in potentially critical path, hopefully perf is neutral 🤞)

@bors
Copy link
Collaborator

bors commented Sep 4, 2022

📌 Commit efda497 has been approved by eddyb

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 Sep 4, 2022
@bors
Copy link
Collaborator

bors commented Sep 5, 2022

⌛ Testing commit efda497 with merge 5b4bd15...

@bors
Copy link
Collaborator

bors commented Sep 5, 2022

☀️ Test successful - checks-actions
Approved by: eddyb
Pushing 5b4bd15 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 5, 2022
@bors bors merged commit 5b4bd15 into rust-lang:master Sep 5, 2022
@rustbot rustbot added this to the 1.65.0 milestone Sep 5, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5b4bd15): 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.9% [0.4%, 1.4%] 10
Regressions ❌
(secondary)
1.1% [0.6%, 1.7%] 9
Improvements ✅
(primary)
-1.2% [-1.3%, -1.2%] 2
Improvements ✅
(secondary)
-3.4% [-3.9%, -3.0%] 6
All ❌✅ (primary) 0.5% [-1.3%, 1.4%] 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.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-2.2%, -0.9%] 3
Improvements ✅
(secondary)
-2.3% [-3.6%, -1.7%] 5
All ❌✅ (primary) -1.5% [-2.2%, -0.9%] 3

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

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@rustbot rustbot added the perf-regression Performance regression. label Sep 5, 2022
Comment on lines +162 to +177
impl RunningSameThreadGuard {
fn new() -> Self {
let already_running = ALREADY_RUNNING_SAME_THREAD.replace(true);
assert!(
!already_running,
"same-thread nesting (\"reentrance\") of proc macro executions is not supported"
);
RunningSameThreadGuard(())
}
}

impl Drop for RunningSameThreadGuard {
fn drop(&mut self) {
ALREADY_RUNNING_SAME_THREAD.set(false);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Oops, these two functions may need #[inline]? I should've really ran perf, my bad :(

mystor added a commit to mystor/rust that referenced this pull request Sep 5, 2022
This is a potential fix to the perf regression from rust-lang#101414.

r? @eddyb
@pnkfelix
Copy link
Member

As noted elsewhere #101461 (comment) ...

... the regressions flagged here are almost certainly noise from recently arising bimodality in certain benchmarks.

@rustbot label: perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Sep 14, 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-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