Skip to content

Commit 17a68b1

Browse files
committed
perf(core): the preview unmounts scenes the playhead leaves, as kept ones slowed each mount
1 parent 993c830 commit 17a68b1

4 files changed

Lines changed: 8 additions & 57 deletions

File tree

‎packages/core/src/runtime/init.swapScenes.test.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { WebAudioTransport } from "./webAudioTransport";
88
import { probeAndCacheElementVolume } from "./mediaVolumeEnvelope.js";
99
import { wrapScopedCompositionScript } from "../compiler/compositionScoping";
1010
import { AFTER_FONTS_SCRIPT_TYPE } from "../compiler/scriptRuns";
11-
import { parkFarScenesAtBoot, setKeptSceneElementsForTests } from "./sceneWindow";
11+
import { parkFarScenesAtBoot } from "./sceneWindow";
1212

1313
vi.mock("./mediaVolumeEnvelope.js", async (importOriginal) => {
1414
const actual = await importOriginal<typeof import("./mediaVolumeEnvelope.js")>();
@@ -1764,18 +1764,14 @@ describe("the preview's scene window", () => {
17641764
for (const k of Object.keys(made)) delete made[k];
17651765
for (const label of ["a1", "a2", "b"]) made[label] = tl(label);
17661766
(window as unknown as { __made: typeof made }).__made = made;
1767-
// None kept: every scene the window leaves parks, unless a test gives a budget.
1768-
keptBudget = setKeptSceneElementsForTests(0);
17691767
});
17701768
afterEach(() => {
17711769
window.__hfRuntimeTeardown?.();
17721770
document.head.innerHTML = "";
17731771
document.body.innerHTML = "";
17741772
delete window.__hfSceneAnimations;
17751773
vi.restoreAllMocks();
1776-
setKeptSceneElementsForTests(keptBudget);
17771774
});
1778-
let keptBudget = 0;
17791775

17801776
it("parks a scene away from the start, keeping its timing, and never runs its script", async () => {
17811777
bootWindowed([S0, S1, S2], trackingRoot().root);
@@ -1940,22 +1936,6 @@ describe("the preview's scene window", () => {
19401936
expect(sceneHost("s2").textContent).toBe("two");
19411937
});
19421938

1943-
it("keeps the scenes the playhead left mounted, most recent first, while they fit the budget", async () => {
1944-
// Each scene here is its host and one child: two elements.
1945-
setKeptSceneElementsForTests(2);
1946-
bootWindowed([S0, S1, S2], trackingRoot().root);
1947-
await tick();
1948-
await seekAndSettle(2.5);
1949-
for (let i = 0; i < 5; i++) await tick();
1950-
expect(parked()).toEqual([]);
1951-
const left = sceneHost("s1");
1952-
await seekAndSettle(5);
1953-
for (let i = 0; i < 5; i++) await tick();
1954-
expect(parked()).toEqual(["s0"]);
1955-
await seekAndSettle(2.5);
1956-
expect(sceneHost("s1")).toBe(left);
1957-
});
1958-
19591939
const audio = (src: string) => `<audio src="${src}" data-start="0.5" data-duration="1"></audio>`;
19601940

19611941
it("lets a scene mounting ahead of a playing film join its mix, leaving what already plays alone", async () => {

‎packages/core/src/runtime/init.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
parkedTimelineScenes,
1111
park as parkScene,
1212
rememberScene,
13-
scenesToKeep,
1413
scenesWanted,
1514
unpark,
1615
windowedScenes,
@@ -4295,8 +4294,6 @@ export function initSandboxRuntimeModular(): void {
42954294
};
42964295
// Scenes Studio holds mounted for selecting or editing, whatever the playhead does.
42974296
let heldScenes = new Set<string>();
4298-
// Scenes the window wanted, most recently first: those it left stay mounted while they fit the budget.
4299-
let recentScenes: string[] = [];
43004297
moveSceneWindow = (time: number): boolean => {
43014298
if (windowScenes.length === 0 || state.tornDown) return false;
43024299
const parked = (name: string) => parkedNames.has(name);
@@ -4308,16 +4305,14 @@ export function initSandboxRuntimeModular(): void {
43084305
for (const name of heldScenes) near.add(name);
43094306
const key = [...near].join("\n");
43104307
if (key === windowNear) return landed !== undefined;
4311-
recentScenes = [...near, ...recentScenes.filter((name) => !near.has(name))];
43124308
// One step at a time, planned again after each, so a later seek or scrub overtakes the rest.
43134309
const neighbour = windowScenes
43144310
.filter(({ name }) => near.has(name) && parked(name))
43154311
.sort((a, b) => Math.abs(a.start - time) - Math.abs(b.start - time))[0];
43164312
const toMount = landed ?? neighbour;
4317-
const kept = toMount ? null : scenesToKeep(recentScenes.filter((name) => !near.has(name)));
4318-
const toRetire = windowScenes.filter(
4319-
({ name }) => kept && !near.has(name) && !kept.has(name) && !parked(name),
4320-
);
4313+
const toRetire = toMount
4314+
? []
4315+
: windowScenes.filter(({ name }) => !near.has(name) && !parked(name));
43214316
if (!toMount && toRetire.length === 0) {
43224317
windowNear = key;
43234318
return false;

‎packages/core/src/runtime/sceneWindow.ts‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,6 @@ export function scenesWanted(scenes: WindowedScene[], time: number, playing: boo
6464
return new Set(scenes.filter(wanted).map((s) => s.name));
6565
}
6666

67-
// At most about 120 MB at the ~2 KB of renderer memory per element a long film measured.
68-
let keptSceneElements = 60_000;
69-
export const setKeptSceneElementsForTests = (elements: number): number => {
70-
const was = keptSceneElements;
71-
keptSceneElements = elements;
72-
return was;
73-
};
74-
75-
/** Of the mounted scenes the playhead left, most recent first, those that stay mounted within the element budget. */
76-
export function scenesToKeep(recentFirst: string[]): Set<string> {
77-
const kept = new Set<string>();
78-
let elements = 0;
79-
for (const name of recentFirst) {
80-
const host = hostOf(name);
81-
if (!host || isParked(host)) continue;
82-
elements += host.getElementsByTagName("*").length + 1;
83-
if (elements > keptSceneElements) break;
84-
kept.add(name);
85-
}
86-
return kept;
87-
}
88-
8967
export const hostOf = (name: string): Element | null =>
9068
document.querySelector(`${partsSelector(name)}:not(style, script)`);
9169

‎packages/producer/src/services/coreRuntimeBrowser.test.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,8 +1344,6 @@ describe("core runtime browser contract", () => {
13441344
});
13451345
try {
13461346
const scenes = [0, 1, 2, 3, 4, 5];
1347-
// Over half the preview's kept-scene budget each, so of the scenes the playhead left only the latest stays.
1348-
const filler = `<p>${"<i></i>".repeat(31_000)}</p>`;
13491347
const hosts = scenes
13501348
.map(
13511349
(i) =>
@@ -1360,7 +1358,7 @@ describe("core runtime browser contract", () => {
13601358
for (const i of scenes)
13611359
writeFileSync(
13621360
resolve(dir, `s${i}.html`),
1363-
`<template id="s${i}-template"><div data-composition-id="s${i}" data-duration="2">${"<b>x</b>".repeat(400)}${filler}<audio src="tone.wav" data-start="0" data-duration="1"></audio><script>const tl = gsap.timeline({ paused: true }); tl.to('[data-composition-id="s${i}"] b', { opacity: 0.5, duration: 1 }); window.__timelines["s${i}"] = tl;</script></div></template>`,
1361+
`<template id="s${i}-template"><div data-composition-id="s${i}" data-duration="2">${"<b>x</b>".repeat(400)}<audio src="tone.wav" data-start="0" data-duration="1"></audio><script>const tl = gsap.timeline({ paused: true }); tl.to('[data-composition-id="s${i}"] b', { opacity: 0.5, duration: 1 }); window.__timelines["s${i}"] = tl;</script></div></template>`,
13641362
);
13651363
const html = addScenePartsManifest(
13661364
await bundleToSingleHtml(dir, { sceneParts: true, staticGuard: false }),
@@ -1408,13 +1406,13 @@ describe("core runtime browser contract", () => {
14081406
});
14091407
// The window unmounts only once it has mounted what is ahead.
14101408
await page.waitForFunction(
1411-
() => document.querySelectorAll("[data-hf-parked-scene]").length >= 4,
1409+
() => document.querySelectorAll("[data-hf-parked-scene]").length >= 5,
14121410
);
1413-
// Four scenes unmounted, each over 30k nodes. Chrome keeps a captured element alive while
1411+
// Five scenes went out of the window, each over 800 nodes. Chrome keeps a captured element alive while
14141412
// its audio context lives, so each scene's audio element stays, but nothing of the scene around it.
14151413
expect(
14161414
await page.evaluate(() => document.querySelectorAll("[data-hf-parked-scene]").length),
1417-
).toBe(4);
1415+
).toBe(5);
14181416
expect((await kept()) - before).toBeLessThan(800);
14191417
} finally {
14201418
await audioBrowser.close();

0 commit comments

Comments
 (0)