// THE CHROME CAPTURE POOL: one budget of renderer workers that a build's // chrome layers share, so the layers capture side by side instead of one // after another. // // Every layer (the deck, the pair strip, the stamps, each chapter card's // window …) is a HyperFrames render of its own, and each pays a fixed start // (the CLI, a browser, a page load per worker) before its first frame. One at // a time, a cut with fifteen small layers paid fifteen starts in a row while // most of the machine's cores sat idle behind a 2-worker render. Now // build-video starts the layers together (`member()` each) and each asks this // pool for workers right before its renderer runs (`lease(frames)`), sized by // the frames it will actually capture -- compose-chrome's frame keys: its // DISTINCT frames, not its length: // // - a lease wants one worker per FRAMES_PER_WORKER frames, at least one, // at most the budget; // - nothing is granted while a started layer is still composing (its frame // count unknown): the deck, composed first but slowest to key, is never // starved by the small layers that asked before it; // - then the largest asks are served first, each leaving one worker for // every other ask that could start beside it, so the small layers run // beside the big one instead of after it; // - at most `maxActive` renders run at once (each is a browser of its own: // memory, render-estimate.mjs). // // Pure: no I/O, no timers. build-video makes one per renderDeck. /** Frames a worker should have to itself before another is worth its start. */ export const FRAMES_PER_WORKER = 120; /** * @param {{ budget: number, maxActive?: number, perWorker?: number }} o * `budget` the workers in all; `maxActive` the renders at once. */ export function createCapturePool({ budget, maxActive = Infinity, perWorker = FRAMES_PER_WORKER }) { const B = Math.max(1, Math.floor(budget)); let free = B; let active = 0; let composing = 0; let seq = 0; const queue = []; const stats = { peakWorkers: 0, peakActive: 0, leases: [] }; const want = (count) => Math.max(1, Math.min(B, Math.ceil(Number(count || 0) / perWorker))); const pump = () => { if (composing > 0) return; // Largest first; ties in the order asked. queue.sort((x, y) => y.count - x.count || x.seq - y.seq); for (let i = 0; i < queue.length && free > 0 && active < maxActive; ) { const q = queue[i]; // The others that could start beside this one now: one worker each. const others = Math.min(queue.length - 1, maxActive - active - 1); const n = Math.max(1, Math.min(want(q.count), free - others)); queue.splice(i, 1); free -= n; active += 1; stats.peakWorkers = Math.max(stats.peakWorkers, B - free); stats.peakActive = Math.max(stats.peakActive, active); stats.leases.push({ count: q.count, workers: n }); let released = false; q.resolve({ workers: n, release() { if (released) return; released = true; free += n; active -= 1; pump(); }, }); } }; const ask = (count) => new Promise((resolve) => { queue.push({ count: Number(count) || 0, seq: seq++, resolve }); pump(); }); return { budget: B, stats, /** Workers for a capture of `count` frames, for a caller that is no member: `{ workers, release }`. */ lease: ask, /** * A layer that has started: until it asks (`lease`) or is finished * (`done`, leased or not), no lease is granted. */ member() { composing += 1; let state = "composing"; const settle = () => { if (state === "composing") composing -= 1; }; return { lease(count) { settle(); state = "asked"; return ask(count); }, done() { settle(); state = "done"; pump(); }, }; }, }; } /** * Run `jobs` (functions returning promises) with at most `limit` running at * once; results in the jobs' order. A failure rejects once every started * job has settled. */ export async function runLimited(jobs, limit = Infinity) { const out = new Array(jobs.length); let next = 0; let failed = null; const width = Math.max(1, Math.min(jobs.length, limit)); await Promise.all(Array.from({ length: width }, async () => { while (next < jobs.length && !failed) { const i = next++; try { out[i] = await jobs[i](); } catch (e) { failed = failed ?? { e }; } } })); if (failed) throw failed.e; return out; }