Files
hyperframes/packages/core/src/runtime/stackingContext.ts
T
Vance IngallsandClaude Opus 5.5 d05bc4215d perf(core): preview runtime scales with what plays, not with the film's clip count (#5674)
* perf(core): probe media volume with one timeline walk per pass, not one per element

Each bind, rebind and scene-swap pass probes every media element for volume
automation, and each probe flattened the whole captured timeline
(getChildren(true, true, false)) to ask whether any tween could move that one
element's volume. A pass cost elements x tweens. Elements with no automation are
never cached, so the cost came back on every rebind pass.

On a two-hour podcast episode (2,354 media clips, ~100k caption tweens) that was
several seconds of preview main thread per pass (CPU profile of the project's
open in Desktop: IE/PE -> getChildren, ~6 s).

volumeMoversOf() now walks the tweens once and lists the elements a tween names
volume on, plus whether any non-DOM setter is tweened. Each pass builds it lazily
and shares it with every probe; a probe without one walks as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(core): find the master bus from the first composition root

The top-level root is the first [data-composition-id] in document order, so
one match replaces a scan of every composition node, read once per clip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(core): lay out a clip again only when its layout inputs change

applyClipLayout ran on every timeline post and, for every root clip, lifted
display:none, read computed style and restored it. A clip whose inline
layout, class and data-width/height are as last laid out gets the same
answer, so it is skipped. On a 2,354-clip episode that was 4 of every 10 s
of playback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(core): a clip is laid out again only when its layout inputs change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(core): play a long film without re-walking it every frame

- The visibility pass skips root-level media far from the playhead during
  steady playback: hidden last pass, not loading or armed, and its window
  (widened by every look-ahead) clear of the span played and the loop start.
  Seeks, forced syncs and renders visit every node.
- The GSAP re-render saves and restores only the landing marks near the
  playhead, placed once per timeline structure; the zero-duration-callback
  check is cached the same way. A caption track of 38k sets was walked twice
  a frame.
- Registered timeline hosts, the root composition and the per-frame
  whole-film queries ([data-start], audio groups, timed audio) are kept for
  one timing revision; a render reads fresh.
- The runtime's own media release (src reset) and load events of clips with
  an authored length no longer count as timing changes, which re-posted the
  timeline about twice a second.
- The timeline payload resolves a stacking context once per parent.

2,354-clip episode in Desktop: playback 15 -> 67-89 fps (unminified runtime).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(core): check each element once per batch of raster-hint records, and cache the root from init

- A script building a large tree (a caption track) made each attribute record
  re-walk its subtree with getComputedStyle; a batch now checks each element
  once, an element whose own attributes changed still reading its own hints.
- The root composition cache installs the timing observer itself: a root
  with a declared length never reads the duration floors that installed it,
  so init looked the root up once per media clip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(core): type the playback visibility skip for any element, and cache clip layout only once loaded

The skip's predicate took an HTMLElement where the pass hands it a
StylableElement (tsconfig.runtime.json). The layout cache records a clip only
once the document has loaded, so a pass before stylesheets apply is redone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(core): realm-safe element checks in the timing caches; cut added comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 18:33:19 +00:00

123 lines
4.5 KiB
TypeScript

import { PREVIEW_RASTER_ATTR } from "../studioPreviewMark";
export const ROOT_CSS_STACKING_CONTEXT_ID = "css:root";
const NON_NONE_STACKING_CONTEXT_PROPERTIES = [
"backdrop-filter",
"clip-path",
"filter",
"mask",
"mask-border-source",
"mask-image",
"perspective",
"rotate",
"scale",
"transform",
"translate",
"-webkit-mask-image",
] as const;
const STACKING_CONTEXT_WILL_CHANGE_PROPERTIES = new Set([
...NON_NONE_STACKING_CONTEXT_PROPERTIES,
"contain",
"isolation",
"mask-border",
"mix-blend-mode",
"opacity",
]);
const Z_INDEXED_POSITIONS = new Set(["absolute", "relative"]);
const Z_INDEXED_PARENT_DISPLAYS = new Set(["flex", "inline-flex", "grid", "inline-grid"]);
const CONTAINMENT_VALUES = new Set(["layout", "paint", "strict", "content"]);
const STACKING_CONTAINER_TYPES = new Set(["size", "inline-size"]);
function createsPositionedStackingContext(style: CSSStyleDeclaration): boolean {
return style.position === "fixed" || style.position === "sticky";
}
function createsZIndexedStackingContext(element: HTMLElement, style: CSSStyleDeclaration): boolean {
if (style.zIndex === "auto" || style.zIndex === "") return false;
if (Z_INDEXED_POSITIONS.has(style.position)) return true;
const parentDisplay = element.parentElement
? element.ownerDocument.defaultView?.getComputedStyle(element.parentElement).display
: null;
return parentDisplay != null && Z_INDEXED_PARENT_DISPLAYS.has(parentDisplay);
}
function createsVisualEffectStackingContext(style: CSSStyleDeclaration): boolean {
const opacity = Number.parseFloat(style.opacity);
if (Number.isFinite(opacity) && opacity < 1) return true;
if (style.getPropertyValue("isolation") === "isolate") return true;
const mixBlendMode = style.getPropertyValue("mix-blend-mode");
if (mixBlendMode && mixBlendMode !== "normal") return true;
return NON_NONE_STACKING_CONTEXT_PROPERTIES.some((property) => {
const value = style.getPropertyValue(property);
return value !== "" && value !== "none";
});
}
function createsContainmentStackingContext(style: CSSStyleDeclaration): boolean {
const contain = style.getPropertyValue("contain").split(/\s+/);
if (contain.some((value) => CONTAINMENT_VALUES.has(value))) return true;
return STACKING_CONTAINER_TYPES.has(style.getPropertyValue("container-type"));
}
function createsWillChangeStackingContext(style: CSSStyleDeclaration): boolean {
return style
.getPropertyValue("will-change")
.split(",")
.map((property) => property.trim())
.some((property) => STACKING_CONTEXT_WILL_CHANGE_PROPERTIES.has(property));
}
function createsCssStackingContext(element: HTMLElement, style: CSSStyleDeclaration): boolean {
if (element.hasAttribute(PREVIEW_RASTER_ATTR)) return true;
if (createsPositionedStackingContext(style)) return true;
if (createsZIndexedStackingContext(element, style)) return true;
if (createsVisualEffectStackingContext(style)) return true;
if (createsContainmentStackingContext(style)) return true;
return createsWillChangeStackingContext(style);
}
/** Stable within one DOM revision and unique even when authors duplicate ids. */
function cssStackingContextPath(element: Element): string {
const indexes: number[] = [];
let cursor: Element | null = element;
while (cursor?.parentElement) {
indexes.push(Array.prototype.indexOf.call(cursor.parentElement.children, cursor));
cursor = cursor.parentElement;
}
return `css:${indexes.reverse().join(".")}`;
}
/**
* Leaf z-index participates in the nearest ANCESTOR stacking context. Exclude
* the leaf itself: an element that establishes a context still compares its own
* z-index with siblings in its parent's context.
*/
export function resolveCssStackingContextId(
node: Element,
byParent?: Map<Element | null, string>,
): string {
const known = byParent?.get(node.parentElement);
if (known !== undefined) return known;
const id = nearestCssStackingContextId(node);
byParent?.set(node.parentElement, id);
return id;
}
function nearestCssStackingContextId(node: Element): string {
const win = node.ownerDocument.defaultView;
if (!win) return ROOT_CSS_STACKING_CONTEXT_ID;
let cursor = node.parentElement;
while (cursor && cursor !== node.ownerDocument.documentElement) {
try {
if (createsCssStackingContext(cursor, win.getComputedStyle(cursor))) {
return cssStackingContextPath(cursor);
}
} catch {
return ROOT_CSS_STACKING_CONTEXT_ID;
}
cursor = cursor.parentElement;
}
return ROOT_CSS_STACKING_CONTEXT_ID;
}