Skip to content

perf(linter/plugins): remove bounds checks on regex tokens#20478

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

perf(linter/plugins): remove bounds checks on regex tokens#20478
graphite-app[bot] merged 1 commit intomainfrom
om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 17, 2026

Possibly the most absurd optimization I've ever encountered.

- if (regexObjects.length > regexIndex) {
+ if (regexIndex < regexObjects.length) {
    regex = regexObjects[regexIndex];
  }

There is no semantic difference whatsoever between regexObjects.length > regexIndex and regexIndex < regexObjects.length. But V8's 2nd-tier Maglev compiler treats them differently. In the> version, regexObjects[regexIndex] incurs a bounds check, whereas in the new < version, no bounds check.

This is a micro-optimization, but this function is extremely hot, so it may actually move the needle in code that includes a lot of regexes. The same optimization is also applied in #20480.

Claude found this oddity by reading V8's source code!

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.

@github-actions github-actions bot added the C-performance Category - Solution not expected to change functional behavior, only performance label Mar 17, 2026
@overlookmotel overlookmotel self-assigned this Mar 18, 2026
@overlookmotel overlookmotel force-pushed the om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments branch from 0eb994b to 7b220af Compare March 18, 2026 09:45
@overlookmotel overlookmotel force-pushed the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch from 66d4271 to 208bee0 Compare March 18, 2026 09:45
@overlookmotel overlookmotel changed the base branch from om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments to graphite-base/20478 March 18, 2026 16:27
@overlookmotel overlookmotel force-pushed the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch from 208bee0 to 5374681 Compare March 18, 2026 16:28
@overlookmotel overlookmotel changed the base branch from graphite-base/20478 to om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments March 18, 2026 16:28
@overlookmotel overlookmotel marked this pull request as ready for review March 18, 2026 16:50
@overlookmotel overlookmotel requested a review from camc314 as a code owner March 18, 2026 16:50
Copilot AI review requested due to automatic review settings March 18, 2026 16:50
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 applies a micro-optimization in the hot token deserialization path by rewriting a regex-cache bounds comparison into a form that V8/Maglev can use to elide an array bounds check.

Changes:

  • Swaps the regex cache guard from regexObjects.length > regexIndex to regexIndex < regexObjects.length to enable Maglev bounds-check elimination.
  • Updates the surrounding comment to document the V8-specific rationale and constraint.

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 20, 2026
@overlookmotel overlookmotel removed the 0-merge Merge with Graphite Merge Queue label Mar 20, 2026
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Mar 20, 2026 — with Graphite App
@overlookmotel overlookmotel changed the base branch from om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments to graphite-base/20478 March 21, 2026 12:20
@overlookmotel overlookmotel force-pushed the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch from 5374681 to 2090432 Compare March 21, 2026 12:21
@overlookmotel overlookmotel changed the base branch from graphite-base/20478 to om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments March 21, 2026 12:21
@overlookmotel overlookmotel force-pushed the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch from 2090432 to 94eb885 Compare March 21, 2026 12:32
@overlookmotel overlookmotel force-pushed the om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments branch from 1d06d2c to c00f3e6 Compare March 21, 2026 12:32
@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 21, 2026

Merge activity

Possibly the most absurd optimization I've ever encountered.

```diff
- if (regexObjects.length > regexIndex) {
+ if (regexIndex < regexObjects.length) {
    regex = regexObjects[regexIndex];
  }
```

There is no semantic difference whatsoever between `regexObjects.length > regexIndex` and `regexIndex < regexObjects.length`. But V8's 2nd-tier Maglev compiler treats them differently. In the`>` version, `regexObjects[regexIndex]` incurs a bounds check, whereas in the new `<` version, no bounds check.

This is a micro-optimization, but this function is extremely hot, so it may actually move the needle in code that includes a lot of regexes. The same optimization is also applied in #20480.

Claude found this oddity by reading V8's source code!
@graphite-app graphite-app bot force-pushed the om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments branch from c00f3e6 to c3d9e91 Compare March 21, 2026 12:46
@graphite-app graphite-app bot force-pushed the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch from 94eb885 to 4ee80ac Compare March 21, 2026 12:47
Base automatically changed from om/03-17-fix_linter_plugins_fix_memory_leak_in_tokens_and_comments to main March 21, 2026 12:50
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 21, 2026
@graphite-app graphite-app bot merged commit 4ee80ac into main Mar 21, 2026
24 checks passed
@graphite-app graphite-app bot deleted the om/03-17-perf_linter_plugins_remove_bounds_checks_on_regex_tokens branch March 21, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins 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