Files
AFFiNE/blocksuite/crates/preview
DarkSky 2144304032 perf(preview): keep PDF documents open in the worker by docId
The preview workers now use a linear-memory ABI with four exports:
`memory`, `prepare(len)`, `invoke()` and `output_ptr()`. The host writes
each request into worker memory in one copy and copies each response out in
one copy. The old ABI called an export for every 4 bytes in each direction.

Rust gives out only the addresses of its own buffers, so the crate keeps
`forbid(unsafe_code)`. Buffers larger than 1 MiB go back to the allocator
after each request.

The PDF worker keeps each parsed document under a `docId` that the host
assigns. The frames are `PDF_OPEN`, `PDF_RENDER` and `PDF_CLOSE`.

The bytes of a document go to the worker once per open, not once per page.
Page pixels come back as a compact RGBA buffer that the worker transfers.

The playground bridge has one session table in `wasm_pdf_state.rs`. It
opens at most 8 documents or 256 MiB of source bytes.

To make room, the table closes least recently used idle documents, but only
when that makes the new document fit. Empty documents and documents larger
than 256 MiB never go to the worker.

The table closes a document when the view clears the last scope that used
it, also when no page request claimed it yet. A reopen that fails after a
worker restart keeps the known page count.

The table restarts the worker before the IDs run out.

The bridge no longer keeps 16 copies of PDF bytes.

Each worker event listener ignores events from a replaced worker, so a late
error of an old worker cannot stop the new one. A crash answers each sent
request once, so the count of unanswered requests stays exact.

`pdf-client.js` uses the same protocol and keeps its public methods. It
keeps the same open budget and waits for room before it sends an open.

New bytes for a known `docKey` now open a new document and drop the old
page count and pages. Before, the client returned the old page count.

Four snapshot IDs of the PDF viewer and the attachment toolbar use
`format_args!`, so the production bundle has no copy of their prefixes. The
prefix gate now checks 14 prefixes.
2026-12-01 18:55:10 +09:00
..