Skip to content

refactor(linter/plugins): add shared binary search function for tokens methods#20310

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/03-12-refactor_linter_plugins_add_shared_binary_search_function_for_tokens_methods
Mar 13, 2026
Merged

refactor(linter/plugins): add shared binary search function for tokens methods#20310
graphite-app[bot] merged 1 commit intomainfrom
om/03-12-refactor_linter_plugins_add_shared_binary_search_function_for_tokens_methods

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 13, 2026

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.

Copy link
Member Author

overlookmotel commented Mar 13, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of 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.

@github-actions github-actions bot added A-linter Area - Linter A-cli Area - CLI A-linter-plugins Area - Linter JS plugins labels Mar 13, 2026
@github-actions github-actions bot added the C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior label Mar 13, 2026
@overlookmotel overlookmotel marked this pull request as ready for review March 13, 2026 00:11
@overlookmotel overlookmotel requested a review from camc314 as a code owner March 13, 2026 00:11
Copilot AI review requested due to automatic review settings March 13, 2026 00:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@overlookmotel overlookmotel self-assigned this Mar 13, 2026
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 13, 2026
@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 13, 2026

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.
@graphite-app graphite-app bot force-pushed the om/03-12-refactor_linter_plugins_add_shared_binary_search_function_for_tokens_methods branch from 32b0578 to 405e741 Compare March 13, 2026 00:25
graphite-app bot pushed a commit that referenced this pull request Mar 13, 2026
…ments methods (#20311)

Continuation of #20310. Use the same binary search function `firstTokenAtOrAfter` (introduced in that PR) for comments methods.
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).
@graphite-app graphite-app bot merged commit 405e741 into main Mar 13, 2026
19 checks passed
@graphite-app graphite-app bot deleted the om/03-12-refactor_linter_plugins_add_shared_binary_search_function_for_tokens_methods branch March 13, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants