Files
hyperframes/packages/core/src/runtime/init.timingResolver.test.ts
T
Miguel Ángel d4fba55a4e perf(runtime): a paused preview stops burning CPU (#3845)
* perf(runtime): stop the preview transport ticking when the editor is paused

A paused, untouched preview asked the browser for a fresh frame sixty times a
second and re-read the page on each one. Nothing it looked at could change
without some observable event firing, so the loop now stands down and wakes on
that event instead, with a slow timer as the safety net.

Parked, the loop keeps two jobs the 60 Hz version did implicitly: the control
bridge's paused heartbeat, on the same interval as before, and a re-read of the
timeline registry, which is a plain object no observer and no event can report.

Everything else arrives by an event now: timing-attribute edits, mounted or
removed timed elements and media metadata through the composition-timing
observer that already existed; a manual-edit gesture starting or ending through
a new attribute-filtered observer, which also replaces a whole-document query
that ran on every paused frame; and playhead or play-state changes through the
forced state post every transport mutation already ends in.

Three periodic jobs (re-binding the root timeline, posting the clip manifest,
binding media-metadata listeners) used a frame counter as a proxy for "the
document may have changed". They now ask that question directly, because the
counter stops advancing while the loop is parked.

The render path is untouched: the loop never parks while an export render is
driving frames. That test is the pair of renderCaptureSeekStarted and the
producer's injected seek config, not the flag alone, because Studio's own
preview falls back to renderSeek for overhanging timelines.

Idle, paused, no input, on a 1689-element project: main-thread self time
128 -> 8.5 ms/s and animation-frame callbacks 282 -> 4.3 per second, both
measured with the runtime and editor changes in place.

* fix(runtime): keep the rebind policy the only owner of "may rebind now"

The parked-loop change let a composition-timing change OR its way past
shouldAttemptPeriodicTimelineBind, which removed the hold that keeps an async
rebind off the first two seconds of playback, and let the clip manifest post on
every frame of a composition that mutates the DOM every frame (measured: 30
posts in 30 frames against one). The change is now an input to that policy,
which still applies the hold, and the change-driven path is confined to the
paused path and rate-limited to the posts per second the frame counter already
produced. A change it defers stays pending, and a pending change keeps the loop
awake, so deferring can never drop it.

Two more holes from the same review:

The parked poll compared only the composition timing revision, so an adapter
duration floor that grew was never noticed. Adapters infer duration from live
animation objects that change with no DOM mutation and no media event, which
makes it the second input nothing can push; both are now in one witness.

Draining the gesture observer's records to answer within a task suppressed the
observer's own callback for them, so a reader could consume the notification
that un-parks the transport. Draining now notifies.

The watch reports whether it is observing at all, and the loop refuses to park
when it is not. That path is unreachable today because the colour-grading
runtime constructs a MutationObserver unconditionally during the same init; the
flag is the explicit statement of the invariant for the day that changes.

* fix(runtime): clear the pending-change latch after the post, not before

postTimeline walks author DOM and can throw. The tail scheduler runs in the
tick's finally either way, so clearing the latch first let it see nothing owed
and park with the change undelivered — and nothing would deliver it until some
unrelated change happened to wake the loop again. Clearing after the post means
a throw leaves the change owed, the loop stays awake, and the frame counter
retries it within twenty frames. Same rule the shared editor loop already
follows for its own re-arm.

Also rewords the note on draining the gesture observer's records: that is
hardening, not a fix for a live lost wake. isActive has exactly one caller
today, inside transportTick, and a tick is its own task, so the observer's
microtask has already run by then.

* fix(runtime): stop the parked heartbeat when the page has gone away

The parked transport holds a timer where the old loop held only an animation
frame, and a frame is discarded when a page or a test environment is torn down
while a timer is not. A test that initialises a runtime and abandons it
therefore left an 80ms timer to fire into a dead global, which CI reported as
an unhandled ReferenceError attributed to whichever file was running when it
landed.

Two changes, because the leak has two ends. The heartbeat stops instead of
re-arming when window or document is gone: nothing is left to report a state
change to, so stopping is the answer rather than throwing. And the one test
that initialised a runtime without ever tearing it down now tears it down.
2026-09-10 08:29:22 -07:00

136 lines
5.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
createMockTimeline,
installImmediateAnimationFrame,
resetRuntimeFixtureDom,
stubDuration,
} from "./runtimeSeekFixture.test-helpers";
const resolverSpy = vi.hoisted(() => ({ constructions: 0 }));
const mediaSpy = vi.hoisted(() => ({ beforeSync: null as null | (() => void) }));
vi.mock("./startResolver", async (importOriginal) => {
const actual = await importOriginal<typeof import("./startResolver")>();
return {
...actual,
createRuntimeStartTimeResolver: (
params: Parameters<typeof actual.createRuntimeStartTimeResolver>[0],
) => {
resolverSpy.constructions += 1;
return actual.createRuntimeStartTimeResolver(params);
},
};
});
vi.mock("./media", async (importOriginal) => {
const actual = await importOriginal<typeof import("./media")>();
return {
...actual,
syncRuntimeMedia: (params: Parameters<typeof actual.syncRuntimeMedia>[0]) => {
mediaSpy.beforeSync?.();
return actual.syncRuntimeMedia(params);
},
};
});
const { initSandboxRuntimeModular } = await import("./init");
describe("runtime timing resolver scoping", () => {
const originalRequestAnimationFrame = window.requestAnimationFrame;
const originalCancelAnimationFrame = window.cancelAnimationFrame;
beforeEach(() => {
resetRuntimeFixtureDom();
installImmediateAnimationFrame();
resolverSpy.constructions = 0;
mediaSpy.beforeSync = null;
});
afterEach(() => {
// The runtime holds a timer while its transport is parked. Abandoning an
// initialised runtime leaves that timer to fire after the environment is
// torn down, which vitest reports as an unhandled error in whichever file
// happens to be running at the time.
window.__hfRuntimeTeardown?.();
window.requestAnimationFrame = originalRequestAnimationFrame;
window.cancelAnimationFrame = originalCancelAnimationFrame;
mediaSpy.beforeSync = null;
delete window.__player;
delete window.__playerReady;
});
/** One seek plus one transport tick: between them they drive all three
* scopes (media cache, visibility pass, media-window duration scan). */
const passWithMediaCount = (count: number): number => {
const frames: FrameRequestCallback[] = [];
window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
frames.push(callback);
return frames.length;
}) as typeof window.requestAnimationFrame;
const flushFrame = () => frames.splice(0, frames.length).forEach((callback) => callback(0));
const clips = Array.from(
{ length: count },
(_, i) => `<video id="clip${i}" data-start="${i}" data-duration="1"></video>`,
).join("");
document.body.innerHTML = `<div data-composition-id="main" data-root="true">${clips}</div>`;
for (const video of document.querySelectorAll("video")) {
stubDuration(video as HTMLVideoElement, 1);
}
window.__timelines = { main: createMockTimeline(count) };
initSandboxRuntimeModular();
// Let the runtime bind its timeline, so the tick below takes the
// duration-resolution branch rather than short-circuiting.
flushFrame();
resolverSpy.constructions = 0;
window.__player?.renderSeek(0.5);
flushFrame();
return resolverSpy.constructions;
};
// Non-vacuity: this is the whole point of the change. Every resolver carries
// WeakMap caches, so building one per call means a seek re-walks the ancestor
// chain of every element. Before the fix this count grew with media count
// (~7 constructions per media element); after it, it is flat.
it("builds a constant number of timing resolvers per seek, not one per element", () => {
const few = passWithMediaCount(2);
const many = passWithMediaCount(12);
expect(many).toBe(few);
// A ceiling, so a future caller that reintroduces per-element construction
// inside a scope fails here even if it happens to be element-count-flat.
expect(many).toBeLessThanOrEqual(6);
});
// The two scopes exist because `syncRuntimeMedia` can call `el.load()` on its
// seek-past-buffered-range retry, which synchronously resets `el.duration`.
// One scope spanning the media pass would serve the pre-`load()` duration to
// the visibility pass that runs after it.
it("resolves a post-load() duration in the visibility pass, not the cached pre-load() one", () => {
document.body.innerHTML =
`<div data-composition-id="main" data-root="true">` +
// `lead`'s duration is read (and cached) while the media cache is built,
// because `follower`'s start is expressed relative to it.
`<video id="lead" data-start="0"></video>` +
`<audio id="follower" data-start="lead + 0" data-duration="1"></audio>` +
`</div>`;
const lead = document.querySelector<HTMLVideoElement>("#lead")!;
const follower = document.querySelector<HTMLAudioElement>("#follower")!;
const leadDuration = stubDuration(lead, 10);
stubDuration(follower, 1);
window.__timelines = {};
initSandboxRuntimeModular();
// Stands in for the `el.load()` the seek-retry performs: the source turns
// out to be shorter than the metadata first reported.
mediaSpy.beforeSync = () => leadDuration.set(2);
window.__player?.renderSeek(2.5);
// Fresh read => follower starts at 2 and is on screen at 2.5.
// Stale cache => follower starts at 10 and is hidden.
expect(follower.style.visibility).toBe("visible");
});
});