import type { PageObjectNumber, Placed, WorkingSetPage } from '@embedpdf/engine-core/runtime'; import { EngineError, EngineErrorCode, placedBefore } from '@embedpdf/engine-core/runtime'; import type { PdfRuntimeModule, Ptr } from '@embedpdf/engine-runtime'; import { withScratch } from '../../runtime/memory/scratch'; import { peekU64 } from '../../runtime/memory/u64'; import { throwIfAborted } from '../../shared/abort'; import type { Slices } from '../../shared/slices'; /** * The parsed pages of every document on one runtime - one WASM heap, or one * server thread - kept within a budget of bytes. * * Parsing a page is the costly part of using it: seconds for a page of * millions of vector objects, where one tile of it renders in milliseconds. * So a page stays parsed after the job that used it, for the next one, as long * as memory allows: * - A page costs what its parse made (`EPDFPage_GetParsedSize`) and a fixed * overhead for the page itself. * - Room is made before a page loads, for its size when it was last open, and * again after every slice of a load, as the page grows. A page a job holds * never closes, so one page may be over the budget on its own: the one a * job needs. * - A page bigger than the whole budget stays parsed after its job too, or * every job on it would parse it again: of such pages, the one that would * close last is kept, and the budget holds every other page beside it. It * closes when the pages jobs hold need the budget's room themselves. * - What the viewers show decides what closes first (`setWorkingSet`): pages * in no working set, then `near` pages, then `visible` pages from the * fewest device pixels on screen up, so a thumbnail rail's pages close * before the main view's. Least recently used first among equals. * - A render loads its page in slices, between which its job can be aborted. * An aborted load is set aside half parsed, and the next job that needs the * page goes on with it. * - A page whose content changed since it was parsed closes when it is next * taken (`EPDFPage_IsContentCurrent`). That sees writes made in layer * transactions; the worker host closes a document's pages around the writes * that change content in place. * - A page's image cache is emptied when its last job releases it, so its next * job renders images as a newly loaded page does. * - Pages no job uses and no viewer shows close after a while, giving their * memory back to the allocator. * * Pages belong to an owner, one per open document ({@link PagePtrPool}). */ export interface PageResidencyPolicy { /** * Bytes of parsed pages kept at once; a page a job holds, or one page bigger * than the budget, may be over it alone. */ budgetBytes: number; /** Pages no job uses close after this many milliseconds; 0 disables the timer. */ idleMs: number; } /** * 384 MB: what a server thread of the hosted plan can spare, and well inside * the heap a phone browser allows a tab. */ export const DEFAULT_PAGE_RESIDENCY_POLICY: PageResidencyPolicy = { budgetBytes: 384 * 1024 * 1024, idleMs: 30_000, }; /** What a page costs besides its objects: the page itself and its caches. */ export const PAGE_OVERHEAD_BYTES = 64 * 1024; /** `EPDF_PAGE_PARSED_SIZE`: 40 bytes on every platform, the bytes at 24. */ const PARSED_SIZE_BYTES = 40; const PARSED_SIZE_ESTIMATED_BYTES_OFFSET = 24; /** Where a page no view shows is: it closes first. */ const ELSEWHERE: Placed = { place: 'elsewhere', pixels: 0 }; /** A budget that finishes any load in one call. */ const WHOLE_LOAD_MS = 0x3fffffff; interface ResidentPage { readonly owner: object; readonly pageObjectNumber: PageObjectNumber; readonly ptr: Ptr; /** Jobs holding the page; it never closes while held. */ refs: number; /** False while its parse goes on in slices. */ loaded: boolean; /** What it costs, as last measured. */ bytes: number; /** When it was last taken or released, for least-recently-used order. */ lastUsed: number; } export class PageResidency { private readonly pages = new Map>(); /** What each closed page cost when it was last open: the room its next load needs. */ private readonly closedSizes = new Map>(); /** Per owner, per view: where the pages it shows are (see {@link setWorkingSet}). */ private readonly workingSets = new Map>>(); private totalBytes = 0; private clock = 0; private timer: ReturnType | null = null; constructor( private readonly runtime: PdfRuntimeModule, private readonly policy: PageResidencyPolicy = DEFAULT_PAGE_RESIDENCY_POLICY, /** * True while a job is between two of its steps, such as a page render * between slices, when nothing else may use PDFium: the idle timer then * waits another period instead of closing pages. */ private readonly busy: () => boolean = () => false, ) {} /** The page, loaded in one go when it isn't parsed; held until {@link release}. */ acquire(owner: object, docPtr: Ptr, pageObjectNumber: PageObjectNumber): Ptr { let page = this.take(owner, pageObjectNumber); if (!page) { this.makeRoom(this.expectedBytes(owner, pageObjectNumber)); const ptr = this.runtime.fn.EPDFDoc_LoadPageByObjectNumberNormalized( docPtr, pageObjectNumber, ); if (!ptr) throw noPage(pageObjectNumber); page = this.add(owner, pageObjectNumber, ptr, true); } page.refs++; if (!page.loaded) { if (this.runtime.fn.EPDFPage_ContinueLoad(page.ptr, WHOLE_LOAD_MS) !== 1) { this.drop(page); throw loadFailed(pageObjectNumber); } page.loaded = true; } this.measure(page); this.makeRoom(0); return page.ptr; } /** * The page, loaded in slices when it isn't parsed: between slices the * caller's thread receives messages, and an abort stops the load there. An * aborted load is set aside, to go on when a job next needs the page. Held * until {@link release}. */ async acquireInSlices( owner: object, docPtr: Ptr, pageObjectNumber: PageObjectNumber, signal: AbortSignal, slices: Slices, ): Promise { throwIfAborted(signal); const { fn } = this.runtime; let page = this.take(owner, pageObjectNumber); if (!page) { this.makeRoom(this.expectedBytes(owner, pageObjectNumber)); const ptr = fn.EPDFDoc_StartLoadPageByObjectNumber(docPtr, pageObjectNumber, true); if (!ptr) throw noPage(pageObjectNumber); page = this.add(owner, pageObjectNumber, ptr, false); } // Held while it loads, so making room never closes it. page.refs++; while (!page.loaded) { const result = fn.EPDFPage_ContinueLoad(page.ptr, slices.budgetMs); if (result < 0) { this.drop(page); throw loadFailed(pageObjectNumber); } page.loaded = result === 1; this.measure(page); this.makeRoom(0); if (page.loaded) break; try { await slices.between(); throwIfAborted(signal); } catch (error) { this.release(owner, pageObjectNumber); throw error; } } return page.ptr; } /** A job is done with the page; it stays parsed while there is room. */ release(owner: object, pageObjectNumber: PageObjectNumber): void { const page = this.pages.get(owner)?.get(pageObjectNumber); if (!page || page.refs <= 0) return; page.refs--; if (page.refs > 0) return; page.lastUsed = ++this.clock; // A page half loaded has drawn nothing yet. A page whose image cache // can't be emptied (a render of it is still open) can't be kept. if (page.loaded && !this.runtime.fn.EPDFPage_ResetRenderCache(page.ptr)) { this.close(page); return; } this.armTimer(); this.makeRoom(0); } /** * What `view` shows of `owner`'s pages, replacing its last set; an empty set * withdraws it. Decides which kept pages close first (see the class notes). * Only bookkeeping: it touches no PDFium state, so it may run mid-render. */ setWorkingSet( owner: object, view: string, pages: Iterable< { pageObjectNumber: PageObjectNumber } & Pick >, ): void { const shown = new Map(); for (const { pageObjectNumber, role, pixels } of pages) { const placed: Placed = { place: role, pixels }; const listed = shown.get(pageObjectNumber); if (!listed || placedBefore(placed, listed)) shown.set(pageObjectNumber, placed); } let views = this.workingSets.get(owner); if (shown.size > 0) { if (!views) { views = new Map(); this.workingSets.set(owner, views); } views.set(view, shown); } else { views?.delete(view); if (views?.size === 0) this.workingSets.delete(owner); } // Pages that left the set may now be idle and unshown: the timer closes them. if (this.totalBytes > 0) this.armTimer(); } /** True while a job holds the page; an idle page is not held. */ isHeld(owner: object, pageObjectNumber: PageObjectNumber): boolean { return (this.pages.get(owner)?.get(pageObjectNumber)?.refs ?? 0) > 0; } /** True while the page is kept, parsed or loading: a job for it parses nothing new. */ isKept(owner: object, pageObjectNumber: PageObjectNumber): boolean { return this.pages.get(owner)?.has(pageObjectNumber) ?? false; } /** Closes every page of `owner`, held or not: its document closes. */ closeOwner(owner: object): void { for (const page of [...(this.pages.get(owner)?.values() ?? [])]) this.close(page); this.pages.delete(owner); this.closedSizes.delete(owner); this.workingSets.delete(owner); } /** Closes the pages of `owner` no job holds: a write changed its content in place. */ closeIdleOf(owner: object): void { for (const page of [...(this.pages.get(owner)?.values() ?? [])]) { if (page.refs === 0) this.close(page); } } /** Closes every page no job holds. */ closeIdle(): void { for (const owned of [...this.pages.values()]) { for (const page of [...owned.values()]) { if (page.refs === 0) this.close(page); } } } /** Closes the pages no job holds and no viewer shows on screen: the idle timer. */ private closeUnshown(): void { for (const owned of [...this.pages.values()]) { for (const page of [...owned.values()]) { if (page.refs === 0 && this.placeOf(page).place !== 'visible') this.close(page); } } } /** Pages kept, held or not (tests and diagnostics). */ get size(): number { let count = 0; for (const owned of this.pages.values()) count += owned.size; return count; } /** What the pages kept cost, as last measured (tests and diagnostics). */ get bytes(): number { return this.totalBytes; } /** The page when it is kept, unless its content changed meanwhile. */ private take(owner: object, pageObjectNumber: PageObjectNumber): ResidentPage | null { const page = this.pages.get(owner)?.get(pageObjectNumber); if (!page) return null; if (page.refs === 0 && !this.runtime.fn.EPDFPage_IsContentCurrent(page.ptr)) { this.close(page); return null; } page.lastUsed = ++this.clock; return page; } private add( owner: object, pageObjectNumber: PageObjectNumber, ptr: Ptr, loaded: boolean, ): ResidentPage { const page: ResidentPage = { owner, pageObjectNumber, ptr, refs: 0, loaded, bytes: PAGE_OVERHEAD_BYTES, lastUsed: ++this.clock, }; let owned = this.pages.get(owner); if (!owned) { owned = new Map(); this.pages.set(owner, owned); } owned.set(pageObjectNumber, page); this.totalBytes += page.bytes; return page; } private measure(page: ResidentPage): void { const { fn, mem } = this.runtime; const parsed = withScratch(mem, PARSED_SIZE_BYTES, (size) => fn.EPDFPage_GetParsedSize(page.ptr, size) ? peekU64(mem, size, PARSED_SIZE_ESTIMATED_BYTES_OFFSET) : 0, ); const bytes = PAGE_OVERHEAD_BYTES + parsed; this.totalBytes += bytes - page.bytes; page.bytes = bytes; } private expectedBytes(owner: object, pageObjectNumber: PageObjectNumber): number { return this.closedSizes.get(owner)?.get(pageObjectNumber) ?? PAGE_OVERHEAD_BYTES; } /** * Closes pages no job holds until `incoming` more bytes fit: the worst * placed first (see the class notes), least recently used first among * equals. * * A page bigger than the budget on its own may stay over it: of such pages, * the one that would close last. So it stays parsed between the jobs that * use it, instead of parsing again for each one. Every other page fits in * the budget beside it; when the pages jobs hold don't, they are the pages * over it, and the spared page closes too. A budget of 0 keeps nothing. */ private makeRoom(incoming: number): void { let spared = this.policy.budgetBytes > 0 ? this.lastToCloseOverBudget() : null; while (this.totalBytes - (spared?.bytes ?? 0) + incoming > this.policy.budgetBytes) { const victim = this.firstToClose(spared); if (victim) { this.close(victim); } else if (spared && spared.refs === 0) { this.close(spared); spared = null; } else { return; } } } /** The page no job holds that closes first, other than `spared`. */ private firstToClose(spared: ResidentPage | null): ResidentPage | null { let victim: ResidentPage | null = null; let victimPlace = ELSEWHERE; for (const owned of this.pages.values()) { for (const page of owned.values()) { if (page.refs > 0 || page === spared) continue; const placed = this.placeOf(page); const worse = !victim || placedBefore(victimPlace, placed) || (!placedBefore(placed, victimPlace) && page.lastUsed < victim.lastUsed); if (worse) { victim = page; victimPlace = placed; } } } return victim; } /** * Of the pages bigger than the budget, the one that closes last: the best * placed, the most recently used among equals. */ private lastToCloseOverBudget(): ResidentPage | null { let kept: ResidentPage | null = null; let keptPlace = ELSEWHERE; for (const owned of this.pages.values()) { for (const page of owned.values()) { if (page.bytes <= this.policy.budgetBytes) continue; const placed = this.placeOf(page); const better = !kept || placedBefore(placed, keptPlace) || (!placedBefore(keptPlace, placed) && page.lastUsed > kept.lastUsed); if (better) { kept = page; keptPlace = placed; } } } return kept; } /** Where the working sets have the page: the best place any view gives it. */ private placeOf(page: ResidentPage): Placed { let best = ELSEWHERE; for (const shown of this.workingSets.get(page.owner)?.values() ?? []) { const placed = shown.get(page.pageObjectNumber); if (placed && placedBefore(placed, best)) best = placed; } return best; } private close(page: ResidentPage): void { if (page.loaded) { let sizes = this.closedSizes.get(page.owner); if (!sizes) { sizes = new Map(); this.closedSizes.set(page.owner, sizes); } sizes.set(page.pageObjectNumber, page.bytes); } this.drop(page); } /** Closes the page without remembering its size. */ private drop(page: ResidentPage): void { this.runtime.fn.FPDF_ClosePage(page.ptr); const owned = this.pages.get(page.owner); owned?.delete(page.pageObjectNumber); if (owned?.size === 0) this.pages.delete(page.owner); this.totalBytes -= page.bytes; if (this.totalBytes <= 0) this.disarmTimer(); } private armTimer(): void { if (this.policy.idleMs <= 0) return; this.disarmTimer(); this.timer = setTimeout(() => { this.timer = null; if (this.busy()) this.armTimer(); else this.closeUnshown(); }, this.policy.idleMs); // A Node worker thread must be able to exit while pages are idle. (this.timer as { unref?: () => void }).unref?.(); } private disarmTimer(): void { if (this.timer) clearTimeout(this.timer); this.timer = null; } } function noPage(pageObjectNumber: PageObjectNumber): EngineError { return new EngineError( EngineErrorCode.NotFound, `no page with object number ${pageObjectNumber}`, ); } function loadFailed(pageObjectNumber: PageObjectNumber): EngineError { return new EngineError( EngineErrorCode.RuntimeUnavailable, `failed to load page object ${pageObjectNumber}`, ); }