Skip to content

perf(lint): scan CSS url references in linear time - #7829

Merged
michaelwang11394 merged 2 commits into
mainfrom
fix-lint-css-timing-test
Nov 15, 2026
Merged

michaelwang11394 merged 2 commits into
mainfrom
fix-lint-css-timing-test

Conversation

@michaelwang11394

@michaelwang11394 michaelwang11394 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

The lint rule that collects CSS url(...) references now scans in linear time. The test for long runs of unclosed url( now uses an input that a quadratic scan cannot finish in time on any runner, instead of a small input against a tight two-second limit.

Why

extractCssUrlReferences in packages/lint/src/rules/composition.ts used the regex /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g. On a stylesheet with many unclosed url(, each match attempt reads to the end of the file before it fails. Total time then grows with the square of the input. Locally, 8,000 copies of mask-image:url( took about 2 seconds, and 30,000 took about 17.5 seconds.

The test added in #7809 used 3,000 copies and a two-second limit. That input took about 0.15 seconds locally, but 2.4 to 3.3 seconds on shared CI runners. So it failed at random in "Test (support)", and it could not tell a slow runner from a quadratic scan.

Related work

Refs #7809, which added the linear CSS scanners in @hyperframes/parsers/css-url-scans and the original test.

How

  • extractCssUrlReferences uses rawCssUrlMatches from @hyperframes/parsers/css-url-scans. That scanner matches the same pattern as the old regex, in linear time. Comments are still stripped first, and the trimmed URL text is the same.
  • The test lints 30,000 unclosed mask-image:url( and the existing 40,000-deep nested url( with a four-second cap. The linear scan takes about 0.1 seconds locally, which leaves a wide margin for slow runners; the quadratic scan takes about 17.5 seconds.
  • An earlier version of this PR compared the time of two input sizes. That ratio was noisy at tens of milliseconds, and the flake gate failed it once in ten runs, so the PR now uses the single large input instead.

Test plan

  • Unit tests added/updated

  • Manual testing performed

  • Documentation updated (if applicable)

  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

  • bun run --cwd packages/lint test: 28 files and 1,320 tests pass. tsc --noEmit for lint, oxfmt --check, oxlint and the comment ratchet pass.

  • With the old composition.ts, the new test fails after 17.5 seconds. With the fix, it passes in about 0.1 seconds.

  • Lint time for a stylesheet with 8,000 unclosed mask-image:url( went from 2,026 ms to 8 ms.

@michaelwang11394 michaelwang11394 changed the title perf(lint): scan CSS url references in linear time; the test checks growth, not wall time perf(lint): scan CSS url references in linear time Nov 15, 2026

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

APPROVE. Reviewed at a060dd09fe6b86bd64a976e6865775f4fa5cd2f4. The scanner finds the same URLs as the old regex, lint time no longer grows with the square of the input, and the new test fails without the fix.

Need: yes, an unclosed url( run made lint quadratic, and the old fixed-limit test flaked on shared runners · Size: ok (+10/-9 in 2 files) · Reuse: uses rawCssUrlMatches from @hyperframes/parsers/css-url-scans, whose suite already checks it against this exact regex · Simpler: fine · Structure: fine
Risk: low — a lint-only change that returns the same URLs

What I checked

  1. Same matches as the old regex.
    • I ran /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g with the old trim-and-skip-empty loop next to the new extractCssUrlReferences body on 600,000 random strings built from url(, URL(, xurl(, quotes, parens, backslashes, whitespace (including NBSP and U+2028), image( and similar pieces. There were no differences.
    • Hand-picked cases also agree:
      • Quoted URLs with inner spaces.
      • Mismatched quotes (url("a')): no match from either.
      • A quote inside an unquoted URL (url(a"b)): no match from either.
      • Whitespace-only url( ): dropped by both.
      • An escaped paren (url(a\)b) → a\).
      • Nested parens (url(image(x)) → image(x).
      • burl(a): no match. url(a)url(b): both URLs.
    • Comment stripping still runs first, and both callers (style attributes and <style>/linked stylesheets) receive the same list.
  2. Linear time. These numbers are for the test's two stylesheets (40,000 nested url( plus 30,000 unclosed mask-image:url(), five runs each on a Linux devbox:
    • At head: 162 ms on the first run, then 104 to 107 ms.
    • With composition.ts restored to the merge base (6a7b4df7): 25.8 s every run. The new test fails (expected 25782 to be less than 4000).
  3. Room under the 4 s cap.
    • The flake gate's failing run of the earlier ratio test took 1,431 ms. The same six lints take about 330 ms on my box, so that runner was about 4 times slower.
    • At that speed this test takes about 0.65 s. That leaves about 6 times headroom under 4 s, and the unfixed code is still far over the cap.
    • The flake gate ran the new test 10 times at this head, and all 10 passed.
    • The ratio test at b1956e1d failed 1 of 10 there (8.96 against 8).
    • Part of the reason is garbage collection. A CPU profile of lint on the nested stylesheet spends most of its time collecting garbage, and that time grows faster than the input. Locally, 4 times the input took 5.0 to 5.3 times as long, never the "about four" the first comment assumed. A single run against a cap avoids that, so the switch was right.
  4. Other regexes in this file.
    • On their own, three patterns take about four times as long when the input doubles:
      • HEAVY_OVERLAY_CSS_PATTERN on repeated filter:.
      • COMPUTED_TIMELINE_REGISTRATION_PATTERN on repeated window.__timelines[a.
      • The infinite animation check on repeated animation:x .
    • Through lintProject I couldn't make any of them grow quadratically. Stylesheet and <style> inputs aimed at each stayed around 100 ms up to 40,000 repeats. The script input grew about 2 to 2.6 times per doubling (0.64 s, 1.28 s, then 3.3 s at 80,000).
    • I'm not asking for changes in this PR. If someone later converts them to the css-url-scans style, those are the three to look at.

CI

  • At this head the flake gate passes (10 of 10 runs of project.test.ts).
  • Test (support) and the remaining jobs were still running when I posted.
  • Locally, the lint suite passes 1,320 of 1,320 tests. The CI setup builds parsers and lint first, and I did the same. Without that build, 26 files fail to resolve @hyperframes/parsers/*.

— Rames

@github-actions

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 111 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

@michaelwang11394
michaelwang11394 added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit bcf3d17 Nov 15, 2026
86 of 87 checks passed
@michaelwang11394
michaelwang11394 deleted the fix-lint-css-timing-test branch November 15, 2026 10:37
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.

2 participants