Skip to content

fix(core): keep SVG id renaming linear in the page size and within the render size limit - #8147

Merged
miguel-heygen merged 5 commits into
mainfrom
secpub/svg-id-budget
Nov 18, 2026
Merged

miguel-heygen merged 5 commits into
mainfrom
secpub/svg-id-budget

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

Compiling a page whose mounted compositions repeat SVG ids now takes time and memory in line with the page's size, and what it adds counts toward the render's size limit.

  • Before, a page with many mounts and many colliding SVG ids built a full copy of the page's rename table for every mount. 200 mounts over 200 renamed ids wrote 41,200 table entries; now it writes well under 10,000.
  • A stylesheet rewrite rebuilt a table of every renamed id for every selector. 300 rules over 2,000 renamed ids wrote 600,000 entries; now it writes only the ids each rule names.
  • A renamed id was <the whole composition id>--<id>, so a very long data-composition-id lengthened every renamed id and every reference to it. A composition id over 48 characters now becomes its first 32 characters and a hash of the whole, and each mount's ids stay distinct. Ordinary ids are renamed exactly as before.
  • The bytes the renaming adds to the page and to mount stylesheets now count toward the render's 32 MB generated-output limit, so an oversized result fails with COMPOSITION_TOO_LARGE instead of running out of memory.

Why

When two mounted compositions use the same SVG id and something references it natively (url(#id), href="#id"), the compiler renames one of them. namespaceCollidingSvgIds returned one map per mount, and each map copied the page-wide table of inherited renames, so memory and time grew as mounts × ids. rewriteSvgIdReferencesInCss turned the whole map into a list for every selector. Neither the renamed ids nor the stylesheet growth was charged to the size budget the compiler already keeps for mounts.

Related work

None.

How

  • svgIdNamespacing.ts: each mount gets a ScopedIdMap, its own renames read over the one shared page table. Ids the mount defines itself still hide the shared entry, as the copied map did. Callers read it through get, has and size, the only parts they used.
  • rewriteSvgIdReferencesInCss reads the #ids a selector names (selectorIdTokens, the same reading replaceSelectorIdTokens does) and looks each up.
  • sanitizeNamespaceSegment caps the prefix, hashing with the FNV-1a function canary.ts already had, now exported. The existing used-id suffix keeps the result unique.
  • namespaceCollidingSvgIds takes an optional charge(scopeIndex, bytes), called after each element with the attribute bytes it added. inlineSubCompositions wires it, and the stylesheet growth, to chargeEmitted under the mount's own file, which the producer charges to its expansion budget.
  • svg-selector-aliases-inline.ts is regenerated, since it bundles selectorIdTokens.ts.

Not changed: rewriteSvgSelectors still builds its reference targets from every recorded alias once per selector. That is bounded by one mount's own input, and a follow-up PR will fix it.

Test plan

  • fails without the fix: these tests in packages/core/src/compiler/svgIdNamespacing.test.ts, the first commit:
    • "shares one document rename table across scopes instead of copying it per scope" (41,200 table writes without the fix);
    • "looks up only the ids a stylesheet names" (600,000);
    • "caps a long namespace's prefix, each mount's renamed ids still distinct" (a 5,007-character id);
    • "charges every attribute byte it adds";
    • "stops at the first charge the budget refuses".
  • fails without the fix: "charges the bytes SVG id renaming adds to the mount that grew" in packages/core/src/compiler/inlineSubCompositions.test.ts.
  • Linux box at the fix commit:
    • the 6 new tests pass 3 of 3 runs;
    • packages/core compiler and runtime pass all 4,045 tests;
    • htmlCompiler.sizeLimit.test.ts in the producer passes 15 of 15;
    • the core and producer typechecks exit 0;
    • svg-selector-aliases-inline.ts regenerates identical to the committed file at main's source, and to this branch's at the fix.
  • Unit tests added/updated
  • Manual testing performed
  • Comments follow CONTRIBUTING.md "Comments"

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed pinned head eca3cb02e32f3c69b332a14b13d35b4971502b38.

[P2] Compute the sanitized/hash prefix once per scope, before minting its IDs (svgIdNamespacing.ts, mintNamespacedId / planRenames). mintNamespacedId calls sanitizeNamespaceSegment(namespace) for every renamed ID. With a namespace over 48 characters, each call scans the entire namespace with replace and FNV-1a. The output prefix is capped, but compile work remains O(namespace length × renamed ID count), even though that namespace appears only once in the mount's input. These scans all happen in planning before the first generated-byte charge can reject the page.

A deterministic regression probe using two mounts, 200 colliding/referenced SVG IDs and one 5,000-character namespace recorded 200 full FNV-1a calls (1,000,000 character iterations), rather than one hash for that scope. A longer composition ID and more IDs amplify this while the emitted prefixes stay small enough not to trip the new output budget. Please cache/precompute each scope's prefix and add a regression that counts work across many IDs under one long namespace.

The shared rename table and per-selector ID lookup remove the two table-copy costs, and the added attribute/stylesheet charging is wired to the producer's budget. The declared separate follow-up for reference-alias selector work is not this finding.

Validation: full workspace build passed; 95 focused core SVG namespacing, selector-token and inlining tests plus 20 producer size-limit tests passed. The additional hash-count regression fails (200 vs at most 1); it was removed and the original core tests passed again. Required failing-test lines are present. CI was still running, with no failed checks at inspection. No render/browser benchmark performed.

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed at 7d0998c. My P2 is resolved: planRenames caches each mount's sanitized prefix before minting its ids, so a 5,000-character namespace is hashed once for 200 renamed ids. Collision suffixing and namespace output remain unchanged. The two regenerated page goldens contain only minifier symbol renames.

Validation: full workspace build passed; 100 focused core tests (including both page goldens) and 20 producer size-limit tests passed. Restoring the old implementation makes the new work-count regression fail with 200 hash calls instead of one; restoring the fix passes. The required fails-without-the-fix test lines are present. CI is still running, with no failed checks at submission. No remaining blocker found in this re-review.

— tai (automated review)

@github-actions

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 271 (base branch 271), smooth 62 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 (0)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 5bd3e32 Nov 18, 2026
95 checks passed
@miguel-heygen
miguel-heygen deleted the secpub/svg-id-budget branch November 18, 2026 08:58
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