Repository navigation
0.8: snapshot sessions, delta saves, block reads and file overlays - #6
Merged
Merged
Conversation
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
force-pushed
the
claude/relaxed-brahmagupta-hitp0i
branch
from
October 9, 2026 08:48
e146cbc to
ea7511e
Compare
benmerckx
marked this pull request as ready for review
October 9, 2026 08:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The changes planned for 0.8, on top of 0.7.0. Breaking for snapshot storage; nothing else changes.
API (breaking)
storage.open()returns aSession(db,snapshot,branch,held,save(),fork(),close({save}));storage.session(db)wraps a database the storage did not open.session.save({key, meta})replacesstorage.checkpoint()and resolves to{status: 'written' | 'joined' | 'mismatch', snapshot}.open({branch, choose})picks where to start, out of the readable snapshots, newest first (replacesfallback: 'any').retain({perBranch, branches, pinned})replacescleanup({keepGroups}); factories areopfsSnapshots,indexedDBSnapshots,memorySnapshots; types areSnapshot*.SnapshotStore.remove(key, {force})resolves to whether it deleted the snapshot.Storage and speed
maxDepth, default 8; full saves otherwise). A branch never needs another branch's snapshots, soretain({branches})drops branches whole.Saving 10 changed rows of the 47 MB benchmark database (ms, OPFS / IndexedDB):
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/fileaddsreadOnlyFileAt(path)for Node and Bun.Testing
🤖 Generated with Claude Code
https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7