fix(studio): full-size composition stills count their real memory, so the cache lets them go (#8576)

This commit is contained in:
Miguel Ángel
2026-11-23 10:41:23 +00:00
committed by GitHub
parent 96abfaf0d0
commit e238ab7fa9
6 changed files with 52 additions and 17 deletions
@@ -5,7 +5,12 @@ import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { MockResizeObserver, reportResize } from "../../hooks/resizeObserverTestUtils";
import { thumbnailScheduler } from "../lib/thumbnailScheduler";
import { buildCompositionThumbnailUrl, CompositionThumbnail } from "./CompositionThumbnail";
import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets";
import {
buildCompositionThumbnailUrl,
CompositionThumbnail,
compositionThumbnailRequest,
} from "./CompositionThumbnail";
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
configurable: true,
@@ -255,3 +260,30 @@ describe("CompositionThumbnail", () => {
expect(MockImage.instances[0]?.src).toBe("blob:composition-thumbnail");
});
});
describe("compositionThumbnailRequest", () => {
// fails without the fix: each full-size still was weighed as a 240x135 poster, so 96 of them (8.3 MB each decoded)
// stayed cached before the 64 MiB byte budget ever counted them full.
it("keeps no more full-size stills than the byte budget holds once their cards are gone", async () => {
let made = 0;
URL.createObjectURL = vi.fn(() => `blob:still-${++made}`);
const project = "/api/projects/demo/preview";
for (let write = 1; write <= 40; write++) {
const request = compositionThumbnailRequest(
`/api/projects/demo/thumbnail/index.html?v=${write}`,
project,
);
const lease = thumbnailScheduler.acquire(request, () => {});
await vi.waitFor(() => expect(MockImage.instances.length).toBe(write));
const probe = MockImage.instances[write - 1]!;
probe.naturalWidth = 1920;
probe.naturalHeight = 1080;
probe.onload?.();
await vi.waitFor(() => expect(thumbnailScheduler.getSnapshot(request).status).toBe("ready"));
lease.release();
}
const kept = made - vi.mocked(URL.revokeObjectURL).mock.calls.length;
const fit = Math.floor(TIMELINE_VIEWPORT_BUDGETS.thumbnailCacheBytes / (1920 * 1080 * 4));
expect(kept).toBeLessThanOrEqual(fit);
});
});
@@ -3,12 +3,12 @@ import { useThumbnailLease } from "../../hooks/useThumbnailLease";
import { useThumbnailStripSize } from "../../hooks/useThumbnailStripSize";
import {
createThumbnailKey,
decodedImageBytes,
type ThumbnailPriority,
type ThumbnailRequest,
} from "../lib/thumbnailScheduler";
import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets";
import { ThumbnailTiles } from "./ThumbnailTiles";
import { computeThumbnailStrip, probeImageAspect } from "./thumbnailUtils";
import { computeThumbnailStrip, probeImageSize } from "./thumbnailUtils";
import { studioApiFetch } from "../../utils/studioApiFetch";
interface CompositionThumbnailProps {
@@ -114,13 +114,10 @@ async function loadCompositionImage(url: string, signal: AbortSignal) {
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
const objectUrl = URL.createObjectURL(blob);
try {
const aspect = await probeImageAspect(objectUrl, signal);
const { width, height } = await probeImageSize(objectUrl, signal);
return {
value: { kind: "image" as const, url: objectUrl, aspect },
weight:
TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalWidth *
TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalHeight *
4,
value: { kind: "image" as const, url: objectUrl, aspect: width / height },
weight: decodedImageBytes(width, height),
dispose: () => URL.revokeObjectURL(objectUrl),
};
} catch (error) {
@@ -36,11 +36,11 @@ export function thumbnailFrameForTile(
* abort signal, so an unmounted clip cannot leave Blink retaining a pending
* image request and its former React tree.
*/
export function probeImageAspect(
export function probeImageSize(
imageSrc: string,
signal: AbortSignal,
tolerateSvgError = false,
): Promise<number> {
): Promise<{ width: number; height: number }> {
return new Promise((resolve, reject) => {
const image = new Image();
const cleanup = () => {
@@ -63,13 +63,13 @@ export function probeImageAspect(
cleanup();
resolve(
image.naturalWidth > 0 && image.naturalHeight > 0
? image.naturalWidth / image.naturalHeight
: 16 / 9,
? { width: image.naturalWidth, height: image.naturalHeight }
: { width: 1920, height: 1080 },
);
};
image.onerror = () => {
cleanup();
if (tolerateSvgError && /\.svg($|\?)/i.test(imageSrc)) resolve(16 / 9);
if (tolerateSvgError && /\.svg($|\?)/i.test(imageSrc)) resolve({ width: 1920, height: 1080 });
else reject(new Error("Image thumbnail failed to load"));
};
signal.addEventListener("abort", onAbort, { once: true });
@@ -1,4 +1,4 @@
import type { ThumbnailLoadedResult } from "./thumbnailScheduler";
import { decodedImageBytes, type ThumbnailLoadedResult } from "./thumbnailScheduler";
import { TIMELINE_VIEWPORT_BUDGETS } from "./timelineViewportBudgets";
function thumbnailSource(source: string): string {
@@ -66,7 +66,10 @@ export async function decodeImageThumbnail(
signal.throwIfAborted();
const width = image.naturalWidth || TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalWidth;
const height = image.naturalHeight || TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalHeight;
return { value: { kind: "image", url, aspect: width / height }, weight: width * height * 4 };
return {
value: { kind: "image", url, aspect: width / height },
weight: decodedImageBytes(width, height),
};
} finally {
image.src = "";
}
@@ -8,6 +8,8 @@ export type ThumbnailValue =
| { kind: "filmstrip"; urls: readonly string[]; aspect: number }
| { kind: "waveform"; peaks: readonly number[] };
export const decodedImageBytes = (width: number, height: number) => width * height * 4;
export interface ThumbnailLoadedResult {
value: ThumbnailValue;
/** Estimated decoded/object-URL bytes retained by this result. */
@@ -1,6 +1,7 @@
import { TIMELINE_VIEWPORT_BUDGETS, type TimelineViewportBudgets } from "./timelineViewportBudgets";
import {
createThumbnailKey,
decodedImageBytes,
type ThumbnailLoadedResult,
type ThumbnailValue,
} from "./thumbnailScheduler";
@@ -236,7 +237,7 @@ function loadedResult(
: { kind: "filmstrip", urls, aspect };
return {
value,
weight: width * height * 4 * decoded.length,
weight: decodedImageBytes(width, height) * decoded.length,
dispose: () => releaseDecodedResources(resources),
};
}