Skip to content

Improve performance when scanning long lines with many [ characters - #20694

Merged
RobinMalfait merged 2 commits into
mainfrom
robin/open-pr-quadratic-scanner-time
Nov 10, 2026
Merged

RobinMalfait merged 2 commits into
mainfrom
robin/open-pr-quadratic-scanner-time

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

Summary

The Oxide extractor took quadratic time on some long lines. Minified files and generated content often have long lines, so this can slow down the scan of a real project. For example, on main, a line of 80 KB with only [ characters takes 3.2 s.

There are two causes:

  1. When the main candidate machine consumes a range, the extractor starts a new candidate machine after each [ in that range, to find the candidates inside the brackets (e.g. [ClassPrefix('gap-y-4')]). Each machine runs until the end of the range. Thus a long line with many [ takes quadratic time.
  2. On a < or an uppercase letter, the candidate machine searches the rest of the line for a boundary character. If the line has none, the machine restarts at the next character, and searches the same bytes again.

This PR fixes both, and the extracted candidates do not change:

  1. The candidate machine now reports each position where it starts from a clean state (no variant in progress). From such a position, the machine finds the same candidates, no matter where it started. Thus when a machine reaches a position where an earlier machine of the same range was in a clean state, it stops: the earlier machine already found the candidates from there.
  2. The candidate machine remembers the position from which the line has no boundary, and a later search stops there.

Times for one line of 80 KB, with an optimized build:

Input main This PR
[ repeated 3.24 s 3.5 ms
a-[ repeated 1.45 s 3.9 ms
"a-[' repeated 576 ms 6.5 ms
a-[a]-[a]… 2.14 s 0.8 ms
<%= a-[ repeated 877 ms 0.5 ms

Test plan

  • Added test_long_lines_without_candidates. It checks that long lines of these shapes give no candidates, and that each line takes less than 5 s. On main, the first line alone takes 89 s in a debug build. With this PR, the full test takes about 0.5 s.
  • Added test_overlapping_candidates_in_long_lines. It checks the candidates in nested arrays on a long line (e.g. minified JSON).
  • I compared the extracted candidates (spans and order) of this PR with those of main. They are the same for:
    • 82.8M lines from 310K real files (4.7 GB). Most of the files come from node_modules, with many minified bundles and source maps. On the same lines, the total time went from 23.1 s to 20.2 s.
    • More than 10M random inputs, made of tokens like [, ], \, quotes, hover:, bg-[ and [&.
    • 20K random short patterns, each repeated to 10 KB.
  • cargo test in crates/oxide, pnpm run build, and vitest pass.

Sent by an AI assistant on behalf of @RobinMalfait.

@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 10, 2026 14:03
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Reviews (2) · Last reviewed commit: "Share work between the machines that ext..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a7993855-c82b-4b8a-82cd-28b6e278fbdd
Commits

Reviewing files that changed from the base of the PR and between d057c05 and db94c8a.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

CandidateMachine now caches positions where boundary searches found no valid boundary and provides a checkpoint-aware scanning method. SubCandidateExtractor reuses CandidateMachine instances and records clean-state positions to stop scans when another machine has already reached the same position. The extractor uses this helper for completed outer candidates and idle scans. Tests cover repeated nested candidates and long candidate-free inputs. A changelog entry describes the long-line scanning fix.

Merge Risk: Minimal · previous review d057c

This change speeds up scanning of long lines with many bracket characters and is reported to extract the same candidates as before. No actionable merge-blocking risk remains after review.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly summarizes the main change: improving performance when scanning long lines with many [ characters.
Description check Passed The description explains the extractor’s quadratic-time behavior, the two optimizations, and the tests for this change.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1bbb0e58-3ba1-468a-a56a-345c6cd6cc0c
Commits

Reviewing files that changed from the base of the PR and between 3603b3a and d057c05.

Files selected for processing: 3

  • .changelog/scanner-long-lines.md
  • crates/oxide/src/extractor/candidate_machine.rs
  • crates/oxide/src/extractor/mod.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

On a `<` or an uppercase letter, the candidate machine searches the
rest of the line for a valid before boundary character. If it finds
none, it restarts at the next character, and searches the same bytes
again. Thus a long line of these characters without a boundary (e.g.
`AAAA…` or `<<<<…`) takes quadratic time.

The machine now remembers the position from which the line has no
boundary, and a later search stops there. This position only depends
on the input, so the machine keeps it when it resets.
When the main candidate machine consumes a range, the extractor starts
a new candidate machine after each `[` in that range, to find the
candidates inside the brackets. Each machine runs until the end of the
range. Thus a long line with many `[` (e.g. `[[[[…`, `a-[a-[…` or
minified JSON) takes quadratic time.

The candidate machine can now report each position where it starts
from a clean state (no variant in progress). From such a position, the
machine finds the same candidates, no matter where it started. Thus
when a machine reaches a position where an earlier machine of the same
range was in a clean state, it stops: the earlier machine already
found the candidates from there.

The machines of a line also share one `CandidateMachine`, so that they
find the position from which the line has no boundary only once.

The extracted candidates do not change.
@RobinMalfait
RobinMalfait force-pushed the robin/open-pr-quadratic-scanner-time branch from d057c05 to db94c8a Compare November 10, 2026 14:23
@RobinMalfait
RobinMalfait merged commit 0583144 into main Nov 10, 2026
13 checks passed
@RobinMalfait
RobinMalfait deleted the robin/open-pr-quadratic-scanner-time branch November 10, 2026 14:30
RobinMalfait added a commit that referenced this pull request Nov 16, 2026
… act on (#20728)

This PR keeps `### Changed` in the changelog for changes that users may
have to act on, and documents which section to use.

`[Unreleased]` had a `### Changed` section with two performance entries
(#20680 and
#20688). A performance
improvement is not a change that users have to act on, and the repo
already lists them under `### Fixed` (e.g.
#20694,
#20678 and
#20663).

## Changes

- `CHANGELOG.md`: remove the `### Changed` section of `[Unreleased]`.
Combine the two entries into one entry in `### Fixed`, because
#20688 only extends
#20680 to the other
integrations. The entry goes in the merge order of
#20680.
- `CHANGELOG.md`: move the canonicalization entry of
#20697 from `### Added`
to `### Fixed`, in merge order, and drop the empty `### Added` heading.
A canonicalization improvement is not a new feature for users, and the
canonicalization entries of 4.3.4 and 4.3.5 are in `### Fixed` too.
- `.github/CONTRIBUTING.md`: add a guide on which section to pick. The
examples for `### Changed` come from earlier releases: changes to the
CSS of existing utilities, renamed or replaced utilities (also when the
upgrade tool migrates them), theme defaults, APIs, stricter parsing,
source detection defaults, and supported versions.
- `.changelog/README.md`: link to that guide.

This PR edits `CHANGELOG.md` directly instead of adding a fragment,
because it fixes entries that the "Compile changelog" workflow already
compiled. Released sections stay as they are. The PR changes only docs,
so it needs no changelog entry of its own.

## Test plan

- `pnpm changelog check` passes.
- `pnpm --silent changelog` shows `[Unreleased]` with only `### Fixed`.
- `pnpm prettier --check` passes for the three files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant