Skip to content

fix(studio-server): sub-composition head and root tag scans take linear time - #7866

Merged
michaelwang11394 merged 1 commit into
mainfrom
fix-subcomp-head-scan-linear
Nov 15, 2026
Merged

michaelwang11394 merged 1 commit into
mainfrom
fix-subcomp-head-scan-linear

Conversation

@michaelwang11394

Copy link
Copy Markdown
Contributor

What

rootHeadContent and tagRootCompositionFile in 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>/i on the project's index.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:

page old regex
25,000 × <head (125 KB) 2.4 s
50,000 × <head (250 KB) 10.1 s
50,000 × <a (100 KB) 9.6 s

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 <head opener 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 for data-composition-id= with a non-word character before it, which is what \b requires, and the scan moves on to the next >.
  • tagRootCompositionFile is exported so the tests can compare it with the regex directly.

Test plan

  • Unit tests added
    • Equivalence: each function is run on 20,000 generated pages and compared with the regex it replaces. Of those pages, 5,022 have a head and 3,570 get tagged.
    • Timing: pages of 100,000 to 150,000 repeated openers (up to 600 KB) must finish within 100 ms each. The old regexes take minutes on them.
    • Ordinary pages: an attribute on <head>, upper-case tags, a root that is already tagged, and xdata-composition-id.
  • vitest run src/helpers in studio-server: 781 passed. tsc --noEmit is clean. oxlint, oxfmt, fallow and comment-ratchet pass.
  • Manual testing performed. Not needed, since the output is the same for every input.
  • Comments say what each scan must match and why the regex was slow.

…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 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.

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

@github-actions

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 93 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 ceb969d Nov 15, 2026
82 checks passed
@michaelwang11394
michaelwang11394 deleted the fix-subcomp-head-scan-linear branch November 15, 2026 21:44
timothybrush pushed a commit to timothybrush/hyperframes that referenced this pull request Nov 15, 2026
* 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
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