Skip to content

perf(linter/plugins): remove regex from getCommentsBefore + getCommentsAfter#20475

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_
Mar 21, 2026
Merged

perf(linter/plugins): remove regex from getCommentsBefore + getCommentsAfter#20475
graphite-app[bot] merged 1 commit intomainfrom
om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 17, 2026

Previously sourceCode.getCommentsBefore and sourceCode.getCommentsAfter found all comments directly before / directly after a node by finding comments before / after the node and then checking if anything other than whitespace between them by using a regex search on slices of source text.

Replace this with a more performant algorithm. Just find consecutive runs of comments before/after the node in the tokens-and-comments buffer. When a token is found, that's the end of the run of comments. No need for regexes and text searches.

This does require generating the tokens-and-comments buffer, which is not so cheap. But it's likely that if a rule calls one of these methods on one node, it'll also call it on many others, so the savings likely outweigh the cost.

Copy link
Member Author

overlookmotel commented Mar 17, 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.

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

This PR optimizes comment-adjacency queries by reusing the merged tokens+comments buffer, avoiding per-call source-text slicing/whitespace regex checks and enabling a bounds-check-free forward scan via a sentinel entry.

Changes:

  • Exported merged-buffer layout constants from tokens_and_comments.ts for reuse by other plugins.
  • Grew the merged buffer by one entry and wrote a MERGED_TYPE_TOKEN sentinel after the last valid entry.
  • Reimplemented getCommentsBefore / getCommentsAfter to binary-search the merged buffer and then walk over consecutive comments using pos32 arithmetic.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
apps/oxlint/src-js/plugins/tokens_and_comments.ts Exports merged-buffer constants and writes a sentinel entry after the merged data to support efficient scans.
apps/oxlint/src-js/plugins/comments_methods.ts Switches before/after comment collection to operate on the merged tokens+comments buffer for improved performance.

@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 20, 2026

Merge activity

  • Mar 20, 10:48 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 20, 10:49 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 21, 12:25 PM UTC: overlookmotel added this pull request to the Graphite merge queue.
  • Mar 21, 12:29 PM UTC: The Graphite merge queue couldn't merge this PR because it had merge conflicts.
  • Mar 21, 12:38 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 21, 12:38 PM UTC: overlookmotel added this pull request to the Graphite merge queue.
  • Mar 21, 12:41 PM UTC: Merged by the Graphite merge queue.

@overlookmotel overlookmotel force-pushed the om/03-14-perf_linter_plugins_lazy_deserialize_tokens_and_comments branch from 77b3b94 to 839f548 Compare March 21, 2026 12:20
@overlookmotel overlookmotel force-pushed the om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_ branch from 759ddcf to 2f9c95b Compare March 21, 2026 12:20
@graphite-app graphite-app bot changed the base branch from om/03-14-perf_linter_plugins_lazy_deserialize_tokens_and_comments to graphite-base/20475 March 21, 2026 12:24
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 21, 2026
@graphite-app graphite-app bot changed the base branch from graphite-base/20475 to main March 21, 2026 12:28
@github-actions github-actions bot added A-parser Area - Parser A-ast-tools Area - AST tools labels Mar 21, 2026
@overlookmotel overlookmotel force-pushed the om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_ branch from 2f9c95b to 1ff472d Compare March 21, 2026 12:32
@overlookmotel overlookmotel added 0-merge Merge with Graphite Merge Queue labels Mar 21, 2026
…mentsAfter` (#20475)

Previously `sourceCode.getCommentsBefore` and `sourceCode.getCommentsAfter` found all comments directly before / directly after a node by finding comments before / after the node and then checking if anything other than whitespace between them by using a regex search on slices of source text.

Replace this with a more performant algorithm. Just find consecutive runs of comments before/after the node in the tokens-and-comments buffer. When a token is found, that's the end of the run of comments. No need for regexes and text searches.

This does require generating the tokens-and-comments buffer, which is not so cheap. But it's likely that if a rule calls one of these methods on one node, it'll also call it on many others, so the savings likely outweigh the cost.
@graphite-app graphite-app bot force-pushed the om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_ branch from 1ff472d to 4a22f60 Compare March 21, 2026 12:38
@graphite-app graphite-app bot merged commit 4a22f60 into main Mar 21, 2026
23 checks passed
@graphite-app graphite-app bot deleted the om/03-16-perf_linter_plugins_remove_regex_from_getcommentsbefore_getcommentsafter_ branch March 21, 2026 12:41
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast-tools Area - AST tools A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins A-parser Area - Parser C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants