perf(studio): a long timeline no longer lays itself out on every render while it settles (#6855)

* perf(studio): a long timeline no longer lays itself out on every render while it settles

* perf(studio): compare row data with sameData and drop the added comments
This commit is contained in:
Miguel Ángel
2026-10-30 18:30:14 +00:00
committed by GitHub
parent e342a90991
commit 9b0df610cc
4 changed files with 95 additions and 2 deletions
@@ -0,0 +1,41 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { expect, it, vi } from "vitest";
import { useSettledScrollLeft } from "./useSettledScrollLeft";
import { TIMELINE_SCROLL_SETTLE_MS } from "./useTimelineScrollViewport";
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
// The ruler's label read scrollLeft on every commit while a long project settled, each read a layout of the whole
// timeline.
it("reads scrollLeft when a scroll settles, never on a render", () => {
vi.useFakeTimers();
const scroll = document.createElement("div");
let reads = 0;
let left = 120;
Object.defineProperty(scroll, "scrollLeft", { get: () => (reads++, left) });
const scrollRef = { current: scroll };
const seen: (number | null)[] = [];
function Probe({ tick }: { tick: number }) {
seen.push(useSettledScrollLeft(scrollRef));
return <i data-tick={tick} />;
}
const root = createRoot(document.createElement("div"));
try {
act(() => root.render(<Probe tick={0} />));
expect(seen.at(-1)).toBe(120);
reads = 0;
for (let tick = 1; tick <= 3; tick += 1) act(() => root.render(<Probe tick={tick} />));
expect(reads, "no layout read on a render").toBe(0);
left = 300;
act(() => void scroll.dispatchEvent(new Event("scroll")));
expect(seen.at(-1)).toBeNull();
act(() => void vi.advanceTimersByTime(TIMELINE_SCROLL_SETTLE_MS + 1));
expect(seen.at(-1)).toBe(300);
expect(reads, "one read when the scroll settles").toBe(1);
} finally {
act(() => root.unmount());
vi.useRealTimers();
}
});
@@ -6,9 +6,12 @@ import { TIMELINE_SCROLL_SETTLE_MS } from "./useTimelineScrollViewport";
// on a large unvirtualized timeline.
export function useSettledScrollLeft(scrollRef: RefObject<HTMLDivElement | null>): number | null {
const scrollingRef = useRef(false);
const settledRef = useRef(0);
const subscribe = useCallback(
(onChange: () => void) => {
const el = scrollRef.current;
settledRef.current = el?.scrollLeft ?? 0;
onChange();
let settle: ReturnType<typeof setTimeout> | undefined;
const onScroll = () => {
if (!scrollingRef.current) {
@@ -18,6 +21,7 @@ export function useSettledScrollLeft(scrollRef: RefObject<HTMLDivElement | null>
clearTimeout(settle);
settle = setTimeout(() => {
scrollingRef.current = false;
settledRef.current = el?.scrollLeft ?? 0;
onChange();
}, TIMELINE_SCROLL_SETTLE_MS);
};
@@ -32,7 +36,7 @@ export function useSettledScrollLeft(scrollRef: RefObject<HTMLDivElement | null>
);
return useSyncExternalStore(
subscribe,
() => (scrollingRef.current ? null : (scrollRef.current?.scrollLeft ?? 0)),
() => (scrollingRef.current ? null : settledRef.current),
() => null,
);
}
@@ -54,3 +54,48 @@ it("leaves the viewport still when a live preview lane is removed before the com
expect(scroll.scrollTop).toBe(160);
act(() => root.unmount());
});
// A long project settling hands the rows a new geometry with the same rows on many commits; each scrollTop read
// forced a layout of every clip.
it("reads no scroll position for a new geometry that places every row where it was", () => {
const scroll = document.createElement("div");
let reads = 0;
let top = 160;
Object.defineProperty(scroll, "scrollTop", {
get: () => (reads++, top),
set: (value: number) => void (top = value),
});
const scrollRef = { current: scroll };
const root = createRoot(document.createElement("div"));
function Probe({ geometry }: { geometry: TimelineRowGeometry }) {
useTimelineRowVirtualization({
scrollRef,
rowGeometry: geometry,
sessionEpoch: 0,
viewport: {
scrollTop: 160,
scrollLeft: 0,
clientWidth: 800,
clientHeight: 200,
scrollWidth: 800,
scrollHeight: 600,
isScrolling: false,
},
elements: [],
selectedElementId: null,
lastScrollLeftRef: { current: 0 },
syncScrollViewport: () => {},
});
return null;
}
const rows = () => createTimelineRowGeometry([0, 1, 2], [48, 48, 48]);
act(() => root.render(<Probe geometry={rows()} />));
reads = 0;
for (let i = 0; i < 3; i += 1) act(() => root.render(<Probe geometry={rows()} />));
expect(reads, "no layout read for the same rows").toBe(0);
act(() =>
root.render(<Probe geometry={createTimelineRowGeometry([0, 9, 1, 2], [48, 48, 48, 48])} />),
);
expect(reads, "a row added still keeps the view anchored").toBeGreaterThan(0);
act(() => root.unmount());
});
@@ -8,6 +8,7 @@ import {
type RefObject,
} from "react";
import type { TimelineElement } from "../store/playerStore";
import { sameData } from "../../utils/sameData";
import { resolveTimelineFocusIdentity } from "./timelineFocusIdentity";
import { getTimelineScrollTopForGeometryChange } from "./timelineViewportGeometry";
import type { TimelineRowGeometry } from "./timelineLayout";
@@ -39,6 +40,8 @@ function getFocusedTimelineRowKey(target: EventTarget | null): number | undefine
return Number.isFinite(rowKey) ? rowKey : undefined;
}
const rowTops = (g: TimelineRowGeometry) => [g.rowKeys, g.rowOffsets, g.padding.top];
export function useTimelineRowVirtualization({
scrollRef,
viewport,
@@ -116,7 +119,7 @@ export function useTimelineRowVirtualization({
scroll.scrollTop = 0;
syncScrollViewport(scroll);
}
} else if (scroll && previousGeometry !== rowGeometry) {
} else if (scroll && !sameData(rowTops(previousGeometry), rowTops(rowGeometry))) {
const nextScrollTop = getTimelineScrollTopForGeometryChange(
previousGeometry,
rowGeometry,