proc-macro-srv: Reimplement token trees via immutable trees - #21097
Merged
Merged
Conversation
Veykril
force-pushed
the
push-zpqupukpkrts
branch
3 times, most recently
from
November 22, 2025 17:39
fb1f83e to
4774e6e
Compare
Veykril
force-pushed
the
push-zpqupukpkrts
branch
from
November 22, 2025 17:53
4774e6e to
b5fc29e
Compare
Veykril
marked this pull request as ready for review
November 22, 2025 18:12
bit-aloo
approved these changes
Nov 23, 2025
ChayimFriedman2
approved these changes
Nov 23, 2025
ChayimFriedman2
left a comment
Contributor
There was a problem hiding this comment.
Mostly skimmed, but LGTM.
Veykril
enabled auto-merge
November 24, 2025 08:34
Veykril
force-pushed
the
push-zpqupukpkrts
branch
2 times, most recently
from
November 24, 2025 08:57
f337eb3 to
dee751d
Compare
Veykril
force-pushed
the
push-zpqupukpkrts
branch
from
November 24, 2025 08:59
dee751d to
9b767f3
Compare
Member
Author
|
This might've introduced a bug according to https://youtrack.jetbrains.com/issue/RUST-19283 |
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
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.