-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
Some changes occurred in library/proc_macro/src/bridge cc @rust-lang/wg-rls-2 |
There was a problem hiding this 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)
dc28e1d
to
d5b5c91
Compare
This comment has been minimized.
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
d5b5c91
to
efda497
Compare
@bors r+ rollup=never (new state in potentially critical path, hopefully perf is neutral 🤞) |
☀️ Test successful - checks-actions |
Finished benchmarking commit (5b4bd15): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDNext Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Footnotes |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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 :(
This is a potential fix to the perf regression from rust-lang#101414. r? @eddyb
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 |
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 theproc-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 aBTreeMap
(as valid symbol id ranges could now bedisjoint), 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 athread-local in the
proc_macro
crate was chosen to add an assertion sothat
rust-analyzer
is aware of the issue if it implementsexpand_expr
in the future.r? @eddyb