Skip to content

Commit 96861cb

Browse files
miguel-heygenCodex
andauthored
perf(studio-server): coordinate cancelable thumbnail generation (#2720)
* perf(studio): schedule adaptive timeline thumbnails * perf(studio): bound thumbnail decoding resources * perf(studio): virtualize timeline thumbnail media * perf(studio): prioritize timeline thumbnail work * perf(studio-server): coordinate cancelable thumbnail generation --------- Co-authored-by: Codex <codex@local>
1 parent 27d113e commit 96861cb

59 files changed

Lines changed: 3524 additions & 866 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
consumeFileWriteReceipt,
3333
getMimeType,
3434
type PreviewApiAdapter,
35+
thumbnailDeviceScaleFactor,
3536
type ResolvedProject,
3637
type RenderJobState,
3738
type BackgroundRemovalRender,
@@ -571,9 +572,18 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
571572
);
572573
}
573574
let page: import("puppeteer-core").Page | null = null;
575+
const closePage = () => void page?.close().catch(() => {});
576+
opts.signal.addEventListener("abort", closePage, { once: true });
574577
try {
575578
page = await session.browser.newPage();
576-
await page.setViewport({ width: opts.width || 1920, height: opts.height || 1080 });
579+
if (opts.signal.aborted) return null;
580+
const width = opts.width || 1920;
581+
const height = opts.height || 1080;
582+
await page.setViewport({
583+
width,
584+
height,
585+
deviceScaleFactor: thumbnailDeviceScaleFactor(opts),
586+
});
577587
await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 });
578588
await page
579589
.waitForFunction(
@@ -621,12 +631,15 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
621631
)) as Buffer;
622632
return screenshot;
623633
} catch (err) {
624-
console.warn(
625-
"[Studio] Thumbnail generation failed:",
626-
err instanceof Error ? err.message : err,
627-
);
634+
if (!opts.signal.aborted) {
635+
console.warn(
636+
"[Studio] Thumbnail generation failed:",
637+
err instanceof Error ? err.message : err,
638+
);
639+
}
628640
return null;
629641
} finally {
642+
opts.signal.removeEventListener("abort", closePage);
630643
await page?.close().catch(() => {});
631644
}
632645
},
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, it } from "vitest";
2+
import { thumbnailDeviceScaleFactor } from "./thumbnailOutput";
3+
4+
describe("thumbnailDeviceScaleFactor", () => {
5+
it("preserves source-density captures and bounds landscape and portrait previews", () => {
6+
expect(
7+
thumbnailDeviceScaleFactor({
8+
width: 1920,
9+
height: 1080,
10+
outputWidth: 1920,
11+
outputHeight: 1080,
12+
}),
13+
).toBe(1);
14+
expect(
15+
thumbnailDeviceScaleFactor({
16+
width: 1920,
17+
height: 1080,
18+
outputWidth: 240,
19+
outputHeight: 135,
20+
}),
21+
).toBe(0.125);
22+
expect(
23+
thumbnailDeviceScaleFactor({
24+
width: 1080,
25+
height: 1920,
26+
outputWidth: 76,
27+
outputHeight: 135,
28+
}),
29+
).toBeCloseTo(76 / 1080);
30+
});
31+
32+
it("rejects invalid dimensions instead of silently changing layout", () => {
33+
expect(() =>
34+
thumbnailDeviceScaleFactor({ width: 0, height: 1080, outputWidth: 240, outputHeight: 135 }),
35+
).toThrow(RangeError);
36+
});
37+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface ThumbnailOutputDimensions {
2+
width: number;
3+
height: number;
4+
outputWidth: number;
5+
outputHeight: number;
6+
}
7+
8+
/** Sole adapter rule for capturing authored layout at bounded physical dimensions. */
9+
export function thumbnailDeviceScaleFactor({
10+
width,
11+
height,
12+
outputWidth,
13+
outputHeight,
14+
}: ThumbnailOutputDimensions): number {
15+
const dimensions = [width, height, outputWidth, outputHeight];
16+
if (dimensions.some((value) => !Number.isFinite(value) || value <= 0)) {
17+
throw new RangeError("Thumbnail dimensions must be positive finite numbers");
18+
}
19+
return Math.min(1, outputWidth / width, outputHeight / height);
20+
}

‎packages/studio-server/src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export {
2020
} from "./helpers/fileVersion.js";
2121
export { buildSubCompositionHtml } from "./helpers/subComposition.js";
2222
export { getElementScreenshotClip, type ScreenshotClip } from "./helpers/screenshotClip.js";
23+
export {
24+
thumbnailDeviceScaleFactor,
25+
type ThumbnailOutputDimensions,
26+
} from "./helpers/thumbnailOutput.js";
2327
export {
2428
createBackgroundRemovalJob,
2529
type BackgroundRemovalRender,

‎packages/studio-server/src/routes/thumbnail.test.ts‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
22
import { Hono } from "hono";
3-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import {
4+
existsSync,
5+
mkdirSync,
6+
mkdtempSync,
7+
readdirSync,
8+
rmSync,
9+
truncateSync,
10+
utimesSync,
11+
writeFileSync,
12+
} from "node:fs";
413
import { tmpdir } from "node:os";
514
import { join } from "node:path";
6-
import { registerThumbnailRoutes } from "./thumbnail";
15+
import { pruneThumbnailCache, registerThumbnailRoutes } from "./thumbnail";
716
import type { StudioApiAdapter } from "../types";
817

918
const tempProjectDirs: string[] = [];
@@ -52,10 +61,37 @@ describe("registerThumbnailRoutes", () => {
5261
seekTime: 1.2,
5362
selector: "#title-card",
5463
format: "jpeg",
64+
outputWidth: 240,
65+
outputHeight: 135,
66+
signal: expect.any(AbortSignal),
5567
}),
5668
);
5769
});
5870

71+
it("deduplicates concurrent generation and writes one complete cache entry", async () => {
72+
const adapter = createAdapter();
73+
const project = await adapter.resolveProject("demo");
74+
if (!project) throw new Error("missing project");
75+
let resolve!: (buffer: Buffer) => void;
76+
const generated = new Promise<Buffer>((done) => (resolve = done));
77+
adapter.generateThumbnail = vi.fn(async () => generated);
78+
const app = new Hono();
79+
registerThumbnailRoutes(app, adapter);
80+
81+
const url = "http://localhost/projects/demo/thumbnail/index.html?t=3";
82+
const first = app.request(url);
83+
const second = app.request(url);
84+
await vi.waitFor(() => expect(adapter.generateThumbnail).toHaveBeenCalledTimes(1));
85+
resolve(Buffer.from("shared"));
86+
87+
expect(await (await first).text()).toBe("shared");
88+
expect(await (await second).text()).toBe("shared");
89+
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(1);
90+
const cached = readdirSync(join(project.dir, ".thumbnails"));
91+
expect(cached).toHaveLength(1);
92+
expect(cached[0]).not.toContain(".tmp");
93+
});
94+
5995
it("forwards png capture requests and returns a png content type", async () => {
6096
const adapter = createAdapter();
6197
const app = new Hono();
@@ -72,10 +108,27 @@ describe("registerThumbnailRoutes", () => {
72108
compPath: "compositions/intro.html",
73109
seekTime: 2,
74110
format: "png",
111+
outputWidth: 1920,
112+
outputHeight: 1080,
75113
}),
76114
);
77115
});
78116

117+
it("allows png callers to opt into bounded preview output", async () => {
118+
const adapter = createAdapter();
119+
const app = new Hono();
120+
registerThumbnailRoutes(app, adapter);
121+
122+
const response = await app.request(
123+
"http://localhost/projects/demo/thumbnail/index.html?format=png&output=preview",
124+
);
125+
126+
expect(response.status).toBe(200);
127+
expect(adapter.generateThumbnail).toHaveBeenCalledWith(
128+
expect.objectContaining({ outputWidth: 240, outputHeight: 135 }),
129+
);
130+
});
131+
79132
it("preserves an explicit zero seek time", async () => {
80133
const adapter = createAdapter();
81134
const app = new Hono();
@@ -220,4 +273,26 @@ describe("registerThumbnailRoutes", () => {
220273

221274
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2);
222275
});
276+
277+
it("prunes expired and over-budget files without touching protected work", () => {
278+
const cacheDir = mkdtempSync(join(tmpdir(), "hf-thumbnail-cache-test-"));
279+
tempProjectDirs.push(cacheDir);
280+
const expiredPath = join(cacheDir, "expired.jpg");
281+
const protectedPath = join(cacheDir, "protected.jpg");
282+
const overflowPath = join(cacheDir, "overflow.jpg");
283+
writeFileSync(expiredPath, "expired");
284+
writeFileSync(protectedPath, "protected");
285+
writeFileSync(overflowPath, "overflow");
286+
const now = Date.now();
287+
const expiredSeconds = (now - 15 * 24 * 60 * 60 * 1000) / 1000;
288+
utimesSync(expiredPath, expiredSeconds, expiredSeconds);
289+
truncateSync(protectedPath, 400 * 1024 * 1024);
290+
truncateSync(overflowPath, 200 * 1024 * 1024);
291+
292+
pruneThumbnailCache(cacheDir, new Set([protectedPath]), now);
293+
294+
expect(existsSync(expiredPath)).toBe(false);
295+
expect(existsSync(protectedPath)).toBe(true);
296+
expect(existsSync(overflowPath)).toBe(false);
297+
});
223298
});

0 commit comments

Comments
 (0)