Skip to content

Open source CapCut alternative.

TypeScript

perf(site): live decoders are budgeted by source pixels

David Lec17875faba14
diff --git a/site/src/cut/lib/frameSource.test.ts b/site/src/cut/lib/frameSource.test.ts
index 0728e5d7..6699554b 100644
--- a/site/src/cut/lib/frameSource.test.ts
+++ b/site/src/cut/lib/frameSource.test.ts
@@ -367,6 +367,28 @@ describe("FrameSourcePool", () => {
expect(pool.get("old", asset("a"), 480)).toBe(old);
});
+ test("idle decoders stand down by the pixels they cover", () => {
+ const uhd = (id: string) => asset(id, 3840, 2160);
+ const hd = (id: string) => asset(id, 1920, 1080);
+ const ids = ["a", "b", "c", "d", "e", "f"];
+ const pool = new FrameSourcePool(12);
+ const big = ids.map((id) => {
+ pool.beginFrame();
+ return pool.get(id, uhd(id), 480);
+ });
+ const small = ids.map((id) => {
+ pool.beginFrame();
+ return pool.get(`${id}-hd`, hd(id), 480);
+ });
+ for (let i = 0; i < 200; i++) pool.beginFrame();
+ pool.evict();
+ // Twelve decoders are inside the count. The six 1080p files are read
+ // most recently and fit the pixel budget together; behind them the 4K
+ // files fill what is left, and the oldest stand down.
+ expect(small.map((s) => s.suspended)).toEqual(ids.map(() => false));
+ expect(big.map((s) => s.suspended)).toEqual([true, true, true, true, false, false]);
+ });
+
test("standing down costs a decoder, not a source", () => {
const pool = new FrameSourcePool(1);
pool.beginFrame();
diff --git a/site/src/cut/lib/frameSource.ts b/site/src/cut/lib/frameSource.ts
index 49551dde..2d789829 100644
--- a/site/src/cut/lib/frameSource.ts
+++ b/site/src/cut/lib/frameSource.ts
@@ -293,6 +293,18 @@ const WARM_PIXELS = 96e6;
/** Stood-down sources kept regardless of the pixel budget, so a project of
* tiny frames cannot leave hundreds of parsed files open. */
const WARM_KEEP_MAX = 32;
+/**
+ * Source pixels the live decoders may cover between them.
+ *
+ * A decoder costs the file's own frame size, whatever size the picture is
+ * drawn at: the hardware decodes every frame at full resolution and holds a
+ * dozen or so of them between the file and the canvas. At 4K that is hundreds
+ * of megabytes a decoder, and a dozen of them is most of a laptop's memory.
+ * So under the decoder count, idle decoders also stand down by the pixels
+ * they cover, most recently read kept first. The budget fits the whole count
+ * at 1080p and holds four at 4K.
+ */
+const LIVE_PIXELS = 34e6;
/** A failed open tries again this much later, growing per attempt over the
* first `RETRIES` tries, so a network blip heals within seconds. */
const RETRY_MS = 1000;
@@ -639,6 +651,13 @@ export class ClipFrameSource {
return POOL * width * this.height;
}
+ /** Pixels in a frame of the file, which is what its decoder's held frames
+ * cost whatever size the picture is drawn at. */
+ get decodePixels(): number {
+ if (this.asset.type === "image") return 0;
+ return (this.asset.width ?? 1920) * (this.asset.height ?? 1080);
+ }
+
/** The URL this source is reading. The pool compares it against the store's
* current one, so a re-minted signed URL replaces the source under it. */
get url(): string {
@@ -1738,6 +1757,17 @@ export class FrameSourcePool {
live--;
}
+ // Then by pixels: the live decoders are kept most recently read first
+ // until the source budget runs out, and the idle ones past it stand down.
+ let livePixels = 0;
+ for (const [id, src] of [...this.sources.entries()].sort((a, b) => b[1].touched - a[1].touched)) {
+ if (src.suspended) continue;
+ livePixels += src.decodePixels;
+ if (livePixels <= LIVE_PIXELS || this.tick - src.touched < EVICT_GRACE) continue;
+ poolLog(`pool suspend ${id} (pixels)`);
+ src.suspend();
+ }
+
// Then the memory. The warm shelf is filled most-recently-read first until
// the canvas budget runs out, and what does not fit closes for real. Only
// stood-down sources are on the shelf, so nothing holding a decoder — and