-
Notifications
You must be signed in to change notification settings - Fork 1.9k
proc-macro-srv: Reimplement token trees via immutable trees #21097
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
+1,647
−1,190
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fb1f83e to
4774e6e
Compare
4774e6e to
b5fc29e
Compare
Shourya742
approved these changes
Nov 23, 2025
Contributor
Shourya742
left a comment
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.
LGTM. Just some questions.
ChayimFriedman2
approved these changes
Nov 23, 2025
Contributor
ChayimFriedman2
left a comment
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.
Mostly skimmed, but LGTM.
f337eb3 to
dee751d
Compare
dee751d to
9b767f3
Compare
Member
Author
|
This might've introduced a bug according to https://youtrack.jetbrains.com/issue/RUST-19283 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For macros, rust has the concept of TokenTrees and TokenStreams which are basically a tree-like form of the underlying tokenized input with
(){}[]delimiters forming Groups (internal nodes) and all other tokens forming leaf nodes of the tree.In rust-analyzer today these trees are implemented as a flattened list using indices to reduce their memory impact as we keep a lot of these memoized (implementation). This comes at the cost of mutating operations being more expensive though and we especially feel this in proc-macro expansion.
As it turns out, one of the primitive and very commonly used operations on token streams in proc-macros is concatenation, an operation that with our model is notoriously expensive as we need to do a deepclone of the first stream, append the second stream and rewrite the internal indices. Combine this with deeply recursive concatenations as is common with the
quote!macro and we run into issues where the proc-macro server can take 8 seconds to expand these 8 derive invocations ofnum_derive::FromPrimitiveon decent machines.Fortunately, the proc-macro server has no reason to use the same implementation of TokenStreams and TokenTrees as rust-analyzer since it doesn't persist them in memory anways. So instead, to gain good concatenation performance, we can do with what rustc does and store them as immutable trees with shared internal nodes.
Before this change cache priming took 56 seconds on Zed's codebase, with the new proc-macro server it now takes 45.