diff --git a/packages/core/src/compiler/inlineSubCompositions.test.ts b/packages/core/src/compiler/inlineSubCompositions.test.ts
index 0eb58ce905..57475c8373 100644
--- a/packages/core/src/compiler/inlineSubCompositions.test.ts
+++ b/packages/core/src/compiler/inlineSubCompositions.test.ts
@@ -1421,3 +1421,36 @@ describe("dropAuthoredSvgReferenceAliases", () => {
expect(serialize()).not.toContain("data-hf-svg-reference-aliases");
});
});
+
+// fails without the fix: each clip's scope tested every mount, and the page's scope every element, by contains().
+it("scopes a page of 2000 svg clips and 200 mounts without testing each against every other", () => {
+ const clips = Array.from(
+ { length: 2000 },
+ (_, i) => `
`,
+ ).join("");
+ const mounts = Array.from(
+ { length: 200 },
+ (_, i) => `
`,
+ ).join("");
+ const { document } = parseHTML(
+ `${mounts}${clips}
`,
+ );
+ const hosts = [...document.querySelectorAll("[data-composition-src]")];
+ const elements = document.querySelectorAll("*").length;
+ let owner = Object.getPrototypeOf(document.body);
+ while (!Object.hasOwn(owner, "contains")) owner = Object.getPrototypeOf(owner);
+ const contains = vi.spyOn(owner, "contains");
+ let checks: number;
+ try {
+ inlineSubCompositions(document, hosts, {
+ resolveHtml: () =>
+ `
`,
+ parseHtml: (html) => parseHTML(html).document,
+ });
+ checks = contains.mock.calls.length;
+ } finally {
+ contains.mockRestore();
+ }
+
+ expect(checks).toBeLessThan(elements);
+});
diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts
index 7152fd8ea2..64b370dae4 100644
--- a/packages/core/src/compiler/inlineSubCompositions.ts
+++ b/packages/core/src/compiler/inlineSubCompositions.ts
@@ -825,6 +825,14 @@ export function inlineSubCompositions(
dropAuthoredSvgReferenceAliases(document);
const hostRoots = svgIdScopes.map(({ root }) => root);
const clips = sceneClipRoots(document, hostRoots);
+ // Clips are outermost, so each host sits in at most one: its nearest clip ancestor.
+ const clipIndex = new Map(clips.map((clip, index) => [clip, index]));
+ const hostsInClip = clips.map((): Element[] => []);
+ for (const host of hostRoots) {
+ let at = host.parentElement;
+ while (at && !clipIndex.has(at)) at = at.parentElement;
+ if (at) hostsInClip[clipIndex.get(at)!]!.push(host);
+ }
const svgIdMaps = namespaceCollidingSvgIds(
document,
[
@@ -844,7 +852,7 @@ export function inlineSubCompositions(
...clips.map((root, index) => ({
root,
namespace: `clip-${index}`,
- exclude: hostRoots.filter((host) => root.contains(host)),
+ exclude: hostsInClip[index],
})),
],
undefined,
diff --git a/packages/core/src/compiler/svgIdNamespacing.test.ts b/packages/core/src/compiler/svgIdNamespacing.test.ts
index 5782d1ce4f..6455c8fc34 100644
--- a/packages/core/src/compiler/svgIdNamespacing.test.ts
+++ b/packages/core/src/compiler/svgIdNamespacing.test.ts
@@ -17,6 +17,7 @@ import {
SVG_AUTHORED_ID_ATTR,
namespaceCollidingSvgIds,
rewriteSvgIdReferencesInCss,
+ sceneClipRoots,
type SvgIdScope,
} from "./svgIdNamespacing";
@@ -750,3 +751,36 @@ it("rewriteSvgIdReferencesInCss scans a selector of 300000 unclosed brackets in
rewriteSvgIdReferencesInCss(`:is(${run}) { fill: red; }`, new Map(), references),
).toContain(run);
}, 10_000);
+
+// fails without the fix: every element of the root scope was checked against every excluded clip, one contains() each.
+it("checks a scope's elements against thousands of excluded clips in one walk each, not once per clip", () => {
+ const clips = Array.from(
+ { length: 2000 },
+ (_, i) => `
`,
+ ).join("");
+ const { document } = parseHTML(
+ ``,
+ );
+ const queryable = document as unknown as Parameters[0];
+ const root = document.querySelector('[data-composition-id="main"]')!;
+ const host = document.querySelector("[data-composition-src]")!;
+ const elements = root.querySelectorAll("*").length;
+ let owner = Object.getPrototypeOf(root);
+ while (!Object.hasOwn(owner, "contains")) owner = Object.getPrototypeOf(owner);
+ const contains = vi.spyOn(owner, "contains");
+ let checks: number;
+ let found: Element[];
+ try {
+ found = sceneClipRoots(queryable, [host]);
+ namespaceCollidingSvgIds(queryable, [
+ { root, namespace: "main", exclude: [host, ...found] },
+ ...found.map((clip, i) => ({ root: clip, namespace: `clip${i}` })),
+ ]);
+ checks = contains.mock.calls.length;
+ } finally {
+ contains.mockRestore();
+ }
+
+ expect(found).toHaveLength(2000);
+ expect(checks).toBeLessThan(elements);
+});
diff --git a/packages/core/src/compiler/svgIdNamespacing.ts b/packages/core/src/compiler/svgIdNamespacing.ts
index b413ac3e8e..f96d649449 100644
--- a/packages/core/src/compiler/svgIdNamespacing.ts
+++ b/packages/core/src/compiler/svgIdNamespacing.ts
@@ -223,15 +223,16 @@ function collectNativelyReferencedIds(
return referenced;
}
-function isExcluded(el: Element, exclude: readonly Element[]): boolean {
- return exclude.some((excluded) => excluded === el || excluded.contains(el));
+function isExcluded(el: Element, exclude: ReadonlySet): boolean {
+ for (let at: Element | null = el; at; at = at.parentElement) if (exclude.has(at)) return true;
+ return false;
}
/** `root` plus every descendant that is not inside an excluded subtree. */
function collectScopeElements(scope: SvgIdScope): Element[] {
- const exclude = scope.exclude ?? [];
+ const exclude = new Set(scope.exclude);
const descendants = [...scope.root.querySelectorAll("*")];
- const own = exclude.length ? descendants.filter((el) => !isExcluded(el, exclude)) : descendants;
+ const own = exclude.size ? descendants.filter((el) => !isExcluded(el, exclude)) : descendants;
return [scope.root, ...own];
}
@@ -244,9 +245,9 @@ interface ResolvedScope {
/** Every ``-subtree element in the scope that carries an id. */
function collectSvgIdElements(scope: SvgIdScope): Element[] {
- const exclude = scope.exclude ?? [];
+ const exclude = new Set(scope.exclude);
const matches = [...scope.root.querySelectorAll("svg [id], svg[id]")];
- return exclude.length ? matches.filter((el) => !isExcluded(el, exclude)) : matches;
+ return exclude.size ? matches.filter((el) => !isExcluded(el, exclude)) : matches;
}
function resolveScope(scope: SvgIdScope): ResolvedScope {
@@ -451,11 +452,12 @@ const CLIP_SELECTOR = "[data-start]:not([data-composition-id]):not([data-composi
/** The outermost timed clips holding an `` outside every composition host. Each is its own id scope, as an
* inlined composition is, so two scenes drawn with the same ids each resolve their own `url(#id)` and `href`. */
export function sceneClipRoots(document: SvgIdQueryable, hosts: readonly Element[]): Element[] {
+ const hostSet = new Set(hosts);
return [...document.querySelectorAll(CLIP_SELECTOR)].filter(
(clip) =>
!clip.parentElement?.closest(CLIP_SELECTOR) &&
!!clip.parentElement?.closest("[data-composition-id]") &&
- !isExcluded(clip, hosts) &&
+ !isExcluded(clip, hostSet) &&
!!clip.querySelector("svg"),
);
}