-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add a MutVisitor for the AST #57662
Add a MutVisitor for the AST #57662
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@Zoxc From what I understand the difference is mostly "noop visiting" vs "noop folding", and both should be pretty cheap. This can probably be measured by a targeted benchmark - "noop fold the crate N times" vs "noop visit the crate N times". |
src/libsyntax/visit_mut.rs
Outdated
} | ||
|
||
if let Some(replacement) = $visitor.$method(&mut $list[i], $(, $extra_args)*) { | ||
$list.remove(i); |
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.
The case replacement.len() == 1
can be optimized to assignment.
src/libsyntax/visit_mut.rs
Outdated
$list.remove(i); | ||
let rlen = replacement.len(); | ||
for (j, r) in replacement.into_iter().enumerate() { | ||
$list.insert(i + j, r); |
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.
Also, don't we have a range insert, like list.insert(replacement.into_iter())
or something?
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.
Hmm, looks like we don't.
Vec::splice
looks very similar though.
@petrochenkov This does give a ~3x speedup to |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
r? @petrochenkov -- I trust them to handle this one =) |
@Zoxc However, I'm not sure folding interpolated tokens is even necessary now (at least visitors ignore them entirely, but I don't remember this causing issues), but I haven't checked in more details yet. |
I don't see why you couldn't do that if that is desirable, there's no "AST lifetime" for this visitor. |
Ah, I see, Do you plan to convert more folders before merging? |
I'm going to apply this to |
@bors try |
☀️ Test successful - checks-travis |
@rust-timer build a0eec98 |
Success: Queued a0eec98 with parent 147311c, comparison URL. |
Finished benchmarking try commit a0eec98 |
Perf summary: mostly noise, |
@Zoxc Could you also make the synthetic benchmark "run noop fold the crate N times" vs "run noop mut visit the crate N times" and tell the numbers? Beside that, converting other folders may require making some extensions to |
cc @rust-lang/compiler This brings some modest performance improvements for the total time (and not so modest for some specific passes), but the code may be a bit uglier perhaps (or perhaps not, when all folders are converted). I'll r+ in a few days if there are no concerns. |
An alternative PR appeared - #58061. |
IMO we should stop folding/mutating ASTs completely and have visitors and lowering to HIR use separate replacement maps. This way we can make progress towards incremental macro expansion. |
I'm not sure whether you approve what this PR does or have some other kind of visitors in mind here...
... and it's also not clear to me what the "replacement map" is. |
I meant immutable visitors, to be clear. In other worse, make the AST more like a persistent data structure. As for the "replacement map" - it already exists, for macro expansion (mapping some "placeholder" IDs to AST fragments). |
Ok, |
Blocked on #58061. |
@Zoxc: I apologize for the overlap with #58061. I'm sorry to have caused you wasted time. I am contemplating the same change for |
@Zoxc |
@Zoxc: I looked into this a bit. I don't think it will be able to benefit from the same change, due mostly to the heavy use of interning. E.g. consider this function, which is one of the hottest folders: Lines 327 to 337 in e544947
It's folding a |
We should be moving more to interning and persistence, even of AST fragments, because of the benefits it has for being able to do anything incremental. That's why I think |
No description provided.