Skip to content

Commit 8bf5b44

Browse files
perf: sublinear Studio rebuilds and seeks (#3837)
* perf(studio): resolve the overlay coordinate basis once per composition The iframe->overlay basis (composition root, root scale, iframe and overlay rects) was resolved inside every geometry call, so measuring a preview cost one querySelector("[data-composition-id]") plus three layout reads PER ELEMENT to rediscover something that is a property of the composition and the canvas zoom. It is now threaded through orientedGroupAwareOverlayRect, groupAwareOverlayRect, orientedOverlayRect, orientedVisibleOverlayRect and toVisibleOverlayRect the way toVisibleOverlayRects already batched it, and resolved once by the two callers that measure many elements in one synchronous pass: the off-canvas indicator rebuild and the overlay RAF loop. Both passes only read the DOM, so nothing can move the canvas between two measurements inside one of them. * perf(studio): rebuild the layer walk from the mutation, not the document A MutationObserver marked the off-canvas indicators dirty and the rebuild then re-derived every element in the preview from scratch. The observer's loudest source is inline style, which is what animation writes, so a composition just sitting there re-derived the whole document several times a second, and each element costs two getComputedStyle reads, two ancestor walks for its source file, and a textContent read over its whole subtree. The records now say WHAT to drop, not merely that something changed, and the per-element derivations are memoized between rebuilds. Two lifetimes, because they do not go stale together: whether an element renders (and how many layer children it has) dies on any nearby attribute write, while how it is ADDRESSED survives every style write and dies only on something that can renumber a selector. That split is what makes an ordinary animation frame cost no document queries at all. Not attributable to specific elements, so still a full rebuild: nodes added or removed, and any change to an identity attribute, which renumbers every element sharing a selector. Two supporting changes: - getDirectLayerChildren asked getDomLayerPatchTarget for a full patch target per child and used it as a boolean. The target carries the selector's occurrence index, which is a whole-document query the yes/no does not depend on. isDomLayerElement answers it without one, for every caller. Its unused options parameter goes with it. - The observer no longer filters attributes. An attribute it never hears about is one the cache would answer stale for, and the old filter omitted id and the data-composition-* attributes that decide a layer's identity. Widening only makes indicators refresh sooner; a rebuild is a pure read and is throttled either way. Every other caller of collectDomEditLayerItems passes no cache and is unchanged. * perf(runtime): visit only the clips a seek can flip Every seek, and every frame of playback, rebuilt the window of every video and audio element in the document and handed all of them to the per-clip sync loop. On a composition of eighty videos that is eighty window derivations and eighty per-clip passes to conclude that one clip is on screen. A clip is active only while start <= t < end, so a clip whose window excludes the new time is inactive there whatever its element state is. Sorting the windows by each endpoint turns a seek into two binary searches: the clips whose start or end lies between the old time and the new one, plus the ones that were in window at the old time. Anything outside both was out of window before and is out of window now, and was already paused and already evicted by the pass that last saw it go out. A one-frame step visits the clip or two that flip; a jump over a hundred clips still visits the hundred boundaries it crosses. The index carries only the windows, and it rides the revision the duration floors already ride: the same timing attributes, the same media metadata events, the same timeline registry signature. Nothing else is cached. Every field handed to syncRuntimeMedia is re-read from the element on every pass, because el.duration can be reset under us by the load() retry, and a cached copy of it would be exactly the stale duration the two-resolver-scope rule exists to prevent. The render/export path does not consult the index at all and visits every element on every frame, and a render seek clears the sweep so a later live seek cannot inherit a position it did not establish. Also folds the duration floors' own invalidation into that shared revision. Draining the observer is what detects a change, and only the first reader in a task gets the records, so two caches each checking for themselves would have had the second told nothing changed. * refactor(studio): split the overlay basis and the layer-walk read into their own modules Clears the 600-line file cap and the fallow audit on this branch. Behaviour is unchanged: this moves code, it does not alter any of it. File size. Both files were already near the cap on main and my three levers pushed them over (615 vs 598, and 605 vs 584): - domEditOverlayGeometry.ts -> 561. The iframe->overlay coordinate basis (OverlayRootScale, computeOverlayRootScale and the root/dimension lookups it owns) moves to domEditOverlayBasis.ts. It is the one piece of that file every other piece depends on, and nothing in it is about a single element's geometry. Its two callers now import it from there. - domEditingLayers.ts -> 587. The per-element read that the walk performs moves to readDomEditLayerWalkEntry in domEditLayerWalkCache.ts, the module that owns the memoization it reads through. The walk keeps the traversal and the depth bookkeeping, which are the parts that are actually about walking. No unrelated code was trimmed to make room. Duplication, all three clone groups fallow flagged: - The runtime seek fixture (createMockTimeline, the synchronous animation-frame clock, the CSS.escape shim, stubDuration) was copied between init.timingResolver.test.ts and init.mediaClipIndex.test.ts. It moves to runtimeSeekFixture.test-helpers.ts. Each suite keeps its own vi.mock calls, which are file-scoped and cannot be shared. The file is excluded from tsconfig.runtime.json alongside the test files it serves, for the same reason. - domEditLayerWalkCache.test.ts repeated its mount/observe/first-walk setup in three tests; that is now withWarmWalk. Complexity. Only one of fallow's three findings is attributable to this branch, and fallow agrees: it marks the other two inherited and excludes them from the gate. - offCanvasIndicatorRefresh.ts update was NEW (absent from main's report, 10 cyclomatic / 31.6 CRAP here). The optional-chain-and-default I added to drain the observer becomes drainPendingLayerMutations, with its own unit tests, and the function drops off the report entirely. - useDomEditOverlayRects.ts update: 37 cyclomatic / 69 cognitive on main AND here. My change added one statement and no branch; the function grew 139 -> 147 lines, which is what resurfaced it. Left alone. - domEditingDom.ts escapeCssIdentifier: 24 cyclomatic / 19 cognitive / 148.4 CRAP on main AND here. Untouched by this branch; only its line number moved (173 -> 182) because the composition-source-map revision counter sits above it. Left alone.
1 parent 622ea5b commit 8bf5b44

17 files changed

Lines changed: 1431 additions & 221 deletions
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import type { RuntimeTimelineLike } from "./types";
3+
import {
4+
createMockTimeline,
5+
installImmediateAnimationFrame,
6+
resetRuntimeFixtureDom,
7+
} from "./runtimeSeekFixture.test-helpers";
8+
9+
/**
10+
* `visits` counts the media elements handed to `syncRuntimeMedia` — the pass's
11+
* own definition of "visited", since every per-clip effect happens inside that
12+
* loop.
13+
*
14+
* `disableNarrowing` neutralises ONLY the index's element narrowing, leaving
15+
* every other line of the runtime identical. That is what makes the cross-check
16+
* below a comparison of the indexed path against the full-visit path, rather
17+
* than a comparison of two differently-configured runtimes.
18+
*/
19+
const mediaSpy = vi.hoisted(() => ({ visits: [] as number[], disableNarrowing: false }));
20+
21+
vi.mock("./media", async (importOriginal) => {
22+
const actual = await importOriginal<typeof import("./media")>();
23+
return {
24+
...actual,
25+
refreshRuntimeMediaCache: (params?: Parameters<typeof actual.refreshRuntimeMediaCache>[0]) =>
26+
actual.refreshRuntimeMediaCache(
27+
mediaSpy.disableNarrowing && params ? { ...params, elements: undefined } : params,
28+
),
29+
syncRuntimeMedia: (params: Parameters<typeof actual.syncRuntimeMedia>[0]) => {
30+
mediaSpy.visits.push(params.clips.length);
31+
return actual.syncRuntimeMedia(params);
32+
},
33+
};
34+
});
35+
36+
const { initSandboxRuntimeModular } = await import("./init");
37+
38+
/**
39+
* `count` back-to-back one-second clips, so the transport crosses exactly one
40+
* start and one end per second of travel and the flip set is knowable without
41+
* reproducing the index's arithmetic.
42+
*/
43+
function mountClips(count: number): void {
44+
const clips = Array.from(
45+
{ length: count },
46+
(_unused, i) => `<video id="clip${i}" data-start="${i}" data-duration="1"></video>`,
47+
).join("");
48+
document.body.innerHTML = `<div data-composition-id="main" data-root="true" data-start="0">${clips}</div>`;
49+
for (const video of document.querySelectorAll("video")) {
50+
Object.defineProperty(video, "duration", { value: 1, configurable: true });
51+
}
52+
window.__timelines = { main: createMockTimeline(count) };
53+
}
54+
55+
/** Every observable the media pass can write, for every element. Compared
56+
* between the two arms instead of re-deriving what each should have been. */
57+
function mediaState(): Array<Record<string, unknown>> {
58+
return Array.from(document.querySelectorAll("video, audio")).map((raw) => {
59+
const el = raw as HTMLMediaElement;
60+
return {
61+
id: el.id,
62+
paused: el.paused,
63+
currentTime: el.currentTime,
64+
volume: el.volume,
65+
muted: el.muted,
66+
visibility: (el as HTMLElement).style.visibility,
67+
};
68+
});
69+
}
70+
71+
describe("per-seek media clip index", () => {
72+
const originalRequestAnimationFrame = window.requestAnimationFrame;
73+
const originalCancelAnimationFrame = window.cancelAnimationFrame;
74+
75+
beforeEach(() => {
76+
resetRuntimeFixtureDom();
77+
installImmediateAnimationFrame();
78+
mediaSpy.visits = [];
79+
mediaSpy.disableNarrowing = false;
80+
});
81+
82+
afterEach(() => {
83+
window.__hfRuntimeTeardown?.();
84+
window.requestAnimationFrame = originalRequestAnimationFrame;
85+
window.cancelAnimationFrame = originalCancelAnimationFrame;
86+
document.body.innerHTML = "";
87+
window.__timelines = {} as Record<string, RuntimeTimelineLike>;
88+
delete window.__player;
89+
delete window.__playerReady;
90+
delete window.__renderReady;
91+
mediaSpy.disableNarrowing = false;
92+
});
93+
94+
/** Elements visited by ONE single-frame seek, after the transport has already
95+
* settled at a time (so the sweep has a previous position to work from). */
96+
function visitsForOneFrameSeek(clipCount: number): number {
97+
mountClips(clipCount);
98+
initSandboxRuntimeModular();
99+
window.__player!.seek(3.0);
100+
mediaSpy.visits = [];
101+
window.__player!.seek(3.02);
102+
expect(mediaSpy.visits).toHaveLength(1);
103+
return mediaSpy.visits[0]!;
104+
}
105+
106+
/**
107+
* The load-bearing assertion, and it is INVARIANCE, not a threshold. A
108+
* threshold ("visits fewer than 20 elements") passes on a small fixture while
109+
* a real composition of eighty videos still visits eighty. Quadrupling the
110+
* clip count must not change what one frame costs at all.
111+
*
112+
* Before the index, both numbers were the clip count: every seek rebuilt the
113+
* window of every media element in the document and handed all of them to the
114+
* per-clip loop.
115+
*/
116+
it("visits the same number of clips for a one-frame seek at n clips and at 4n", () => {
117+
const few = visitsForOneFrameSeek(8);
118+
const many = visitsForOneFrameSeek(32);
119+
120+
expect(many).toBe(few);
121+
// A ceiling too: within one frame at t=3.0 only the clip covering [3,4) is
122+
// in window and no boundary is crossed, so this is the active set alone.
123+
expect(many).toBeLessThanOrEqual(2);
124+
});
125+
126+
it("still visits every clip whose window a long jump crosses", () => {
127+
mountClips(32);
128+
initSandboxRuntimeModular();
129+
window.__player!.seek(1.0);
130+
mediaSpy.visits = [];
131+
// Twenty seconds of travel crosses twenty starts and twenty ends. The point
132+
// is that the index does not silently skip them — a sublinear seek that
133+
// under-visits leaves elements playing.
134+
window.__player!.seek(21.0);
135+
expect(mediaSpy.visits[0]!).toBeGreaterThanOrEqual(20);
136+
});
137+
138+
/**
139+
* The equivalence half, and the one that catches a wrong optimisation rather
140+
* than a missing one. Random seek sequences, and after every single seek the
141+
* indexed runtime's media state must equal the full-visit runtime's — the
142+
* same code with only the narrowing turned off.
143+
*
144+
* Deterministic: a fixed seed, so a failure is reproducible rather than a
145+
* flake someone reruns until it passes.
146+
*/
147+
it("leaves every media element in the same state as a full-visit pass", () => {
148+
let seed = 0x5eed;
149+
const random = () => {
150+
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
151+
return seed / 0x7fffffff;
152+
};
153+
154+
const runSequence = (times: number[], disableNarrowing: boolean) => {
155+
mediaSpy.disableNarrowing = disableNarrowing;
156+
mountClips(12);
157+
initSandboxRuntimeModular();
158+
const states: Array<Array<Record<string, unknown>>> = [];
159+
for (const time of times) {
160+
window.__player!.seek(time);
161+
states.push(mediaState());
162+
}
163+
window.__hfRuntimeTeardown?.();
164+
document.body.innerHTML = "";
165+
return states;
166+
};
167+
168+
for (let sequence = 0; sequence < 4; sequence += 1) {
169+
// A mix of single frames, mid-clip nudges and long jumps in both
170+
// directions — the shapes that decide whether the sweep is right.
171+
const times = Array.from({ length: 16 }, () => Math.round(random() * 1200) / 100);
172+
expect(runSequence(times, false), `seek sequence ${sequence}`).toEqual(
173+
runSequence(times, true),
174+
);
175+
}
176+
// Two full runtimes per sequence, and under coverage instrumentation that
177+
// runs well past the 5s default. An explicit budget, not a global bump.
178+
}, 30_000);
179+
180+
/**
181+
* The render path must not consult the index at all: capture depends on the
182+
* exact state of every element on every frame, and a frame captured against a
183+
* stale one cannot be recovered. Same latch, and the same reasoning, as the
184+
* duration-floor cache.
185+
*/
186+
it("visits every clip on a render-capture seek, however small the step", () => {
187+
mountClips(16);
188+
initSandboxRuntimeModular();
189+
window.__player!.renderSeek(3.0);
190+
mediaSpy.visits = [];
191+
window.__player!.renderSeek(3.02);
192+
193+
expect(mediaSpy.visits[0]!).toBe(16);
194+
});
195+
196+
it("reseeds after a render seek, so a later live seek cannot trust a stale sweep", () => {
197+
mountClips(16);
198+
initSandboxRuntimeModular();
199+
window.__player!.renderSeek(3.0);
200+
mediaSpy.visits = [];
201+
// The sweep has no trustworthy previous position, so this visits everything
202+
// rather than assuming the render pass left it one.
203+
window.__player!.seek(3.02);
204+
expect(mediaSpy.visits[0]!).toBe(16);
205+
});
206+
207+
it("re-derives the index when a clip is moved on the timeline", () => {
208+
mountClips(16);
209+
initSandboxRuntimeModular();
210+
window.__player!.seek(3.0);
211+
212+
// clip15 lived at [15,16). Moved onto the playhead it must become active,
213+
// which it cannot if the index still holds its old window.
214+
document.querySelector("#clip15")!.setAttribute("data-start", "3");
215+
mediaSpy.visits = [];
216+
window.__player!.seek(3.01);
217+
218+
expect(document.querySelector<HTMLVideoElement>("#clip15")!.style.visibility).not.toBe(
219+
"hidden",
220+
);
221+
});
222+
});

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

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import type { RuntimeTimelineLike } from "./types";
2+
import {
3+
createMockTimeline,
4+
installImmediateAnimationFrame,
5+
resetRuntimeFixtureDom,
6+
stubDuration,
7+
} from "./runtimeSeekFixture.test-helpers";
38

49
const resolverSpy = vi.hoisted(() => ({ constructions: 0 }));
510
const mediaSpy = vi.hoisted(() => ({ beforeSync: null as null | (() => void) }));
@@ -30,49 +35,13 @@ vi.mock("./media", async (importOriginal) => {
3035

3136
const { initSandboxRuntimeModular } = await import("./init");
3237

33-
function createMockTimeline(duration: number): RuntimeTimelineLike {
34-
const state = { time: 0, paused: true };
35-
return {
36-
play: () => {
37-
state.paused = false;
38-
},
39-
pause: () => {
40-
state.paused = true;
41-
},
42-
seek: (time?: number) => (time === undefined ? state.time : (state.time = time)),
43-
totalTime: (time?: number) => (time === undefined ? state.time : (state.time = time)),
44-
time: () => state.time,
45-
duration: () => duration,
46-
add: () => {},
47-
paused: (value?: boolean) =>
48-
typeof value === "boolean" ? (state.paused = value) : state.paused,
49-
timeScale: () => {},
50-
set: () => {},
51-
getChildren: () => [],
52-
};
53-
}
54-
55-
/** jsdom leaves `duration` as NaN; a writable getter also lets a test mimic the
56-
* `el.load()` reset `syncRuntimeMedia` performs on the seek-retry path. */
57-
function stubDuration(el: HTMLMediaElement, initial: number): { set: (next: number) => void } {
58-
let value = initial;
59-
Object.defineProperty(el, "duration", { get: () => value, configurable: true });
60-
return { set: (next: number) => (value = next) };
61-
}
62-
6338
describe("runtime timing resolver scoping", () => {
6439
const originalRequestAnimationFrame = window.requestAnimationFrame;
6540
const originalCancelAnimationFrame = window.cancelAnimationFrame;
6641

6742
beforeEach(() => {
68-
document.body.innerHTML = "";
69-
(globalThis as typeof globalThis & { CSS?: { escape?: (value: string) => string } }).CSS ??= {};
70-
globalThis.CSS.escape ??= (value: string) => value;
71-
window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
72-
callback(0);
73-
return 1;
74-
}) as typeof window.requestAnimationFrame;
75-
window.cancelAnimationFrame = (() => {}) as typeof window.cancelAnimationFrame;
43+
resetRuntimeFixtureDom();
44+
installImmediateAnimationFrame();
7645
resolverSpy.constructions = 0;
7746
mediaSpy.beforeSync = null;
7847
});

0 commit comments

Comments
 (0)