refactor(formatter): ArrowChain avoid double-references#18300
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
|
@Dunqing Please see my last comment in the PR description
If my assumption is wrong, then this PR is wrong, and we should close it. |
There was a problem hiding this comment.
Pull request overview
This PR refactors the ArrowChain struct's arrows() method to return single references (&AstNode) instead of double references (&&AstNode), improving code clarity and potentially performance.
Changes:
- Modified
arrows()method to return&'b AstNodeinstead of&&'b AstNode - Updated method implementation to use
.copied()on the middle iterator and removed unnecessary reference operators - Adjusted the
fmt()method to destructure with*selfand removed a dereference operator
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Merge activity
|
Refactor. `ArrowChain::arrows` was returning an iterator of `&&AstNode<ArrowFunctionExpression>>`s (double references). Instead make it return an iterator of `&AstNode<ArrowFunctionExpression>>`s. I came across this while replacing usage of `std::ptr::eq` in linter (#18249), and initially thought that the `ptr::eq` call here was buggy, because it was comparing double-references (`&&AstNode` not `&AstNode`): https://github.com/oxc-project/oxc/blob/f79e770a82aee2dc3e64f7935fd7169bc1991ff9/crates/oxc_formatter/src/print/arrow_function_expression.rs#L545-L549 I was wrong about that - it's fine to compare `&&AstNode`s in this case because they're all stable references. But still IMO it's preferable not to be using double-references where it's avoidable. It's (to me, at least) confusing, and can be less performant due to an extra pointer-fetch on each access, if compiler doesn't manage to optimize that out. This PR does subtly change behavior of the `ptr::eq` check. It will now pass if an earlier `AstNode` and the last `AstNode` are the same node (e.g. `head == tail`). But I assume that's impossible here.
f79e770 to
cac2909
Compare
|
I stopped Graphite from merging. I think this one needs @Dunqing's input before merging (#18300 (comment)). I don't know the formatter at all, so I don't want to screw it up! |
Dunqing
left a comment
There was a problem hiding this comment.
As you said, it is impossible, as ArrowChain must have two arrow functions, so head and tail are different.
Refactor. `ArrowChain::arrows` was returning an iterator of `&&AstNode<ArrowFunctionExpression>>`s (double references). Instead make it return an iterator of `&AstNode<ArrowFunctionExpression>>`s. I came across this while replacing usage of `std::ptr::eq` in linter (#18249), and initially thought that the `ptr::eq` call here was buggy, because it was comparing double-references (`&&AstNode` not `&AstNode`): https://github.com/oxc-project/oxc/blob/f79e770a82aee2dc3e64f7935fd7169bc1991ff9/crates/oxc_formatter/src/print/arrow_function_expression.rs#L545-L549 I was wrong about that - it's fine to compare `&&AstNode`s in this case because they're all stable references. But still IMO it's preferable not to be using double-references where it's avoidable. It's (to me, at least) confusing, and can be less performant due to an extra pointer-fetch on each access, if compiler doesn't manage to optimize that out. This PR does subtly change behavior of the `ptr::eq` check. It will now pass if an earlier `AstNode` and the last `AstNode` are the same node (e.g. `head == tail`). But I assume that's impossible here.
cac2909 to
07bdea7
Compare

Refactor.
ArrowChain::arrowswas returning an iterator of&&AstNode<ArrowFunctionExpression>>s (double references). Instead make it return an iterator of&AstNode<ArrowFunctionExpression>>s.I came across this while replacing usage of
std::ptr::eqin linter (#18249), and initially thought that theptr::eqcall here was buggy, because it was comparing double-references (&&AstNodenot&AstNode):oxc/crates/oxc_formatter/src/print/arrow_function_expression.rs
Lines 545 to 549 in f79e770
I was wrong about that - it's fine to compare
&&AstNodes in this case because they're all stable references. But still IMO it's preferable not to be using double-references where it's avoidable. It's (to me, at least) confusing, and can be less performant due to an extra pointer-fetch on each access, if compiler doesn't manage to optimize that out.This PR does subtly change behavior of the
ptr::eqcheck. It will now pass if an earlierAstNodeand the lastAstNodeare the same node (e.g.head == tail). But I assume that's impossible here.