refactor(linter/plugins): add shared binary search function for tokens methods#20310
Merged
graphite-app[bot] merged 1 commit intomainfrom Mar 13, 2026
Conversation
Member
Author
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. |
This was referenced Mar 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors SourceCode token helper methods to reuse a single shared binary search implementation, reducing duplicated logic across the linter plugin token APIs and paving the way for future token-buffer optimizations.
Changes:
- Replaces repeated inline binary-search loops in token methods with a shared helper.
- Adds
firstTokenAtOrAfter()to centralize “lower_bound by start offset” behavior.
Contributor
Merge activity
|
…s methods (#20310) All tokens methods contain binary searches through `tokens` array. The code is repeated inline in every method to avoid the cost of function calls, but the downside is that it makes the code verbose and hard to understand. Break out the binary search logic into a function which is used in every method. This is a perf regression due to the extra cost of function calls, but that is fixed in #20312 by a TSDown plugin which inlines `firstTokenAtOrAfter` into all call sites, producing code in final build that is almost identical to before this PR. This change makes the code shorter and easier to understand, but main motivation is to enable switching in a future PR to performing the binary searches reading directly from the buffer, without touching token objects. This means that tokens can be deserialized lazily, instead of having to deserialize the entire tokens array eagerly as we do now. This will be a large perf gain, but would be impractically complex if the convoluted buffer-search logic had to be repeated in 20+ places.
32b0578 to
405e741
Compare
graphite-app bot
pushed a commit
that referenced
this pull request
Mar 13, 2026
…20312) #20310 and #20311 reduce code size and complexity by moving binary search routine which is used in all tokens and comments methods into a shared function `firstTokenAtOrAfter`. However, as noted in #20310, the downside of that change is a perf hit due to the cost of function calls. This PR fixes that regression by adding a TSDown plugin to the build which inlines the body of `firstTokenAtOrAfter` into all the call sites in tokens and comments methods. The net result is that the final code in bundled output is almost identical to before #20310, but the source code is much shorter and easier to understand. This unlocks a large optimization which will follow in a future PR (see #20310 for more details).
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.

All tokens methods contain binary searches through
tokensarray. The code is repeated inline in every method to avoid the cost of function calls, but the downside is that it makes the code verbose and hard to understand.Break out the binary search logic into a function which is used in every method.
This is a perf regression due to the extra cost of function calls, but that is fixed in #20312 by a TSDown plugin which inlines
firstTokenAtOrAfterinto all call sites, producing code in final build that is almost identical to before this PR.This change makes the code shorter and easier to understand, but main motivation is to enable switching in a future PR to performing the binary searches reading directly from the buffer, without touching token objects. This means that tokens can be deserialized lazily, instead of having to deserialize the entire tokens array eagerly as we do now. This will be a large perf gain, but would be impractically complex if the convoluted buffer-search logic had to be repeated in 20+ places.