Skip to content

0.8: snapshot sessions, delta saves, block reads and file overlays - #6

Merged
benmerckx merged 16 commits into
mainfrom
claude/relaxed-brahmagupta-hitp0i
Oct 9, 2026
Merged

benmerckx merged 16 commits into
mainfrom
claude/relaxed-brahmagupta-hitp0i

Conversation

@benmerckx

@benmerckx benmerckx commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

The changes planned for 0.8, on top of 0.7.0. Breaking for snapshot storage; nothing else changes.

API (breaking)

  • storage.open() returns a Session (db, snapshot, branch, held, save(), fork(), close({save})); storage.session(db) wraps a database the storage did not open.
  • session.save({key, meta}) replaces storage.checkpoint() and resolves to {status: 'written' | 'joined' | 'mismatch', snapshot}.
  • Groups are branches, fixed per session; open({branch, choose}) picks where to start, out of the readable snapshots, newest first (replaces fallback: 'any').
  • retain({perBranch, branches, pinned}) replaces cleanup({keepGroups}); factories are opfsSnapshots, indexedDBSnapshots, memorySnapshots; types are Snapshot*.
  • SnapshotStore.remove(key, {force}) resolves to whether it deleted the snapshot.
  • IndexedDB snapshot stores move to schema version 3 and drop older caches.

Storage and speed

  • Saves write only the changed pages, as deltas over the snapshot below, when that snapshot is of the same branch (at most maxDepth, default 8; full saves otherwise). A branch never needs another branch's snapshots, so retain({branches}) drops branches whole.
  • Snapshots are read a 1 MB block at a time while reads run forward.
  • Joining an existing key compares only a delta's pages when it lies over the very snapshot the database reads.
  • Stores check atomically that a delta's parent exists as they add it, and that no delta lies over a snapshot as they delete it.
  • A full save to IndexedDB reads the parts of the old snapshot it keeps into memory before adding the new Blob. WebKit stores a Blob composed of slices of other IndexedDB Blobs wrongly once it is over about 32 MB: other Workers read some parts from the wrong offsets. Chromium and Firefox store such Blobs correctly.

Saving 10 changed rows of the 47 MB benchmark database (ms, OPFS / IndexedDB):

Browser 0.7 0.8
Chromium 141 211 / 123 27 / 14
Firefox 155 434 / 264 201 / 15
WebKit 26.6 343 / 4155 24 / 16

Full scans are 6–8× faster in Chromium.

New

  • openOverlay(Database, file) opens a database over a read-only file without loading it; @alinea/sqlite-wasm/file adds readOnlyFileAt(path) for Node and Bun.

Testing

  • 347 Bun tests, including randomized comparisons against a plain database with deltas in every store.
  • Browser tests pass in Chromium 141, Firefox 155 and WebKit 26.6 (Playwright builds); CI is green.
  • Two code reviews; their findings are fixed in 3fa9b0d and 0f8bae4.
  • alinea migration (on top of Keep the dashboard's browser cache as immutable snapshots, so workers of different builds can't corrupt it alinea#573) passes its BrowserEntryStore tests and its dashboard cache spec. The spec found that deltas across branches kept dropped branches alive, fixed in 30f5a69.
  • The benchmark's two-SharedWorker check failed in WebKit with IndexedDB: a full save over deltas of the 47 MB database read back corrupt in other Workers. A minimal reproduction without this library showed the WebKit Blob storage bug above; fixed in 2226279, with a browser test that writes a full save over deltas of a 40 MB database and reads it in other Workers. Without the fix, the test fails in WebKit with SQLITE_CORRUPT. With it, the two-Worker check passed 4 runs out of 4, and every snapshot read back correctly in a fresh Worker.

🤖 Generated with Claude Code

https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7

benmerckx and others added 16 commits October 8, 2026 20:58
storage.open() now returns a Session that holds the database and its link
to the storage: session.save() replaces storage.checkpoint(), and resolves
to {status, snapshot}, where status tells a written snapshot from a key
that held the same content ('joined') or other content ('mismatch'), which
a boolean could not. session.snapshot, .branch and .held replace baseOf(),
the group WeakMap and PRAGMA overlay_pages arithmetic; session.fork() and
session.close({save}) round it off, and storage.session(db) wraps a
database the storage did not open.

Groups are branches (head() replaces newest()), bases are snapshots
(SnapshotStore, SnapshotInfo, SnapshotMeta), checkpoint options are
autoSave on the session, and cleanup({keepGroups}) is
retain({perBranch, branches, pinned}). The factories are opfsSnapshots,
indexedDBSnapshots and memorySnapshots. No aliases: IndexedDB snapshot
stores move to schema version 2 and drop version 1's bases, which are a
cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
A FileReaderSync read costs about a millisecond however little it reads,
so reading a snapshot page by page made scans slow. readOnlyFile now reads
the whole 1 MB block a read starts in when it starts where the previous
read ended, or shortly after, and keeps the newest 4 blocks; reads in
random order still read only what they ask for, as reading whole blocks
for them made index lookups across a large table slower.

In Chromium 141, a cold full scan of the 47 MB benchmark database drops
from 1255 to 160 ms on OPFS and from 793 to 134 ms on IndexedDB; point
lookups and the index range are as fast or faster (BENCHMARKS.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
A save rewrote the whole database even when a few rows changed. It now
writes the changed pages as a delta over the snapshot the database reads,
with their layout (DeltaLayout: parent, page indexes, visible bytes),
while that keeps at most maxDepth (default 8) deltas on a full snapshot,
the new pages are at most half the database, and all those deltas
together no more than it; else it writes the whole database, composing
the content from the layers. Readers resolve each page through the
layers; OPFS readers lock every snapshot of the chain, retain() keeps
the snapshots kept ones lie over, and opening a delta whose parent is
missing fails with SQLITE_CANTOPEN.

OPFS stores a delta as a marker and its pages, with the layout in the
metadata file; IndexedDB and memory stores keep the layout in the record.
SnapshotInfo has the parent; storages take {maxDepth}, 0 for full
snapshots only.

In Chromium 141, saving 10 changed rows of the 47 MB benchmark database
drops from 211 to 27 ms on OPFS and from 123 to 14 ms on IndexedDB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
openOverlay(Database, file) opens a database over a read-only file as
snapshot storage does: pages are read as queries need them, changes stay
in memory, forks are cheap, and the file closes with the last of them.
@alinea/sqlite-wasm/file adds readOnlyFileAt(path), which reads a file
with fs.readSync, for Node and Bun where the native overlay extension
cannot be loaded (Bun's SQLite on macOS, or a platform without a prebuilt
binary): there a generated database no longer has to be read into memory
whole. Nothing locks the file, so every read checks that its size and
modification time are unchanged, and fails rather than mix the pages of
a file written in place; a file replaced by a rename keeps being read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
- Without locks, a save checks that the snapshot a delta would lie over
  still exists, and writes in full if retain() elsewhere deleted it, so a
  head never lies over a missing snapshot.
- open() no longer retries the same unreadable head: when it, or a
  snapshot below it, is missing, it tries the branch's other snapshots,
  newest first, then (with fallback) other branches, and starts empty
  when none opens.
- maxDepth is at most 64, the deepest chain that opens.
- Opening a snapshot and openOverlay share one sequence, which releases
  the file once: openOverlay could close it twice when opening failed.
- With blocks: 0, readOnlyFile reads what is asked instead of whole blocks
  it cannot keep.
- Files of layers are made once per layer, so files over one chain share
  block caches, and the composed content of a base is no longer kept (it
  copied a database in memory for every base read from bytes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
…ession

open({branch, fallback: 'any-branch'}) mixed where a session starts with
where it saves, and save({branch}) moved a session to another branch.
Now branch only names where a session saves, fixed when it opens, and
open() takes choose(snapshots): the snapshot to start from out of the
readable ones, newest first, by default the head of the branch. A
fallback to another branch is ordinary code, and a snapshot that turns
out unreadable is left out and choose asked again. save() no longer
takes a branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
When a save's key exists, the database moves onto that snapshot only if
it holds the same bytes, which took reading the whole database. Two
Workers that make the same changes on the same snapshot write deltas
over it with the same pages, so when the existing snapshot is a delta
over the one the database reads, with the same page list and size,
comparing those pages decides it. On a 49 MB database in Bun, joining
such a save drops from about 70 ms to 3 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
The content a full save writes over deltas was composed again every time
a store read it, which IndexedDB does once per run of unchanged pages:
saves that changed many pages over a delta chain took seconds (6 s on
the 47 MB benchmark database with 64 KB pages, 30 s with 16 KB pages,
in Bun). It is now composed once per save, and still not kept after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
A full save to OPFS streamed the whole old snapshot into the new file and
then wrote the changed pages over it, so a save that changed most pages
wrote the database twice; and the IndexedDB store asked for the old
content before checking that any of it was kept, which composes a whole
database out of a delta chain. Both now read the old content only for
the runs no page replaces. FileWriter.write takes Blob slices too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
…page

The Blob of a snapshot in IndexedDB had a part per page, copied out of
Wasm one by one: with smaller pages, thousands of parts. Consecutive
pages (all pages, for a delta) are now copied into one buffer of up to
4 MB each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
A full save over a delta chain sliced the runs of unchanged pages out of
the whole old database, composed from the chain as one Blob of thousands
of parts. NewSnapshot.baseRange(start, end) now composes just the range a
run keeps, straight from the layers, and the stores use it where given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
- Stores check atomically that a delta's parent exists as they add it,
  and that no delta lies over a snapshot as they delete it: IndexedDB in
  one transaction (with an index on the parent, schema version 3), the
  memory store at once; the OPFS store, whose readers lock their chains,
  checks before it shows the file. A save whose parent went missing
  writes in full. SnapshotStore.remove(key, {force}) resolves to whether
  it deleted the snapshot.
- Joining a key by comparing only a delta's pages also requires its
  parent to be the very snapshot the database reads (by time and size),
  not one stored again under the same key.
- Old content shorter than recorded fails a full save instead of being
  padded with zeros.
- openOverlay closes the file when its size cannot be read.
- Chains of more than 64 deltas fail to open, as maxDepth allows.
- open() lists snapshots once, however many turn out unreadable.
- Comparing an existing snapshot reads both in ranges instead of
  composing a whole database.
- Files over one layer share its block cache but each tracks its own
  reads, so interleaved scans keep reading ahead.
- The OPFS store writes pages through the same run helpers as IndexedDB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
A session that opened another branch's snapshot (such as a new config
starting from the newest base of any) saved a delta over it, so its
branch needed the other's snapshot and retain({branches}) kept a branch
it was asked to drop. A save now writes in full over a snapshot of
another branch, and deltas within its own branch after that.

Found by running alinea's snapshot cache spec (alineacms/alinea#573)
against this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
WebKit stores a Blob composed of slices of other IndexedDB Blobs wrongly
once it is over about 32 MB: Workers that read the record later get some
parts from the wrong offsets. A full save over deltas then left a corrupt
snapshot, which the two-Worker benchmark caught as a wrong log count.
Chromium and Firefox store such Blobs correctly.

A full save to IndexedDB now reads the parts of the old snapshot it keeps
into memory before adding the Blob. Deltas were built from pages in
memory already. The new browser test writes a full save over deltas of a
40 MB database and reads it in other Workers; without the fix it fails in
WebKit with SQLITE_CORRUPT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
@benmerckx
benmerckx force-pushed the claude/relaxed-brahmagupta-hitp0i branch from e146cbc to ea7511e Compare October 9, 2026 08:48
@benmerckx
benmerckx marked this pull request as ready for review October 9, 2026 08:48
@benmerckx
benmerckx merged commit 8e0616d into main Oct 9, 2026
7 checks passed
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.

1 participant