Repository navigation
fix(studio-server): sub-composition head and root tag scans take linear time - #7866
Conversation
…ar time rootHeadContent and tagRootCompositionFile ran an opener, then [^>]* or a lazy run, then a required literal, over the project's own HTML. When the literal never came, each opener rescanned to the end of the page: about 10 s for 250 KB of "<head" or of "<a". Both are now plain scans that give the same results: only the first <head can match, and each start tag is checked up to its own > once.
jrusso1020
left a comment
There was a problem hiding this comment.
Reviewed at c22cb9882bcc1142ff541a9b79aa951f24e49bf2.
The scans advance across non-overlapping tag segments or perform a fixed number of forward searches. They preserve the existing lexical contract rather than introducing different HTML-parser semantics.
Need: yes · Size: ok · Reuse: existing attribute escaping and sub-composition helpers · Simpler: bounded scans fit the compatibility requirement · Structure: fine (checked existing tag/parser helpers and their different semantics; no split)
Risk: low — focused scan-cost change with output-parity coverage.
Validation: frozen install/full build passed. Gated Vitest runs of subComposition.test.ts plus reviewer controls: 38 tests passed, including a separate 50,000-page differential corpus with case, quoted delimiters, comments, CDATA, self-closing/unclosed tags, prefixes and escaped composition paths. No mismatch found.
Independent 1/2/4 MB probes (median of five runs) were each under 1 ms on this Linux runner. Restoring the old functions makes the reviewer's bounded 100 ms guards fail at approximately 1,357 ms and 850 ms. The PR's larger fixtures ran on the new code; I used smaller inputs for the reverted-code timing check to avoid a prolonged stall. These measurements are runner-specific, not a portable latency guarantee. No browser walkthrough was run.
No direct changed-file overlap with #7857 at 6ccb0f5c9fb55a6ef8c13b74c26d5b7723ec82aa; this improves scan cost independently of moving the build to a worker.
Verdict: APPROVE
Reasoning: The scan structure bounds repeated work in these two functions, and targeted tests plus the differential corpus found no output changes. No blocker found.
— Rames
* chore: release v0.9.55 * chore: release v0.9.55 notes include heygen-com#7830 * chore: release v0.9.55 notes include heygen-com#7859 and heygen-com#7867 * chore: release v0.9.55 notes name the docs change and cloud render * chore: release v0.9.55 notes include heygen-com#7866 and heygen-com#7801 * chore: release v0.9.55 notes match the style bar
What
rootHeadContentandtagRootCompositionFilein studio-server's sub-composition builder now take time linear in the page. Their results are unchanged.Why
Both functions ran a regex over the project's own HTML. Each regex had an opener, then a run up to
>(or a lazy run), then a literal it required:rootHeadContent:/<head[^>]*>([\s\S]*?)<\/head>/ion the project'sindex.html.tagRootCompositionFile:/<[a-zA-Z][^>]*\bdata-composition-id=/on the built body. A script's text keeps its<through serialization, so the body can hold a long run of openers.When the literal never came, the engine retried from every opener and scanned to the end of the page each time. That cost grows with the square of the page size. Measured on Node 25:
<head(125 KB)<head(250 KB)<a(100 KB)The build runs synchronously, so a page like this holds the server's request thread for that whole time.
Related work
Refs #7857, which moves the sub-composition build off the request thread with a deadline. This PR fixes the cost inside the build itself. The two PRs touch different files.
How
rootHeadContent: only the first<headopener can match. A later opener has no>or</head>after it that the first one could not also use. So the function finds the first opener, the first>after it and the first</head>after that. The two literal searches are case-insensitive regexes with no repetition.tagRootCompositionFile: a candidate cannot cross a>, so each start tag runs from its<to its own>. Any opener inside that tag can only do worse than the tag's own opener. So each tag is checked once fordata-composition-id=with a non-word character before it, which is what\brequires, and the scan moves on to the next>.tagRootCompositionFileis exported so the tests can compare it with the regex directly.Test plan
<head>, upper-case tags, a root that is already tagged, andxdata-composition-id.vitest run src/helpersin studio-server: 781 passed.tsc --noEmitis clean. oxlint, oxfmt, fallow and comment-ratchet pass.