Skip to content

Commit e38585e

Browse files
vanceingallsclaude
andauthored
perf(studio): timeline thumbnails come back from disk in the next session (#6676)
* perf(studio): timeline thumbnails come back from disk in the next session Reopening a project re-read every video's index and re-decoded every strip: the frame cache and the source facts lived in memory, keyed by the session. Now each source's facts (aspect, duration) and every decoded frame are kept in Cache Storage under the file's ETag, the version the server says it is. A new session checks each source once with a conditional one-byte read: a 304 means the stored frames are still that file's, and a strip they cover paints without opening a reader. A new ETag drops what was stored for the old one. Sources are dropped whole, least recently used first, past 256 MiB. A store that is missing or fails (private window, quota) warns once and the strip decodes as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): a reopened project checks its videos again, and the disk index counts a frame once The stored version a session validated was kept per video for the whole page, so reopening a project after one of its videos was replaced at the same path painted the old file's frames until a full reload. It is now kept per video and project open (its contentVersion): each open re-validates once. The index now counts a frame stored again under its name once, and merges with what other tabs saved before writing, keeping caches this tab dropped out, so the 256 MiB budget holds across tabs. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 67c7ec2 commit e38585e

6 files changed

Lines changed: 556 additions & 6 deletions

File tree

‎packages/studio/src/player/lib/mediaFetch.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
177177
const failuresByStart = new Map<number | null, number>();
178178
const inFlightReads = new AbortController();
179179
let ranges: "unknown" | "readable" | "hidden" = reader === "thumbnail" ? "unknown" : "hidden";
180+
let etag: string | undefined;
181+
const noteEtag = (response: Response) =>
182+
void (etag ??= response.headers.get("ETag") ?? undefined);
180183
const fetchFn = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
181184
const sent = ranges === "readable" ? boundedRange(init) : init;
182185
const asked = Number(rangeStart(sent, true) ?? 0);
@@ -187,6 +190,7 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
187190
);
188191
const response = await typedFetch(reader, input, sent ?? {}, asked, request);
189192
if (ranges !== "hidden") ranges = rangesReadable(sent, response) ? "readable" : "hidden";
193+
noteEtag(response);
190194
return typedResponse(response, asked, request);
191195
};
192196
const getRetryDelay = (_previousAttempts: number, error: unknown): number | null => {
@@ -201,8 +205,9 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
201205
};
202206
const cancelReads = () =>
203207
inFlightReads.abort(new DOMException("Media reads cancelled", "AbortError"));
204-
if (reader === "probe") return { fetchFn, getRetryDelay, cancelReads };
205-
return { fetchFn, getRetryDelay, cancelReads, maxCacheSize: POOLED_READER_CACHE_BYTES };
208+
const etagOf = () => etag;
209+
if (reader === "probe") return { fetchFn, getRetryDelay, cancelReads, etagOf };
210+
return { fetchFn, getRetryDelay, cancelReads, etagOf, maxCacheSize: POOLED_READER_CACHE_BYTES };
206211
}
207212

208213
export function reportMediaFetchFailure(error: unknown): void {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// @vitest-environment happy-dom
2+
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
readStoredFrame,
6+
readStoredInfo,
7+
rememberVersion,
8+
resetStoredThumbnailsForTests,
9+
STORED_THUMBNAIL_BYTES,
10+
storedVersion,
11+
storeFrame,
12+
storeInfo,
13+
} from "./thumbnailDiskCache";
14+
15+
/** Cache Storage as a Map of Maps: what a browser keeps across sessions. */
16+
function fakeCacheStorage() {
17+
const stores = new Map<string, Map<string, Response>>();
18+
const storage = {
19+
open: async (name: string) => {
20+
const store = stores.get(name) ?? new Map<string, Response>();
21+
stores.set(name, store);
22+
return {
23+
match: async (url: string) => store.get(String(url))?.clone(),
24+
put: async (url: string, response: Response) => void store.set(String(url), response),
25+
};
26+
},
27+
has: async (name: string) => stores.has(name),
28+
keys: async () => [...stores.keys()],
29+
delete: async (name: string) => stores.delete(name),
30+
};
31+
return { storage, stores };
32+
}
33+
34+
const v1 = { source: "/media/take.mp4", etag: '"one"' };
35+
const v2 = { source: "/media/take.mp4", etag: '"two"' };
36+
// a picture that reports `bytes` without holding them, so the budget test needs no real 128 MB
37+
function jpeg(bytes = 5): Blob {
38+
const blob = new Blob([new Uint8Array(Math.min(bytes, 5))], { type: "image/jpeg" });
39+
return Object.defineProperty(blob, "size", { value: bytes });
40+
}
41+
42+
let stores: Map<string, Map<string, Response>>;
43+
44+
beforeEach(() => {
45+
resetStoredThumbnailsForTests();
46+
localStorage.clear();
47+
const fake = fakeCacheStorage();
48+
stores = fake.stores;
49+
vi.stubGlobal("caches", fake.storage);
50+
});
51+
52+
afterEach(() => {
53+
vi.unstubAllGlobals();
54+
vi.restoreAllMocks();
55+
});
56+
57+
describe("stored thumbnails", () => {
58+
it("keep a source's facts and each frame with the time it was decoded at", async () => {
59+
await storeInfo(v1, { aspect: 16 / 9, metadataDuration: 30 });
60+
await storeFrame(v1, "cover 320x180 2.5", 2.4, jpeg());
61+
62+
expect(await readStoredInfo(v1)).toEqual({ aspect: 16 / 9, metadataDuration: 30 });
63+
const frame = await readStoredFrame(v1, "cover 320x180 2.5");
64+
expect(frame?.decodedAt).toBe(2.4);
65+
expect(frame?.blob.size).toBe(5);
66+
expect(await readStoredFrame(v1, "cover 320x180 5")).toBeUndefined();
67+
expect(await readStoredFrame(v2, "cover 320x180 2.5")).toBeUndefined();
68+
});
69+
70+
it("are trusted in a new session only when the server answers the stored ETag with a 304", async () => {
71+
const fetch = vi.fn(async () => new Response(null, { status: 304 }));
72+
vi.stubGlobal("fetch", fetch);
73+
expect(await storedVersion(v1.source)).toBeUndefined();
74+
expect(fetch).not.toHaveBeenCalled();
75+
76+
await rememberVersion(v1);
77+
expect(await storedVersion(v1.source)).toEqual(v1);
78+
expect(fetch).toHaveBeenCalledWith(v1.source, {
79+
headers: { Range: "bytes=0-0", "If-None-Match": '"one"' },
80+
});
81+
fetch.mockResolvedValueOnce(new Response("x", { status: 206 }));
82+
expect(await storedVersion(v1.source)).toBeUndefined();
83+
});
84+
85+
it("drop what was stored for a file's earlier version once a new one is read", async () => {
86+
await rememberVersion(v1);
87+
await storeFrame(v1, "cover 320x180 1", 1, jpeg());
88+
await rememberVersion(v2);
89+
90+
expect(await readStoredFrame(v1, "cover 320x180 1")).toBeUndefined();
91+
expect(stores.size).toBe(0);
92+
});
93+
94+
it("drop the least recently used sources once they pass the budget", async () => {
95+
const big = Math.floor(STORED_THUMBNAIL_BYTES / 2) + 1;
96+
const others = ["/a.mp4", "/b.mp4", "/c.mp4"].map((source) => ({ source, etag: '"1"' }));
97+
for (const version of others) {
98+
await storeFrame(version, "cover 1x1 0", 0, jpeg(big));
99+
vi.spyOn(Date, "now").mockReturnValue(Date.now() + 1000);
100+
}
101+
102+
expect(await readStoredFrame(others[0]!, "cover 1x1 0")).toBeUndefined();
103+
expect(await readStoredFrame(others[2]!, "cover 1x1 0")).toBeDefined();
104+
});
105+
106+
it("never fail a thumbnail: a broken store warns once and reads as empty", async () => {
107+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
108+
vi.stubGlobal("caches", {
109+
open: async () => {
110+
throw new DOMException("quota", "QuotaExceededError");
111+
},
112+
});
113+
114+
expect(await readStoredInfo(v1)).toBeUndefined();
115+
expect(await storeFrame(v1, "cover 1x1 0", 0, jpeg())).toBeUndefined();
116+
expect(warn).toHaveBeenCalledTimes(1);
117+
});
118+
119+
it("are skipped where the browser offers no Cache Storage", async () => {
120+
vi.stubGlobal("caches", undefined);
121+
expect(await readStoredInfo(v1)).toBeUndefined();
122+
expect(await storeInfo(v1, { aspect: 1, metadataDuration: 1 })).toBeUndefined();
123+
});
124+
125+
it("count a frame stored again under its name once, so the budget holds", async () => {
126+
await storeFrame(v1, "cover 1x1 0", 0, jpeg(100));
127+
await storeFrame(v1, "cover 1x1 0", 0, jpeg(100));
128+
129+
await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), {
130+
timeout: 3000,
131+
});
132+
const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
133+
expect(Object.values(saved.caches)).toMatchObject([{ bytes: 100 }]);
134+
});
135+
136+
it("keep what another tab stored when saving, but not what this tab dropped", async () => {
137+
await rememberVersion(v1);
138+
await storeFrame(v1, "cover 1x1 0", 0, jpeg());
139+
await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), {
140+
timeout: 3000,
141+
});
142+
// another tab saves its snapshot: a source of its own, and v1 as it was before this tab moved on
143+
const other = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
144+
other.caches["hf-thumbnails:other:tab"] = { usedAt: Date.now(), bytes: 7 };
145+
localStorage.setItem("hf-thumbnails-index", JSON.stringify(other));
146+
const v1Cache = Object.keys(other.caches).find((name) => name !== "hf-thumbnails:other:tab")!;
147+
await rememberVersion(v2);
148+
await storeFrame(v2, "cover 1x1 0", 0, jpeg());
149+
150+
await vi.waitFor(
151+
() => expect(localStorage.getItem("hf-thumbnails-index")).not.toContain(v1Cache),
152+
{ timeout: 3000 },
153+
);
154+
const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
155+
expect(Object.keys(saved.caches)).toContain("hf-thumbnails:other:tab");
156+
expect(Object.keys(saved.caches)).toHaveLength(2);
157+
});
158+
});

0 commit comments

Comments
 (0)