-
-
Notifications
You must be signed in to change notification settings - Fork 763
perf(linter/approx-constant): break after first match to avoid extra iterations #16848
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
perf(linter/approx-constant): break after first match to avoid extra iterations #16848
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. |
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.
No issues found with this change. The added break; is a clear, safe micro-optimization that also prevents duplicate diagnostics once a match has been handled.
Additional notes (1)
- Maintainability |
crates/oxc_linter/src/rules/oxc/approx_constant.rs:67-71
Addingbreakafter the first diagnostic changes behavior if the loop could legitimately find multiple approximate constants that should each be reported for the samenumber_literal/node (or if the loop is iterating over multiple candidate constants to choose a best match). If multiple reports were previously possible/desired, this will silently reduce diagnostics to one.
Given the PR title, that may be intentional, but the code currently doesn’t make it obvious that the first match is the correct/only match (e.g., if candidate ordering changes, you might report a less-ideal constant). Consider ensuring determinism/quality by explicitly selecting the best match (or documenting that candidates are mutually exclusive / ordered by preference).
Summary of changes
What changed
- In
ApproxConstantrule, added abreak;immediately after emitting a diagnostic viactx.diagnostic_with_suggestion(...). - This stops iterating after the first match is found, reducing unnecessary loop iterations and avoiding multiple diagnostics for the same visit.
Key code change
- Added
break;after the diagnostic call within the loop that searches for an approximate constant match.
Merge activity
|
CodSpeed Performance ReportMerging #16848 will not alter performanceComparing Summary
Footnotes
|
21ffa11 to
e86da34
Compare
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.
Pull request overview
This PR adds a performance optimization to the approx_constant linter rule by breaking out of the loop after finding the first matching constant, avoiding unnecessary iterations through the remaining constants.
- Adds a
breakstatement after the first constant match is found and reported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
e86da34 to
9c1be35
Compare

No description provided.