Skip to content

Read Blob snapshots through a shared cache of blocks (0.8.2) - #7

Merged
benmerckx merged 5 commits into
mainfrom
claude/stoic-cori-d073i6
Oct 13, 2026
Merged

benmerckx merged 5 commits into
mainfrom
claude/stoic-cori-d073i6

Conversation

@benmerckx

Copy link
Copy Markdown
Member

Fixes slow random reads from Blob-backed snapshots. A profile of a full-text search in alinea's dashboard spent 14.6 s of 16 s in FileReaderSync.readAsArrayBuffer and Blob.slice.

Problem

In 0.8.1, readOnlyFile kept 4 blocks of 1 MiB, and used them only for reads that ran forward or whose blocks were all cached. Any other read was its own slice + readAsArrayBuffer of exactly the bytes asked. FTS5 and b-tree lookups read pages in random order, so nearly every page SQLite missed became its own synchronous Blob read.

Change

  • Every Blob read goes through the cache. It holds 16 KiB blocks, 32 MiB by default. A read fetches the blocks it is missing with one slice + readAsArrayBuffer per contiguous run of them.
  • A run takes along the missing blocks around it: the aligned 256 KiB it lies in, or up to 1 MiB ahead when reads run forward. Blocks taken along wait in at most a quarter of the budget and leave first, so they never push out blocks in use.
  • One cache per SnapshotStorage. It is shared by every database the storage opens and by snapshots that lie over the same layer (below). Blocks are keyed by createdAt:size:key, because each store.get returns a new Blob object.
  • New option readCache: {blockSize, cacheBytes, readAround, readAhead} on indexedDBSnapshots, opfsSnapshots and SnapshotStorage. readOnlyFile takes the same options. Its blocks option is deprecated and still counts blocks of 1 MiB.
  • The Uint8Array path, the snapshot format and the stored data are unchanged.
  • Version 0.8.2, with a new CHANGELOG.md.

Benchmark

bun script/test-browser.ts --fts (new) builds a 208 MB database with an FTS5 table and saves it to IndexedDB. A new Worker then runs two rounds of the same queries and counts FileReaderSync reads. The queries are typed-out prefix MATCH searches, a broad a* joined to the entries table, and 2,000 indexed lookups. Headless Chromium, median of 3 runs:

0.8.1 ms / reads 0.8.2 ms / reads
match k* (first time) 1202 / 745 912 / 261
round 1, all queries 3129 / 1980 3082 / 1154
round 2, all queries 3139 / 1963 871 / 0

Round 1 makes 42% fewer reads but takes about as long. In this environment a read costs about 1 ms and grows with its size, and round 1 times vary by ±20% between runs. Where each read costs more, as with IndexedDB Blobs in the dashboard's profile, fewer reads should count for more. These numbers were measured before the review fixes. Details and other settings are in BENCHMARKS.md.

Testing

  • New unit tests cover:
    • reads across block edges, at the end of the Blob and past it
    • random reads compared byte for byte with the source
    • a read larger than the budget
    • LRU eviction, and memory staying within the budget on a shared cache
    • slice counts for random 4 KiB page reads, and none for repeat reads
    • read-around and read-ahead
    • blocks in use kept over blocks read along
    • sharing the cache across snapshots and deltas of one storage
  • bun test: 374 pass. Browser tests in Chromium: 12 pass, including delta snapshots read in another Worker.
  • Locally the tests ran against the published 0.8.1 Wasm with snapshots.js rebuilt from source, because there was no Emscripten in the environment. CI does the full build.

🤖 Generated with Claude Code

https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk


Generated by Claude Code

Every read of a Blob (snapshots in IndexedDB and OPFS, the layers deltas lie
over, readOnlyFile) now goes through an LRU of 16 KiB blocks, 32 MiB per
storage by default. Missing blocks are read one run at a time, with the
aligned 256 KiB around them, or 1 MiB ahead for forward reads; blocks read
along wait apart and leave first. New readCache option on the storages.
Adds an FTS5 benchmark in a Worker (script/test-browser.ts --fts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk
…den the forward window

- The deprecated blocks option counts blocks of 1 MiB (as in 0.8.1) unless
  blockSize is given, instead of the new 16 KiB default
- blockSize, readAround and readAhead must be finite
- A read counts as running forward within the span read around, not one
  16 KiB block, as 0.8.1 did with its 1 MiB blocks
- The cache identifies a snapshot by its size too
- Fix the size and the read cost given in comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk
- A block taken along that does not fit beside the blocks in use is not
  kept, instead of evicting one
- blockSize is at least 512, so block keys never overflow into those of
  other snapshots
- Validate the deprecated blocks option on its own
- --fts refuses a --runs that is not a number of at least 1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk
@pkg-pr-new

pkg-pr-new Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

bun add https://pkg.pr.new/@alinea/sqlite-wasm@001a081
npm i https://pkg.pr.new/@alinea/sqlite-wasm@001a081

commit: 001a081

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cache metadata and large-read memory can grow beyond the configured budget, and benchmark failure handling needs correction.

5 open findings
What changed in this PR

Adds a shared block cache to improve random reads from Blob-backed SQLite snapshots.

Changes:

  • Introduces configurable block caching with read-around/read-ahead behavior.
  • Adds extensive cache tests and an FTS benchmark.
  • Updates release documentation and version to 0.8.2.
File Description
src/​snapshots.ts Implements shared Blob block caching.
snapshots.test.js Tests cache correctness, eviction, and sharing.
test/​browser/​fts-worker.js Builds and queries the FTS benchmark database.
test/​browser/​fts-bench.js Coordinates benchmark workers.
script/​test-browser.ts Adds the --fts benchmark command.
README.md Documents snapshot caching.
CHANGELOG.md Records the 0.8.2 changes.
BENCHMARKS.md Documents FTS benchmark results.
package.json Bumps the package version.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread script/test-browser.ts Resolved
Comment thread src/snapshots.ts Resolved
Comment thread test/browser/fts-bench.js Resolved
Comment thread src/snapshots.ts Resolved
Comment thread src/snapshots.ts Resolved
- Key cached blocks by their content string, so nothing about a snapshot
  outlives its blocks in the cache
- Of a read larger than the cache, copy only the blocks that can stay
- Document that readAround and readAhead read at most a quarter of
  cacheBytes
- --fts takes a whole number of runs, and a Worker that fails rejects its
  call instead of hanging

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011zxsJtUFr4iK5q3Gkdscnk
@benmerckx
benmerckx merged commit 701952f into main Oct 13, 2026
8 checks passed
benmerckx added a commit that referenced this pull request Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants