Repository navigation
perf(lint): scan CSS url references in linear time - #7829
Merged
Merged
Conversation
…rowth, not wall time
jrusso1020
approved these changes
Nov 15, 2026
jrusso1020
left a comment
Collaborator
There was a problem hiding this comment.
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
- Same matches as the old regex.
- I ran
/\burl\(\s*(["']?)([^)"']+)\1\s*\)/gwith the old trim-and-skip-empty loop next to the newextractCssUrlReferencesbody on 600,000 random strings built fromurl(,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 (
styleattributes and<style>/linked stylesheets) receive the same list.
- I ran
- Linear time. These numbers are for the test's two stylesheets (40,000 nested
url(plus 30,000 unclosedmask-image:url(), five runs each on a Linux devbox:- At head: 162 ms on the first run, then 104 to 107 ms.
- With
composition.tsrestored to the merge base (6a7b4df7): 25.8 s every run. The new test fails (expected 25782 to be less than 4000).
- 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
b1956e1dfailed 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.
- Other regexes in this file.
- On their own, three patterns take about four times as long when the input doubles:
HEAVY_OVERLAY_CSS_PATTERNon repeatedfilter:.COMPUTED_TIMELINE_REGISTRATION_PATTERNon repeatedwindow.__timelines[a.- The
infiniteanimation check on repeatedanimation:x.
- Through
lintProjectI 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-scansstyle, those are the three to look at.
- On their own, three patterns take about four times as long when the input doubles:
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
parsersandlintfirst, and I did the same. Without that build, 26 files fail to resolve@hyperframes/parsers/*.
— Rames
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The lint rule that collects CSS
url(...)references now scans in linear time. The test for long runs of unclosedurl(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
extractCssUrlReferencesinpackages/lint/src/rules/composition.tsused the regex/\burl\(\s*(["']?)([^)"']+)\1\s*\)/g. On a stylesheet with many unclosedurl(, 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 ofmask-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-scansand the original test.How
extractCssUrlReferencesusesrawCssUrlMatchesfrom@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.mask-image:url(and the existing 40,000-deep nestedurl(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.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 --noEmitfor lint,oxfmt --check,oxlintand 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.