From b1956e1d83455ef57da5a1ffe8eb1ff787d191cb Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sun, 15 Nov 2026 01:49:49 -0800 Subject: [PATCH 1/2] perf(lint): scan CSS url references in linear time; the test checks growth, not wall time --- packages/lint/src/project.test.ts | 39 +++++++++++++++++--------- packages/lint/src/rules/composition.ts | 8 ++---- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/lint/src/project.test.ts b/packages/lint/src/project.test.ts index 7b45a7f219f..26b4341e38c 100644 --- a/packages/lint/src/project.test.ts +++ b/packages/lint/src/project.test.ts @@ -549,21 +549,34 @@ describe("texture_mask_asset_not_found declarations", () => { }, ); - it("reads long runs of unclosed urls in valid and malformed stylesheets in well under two seconds", async () => { - const links = - ''; - const project = makeProject(validHtml().replace("", `${links}`)); - writeFileSync( - join(project, "nested.css"), - `a{background:${"url(".repeat(40_000)}${")".repeat(40_000)}}`, - ); - writeFileSync(join(project, "broken.css"), `a{${"mask-image:url(".repeat(3_000)}`); + it("reads long runs of unclosed urls in valid and malformed stylesheets in linear time", async () => { + // Shared CI runners are often several times slower than a laptop, so a fixed time limit + // fails at random. Four times the input takes about four times as long when the scan is + // linear, and about sixteen times as long when it is quadratic. + const fastestLint = async (runs: number): Promise => { + const links = + ''; + const project = makeProject(validHtml().replace("", `${links}`)); + writeFileSync( + join(project, "nested.css"), + `a{background:${"url(".repeat(runs)}${")".repeat(runs)}}`, + ); + writeFileSync(join(project, "broken.css"), `a{${"mask-image:url(".repeat(runs / 10)}`); + let fastest = Number.POSITIVE_INFINITY; + for (let attempt = 0; attempt < 3; attempt++) { + const started = performance.now(); + await lintProject(project); + fastest = Math.min(fastest, performance.now() - started); + } + return fastest; + }; - const started = performance.now(); - await lintProject(project); + const small = await fastestLint(20_000); + const large = await fastestLint(80_000); - expect(performance.now() - started).toBeLessThan(2000); - }); + expect(large / small).toBeLessThan(8); + expect(large).toBeLessThan(10_000); + }, 60_000); }); describe("template shell style sources", () => { diff --git a/packages/lint/src/rules/composition.ts b/packages/lint/src/rules/composition.ts index 96118e1d831..cffb014d332 100644 --- a/packages/lint/src/rules/composition.ts +++ b/packages/lint/src/rules/composition.ts @@ -22,6 +22,7 @@ import { isSafeMediaUrl, } from "@hyperframes/parsers/composition"; import { COMPOSITION_ATTRIBUTES, readClipTiming } from "@hyperframes/parsers/composition-contract"; +import { rawCssUrlMatches } from "@hyperframes/parsers/css-url-scans"; import { resolveCompositionDuration } from "@hyperframes/parsers/composition-duration"; import { readAuthoredDurationSeconds, @@ -132,11 +133,8 @@ function isCompositionRootOrMount(rawTag: string): boolean { // rules that consume this — the helper just yields raw URL values. function extractCssUrlReferences(css: string): string[] { const out: string[] = []; - const noComments = replaceBlockComments(css, ""); - const urlPattern = /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g; - let m: RegExpExecArray | null; - while ((m = urlPattern.exec(noComments)) !== null) { - const raw = (m[2] ?? "").trim(); + for (const { parts } of rawCssUrlMatches(replaceBlockComments(css, ""))) { + const raw = (parts[2] ?? "").trim(); if (raw) out.push(raw); } return out; From a060dd09fe6b86bd64a976e6865775f4fa5cd2f4 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sun, 15 Nov 2026 01:56:19 -0800 Subject: [PATCH 2/2] test(lint): cap one large malformed stylesheet instead of comparing two small timings --- packages/lint/src/project.test.ts | 38 ++++++++++++------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/lint/src/project.test.ts b/packages/lint/src/project.test.ts index 26b4341e38c..ed36fa5586c 100644 --- a/packages/lint/src/project.test.ts +++ b/packages/lint/src/project.test.ts @@ -550,32 +550,22 @@ describe("texture_mask_asset_not_found declarations", () => { ); it("reads long runs of unclosed urls in valid and malformed stylesheets in linear time", async () => { - // Shared CI runners are often several times slower than a laptop, so a fixed time limit - // fails at random. Four times the input takes about four times as long when the scan is - // linear, and about sixteen times as long when it is quadratic. - const fastestLint = async (runs: number): Promise => { - const links = - ''; - const project = makeProject(validHtml().replace("", `${links}`)); - writeFileSync( - join(project, "nested.css"), - `a{background:${"url(".repeat(runs)}${")".repeat(runs)}}`, - ); - writeFileSync(join(project, "broken.css"), `a{${"mask-image:url(".repeat(runs / 10)}`); - let fastest = Number.POSITIVE_INFINITY; - for (let attempt = 0; attempt < 3; attempt++) { - const started = performance.now(); - await lintProject(project); - fastest = Math.min(fastest, performance.now() - started); - } - return fastest; - }; + // A linear scan lints these stylesheets in about 0.1 s on a laptop. A quadratic one takes + // over 10 s, because it reads to the end of broken.css after each unclosed url(. The cap + // leaves room for slow shared CI runners. + const links = + ''; + const project = makeProject(validHtml().replace("", `${links}`)); + writeFileSync( + join(project, "nested.css"), + `a{background:${"url(".repeat(40_000)}${")".repeat(40_000)}}`, + ); + writeFileSync(join(project, "broken.css"), `a{${"mask-image:url(".repeat(30_000)}`); - const small = await fastestLint(20_000); - const large = await fastestLint(80_000); + const started = performance.now(); + await lintProject(project); - expect(large / small).toBeLessThan(8); - expect(large).toBeLessThan(10_000); + expect(performance.now() - started).toBeLessThan(4000); }, 60_000); });