Skip to content

OPFS storage, sharing databases between tabs, and faster row reads - #5

Merged
benmerckx merged 14 commits into
mainfrom
storage-prototypes
Oct 5, 2026
Merged

benmerckx merged 14 commits into
mainfrom
storage-prototypes

Conversation

@benmerckx

@benmerckx benmerckx commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Adds storage that keeps a database in an OPFS file without holding it in memory, a way for every tab to use one database, and some speed and size improvements along the way.

OPFS storage (@alinea/sqlite-wasm/opfs)

Database.sync(opfsStorage(name)) keeps a database in a file of the origin private file system, in a dedicated Worker. Pages are read from the file as queries need them, so only SQLite's page cache and the open transaction are held in memory.

  • Overlay (src/overlay.c): the overlay can now open its base file writable through another VFS (base=<vfs>) and write every commit back to it, after which it drops the written pages from memory.
  • Durability: commits are made durable in a redo journal next to the file, in the style of a write-ahead log. Each commit appends one record and syncs only the journal; the file is synced and the journal started over every 4 MB and on close or detach. Records carry a salt and a sequence number, so a crash never replays records from before a checkpoint.
  • Forks: snapshots borrow the source's file handle. Before the source overwrites a page they still read from the file, they copy it, and when the source closes or detaches they copy everything they read.
  • Attach and detach: db.attach(opfsStorage(...)) moves an in-memory database into a file, replacing what it held as one commit; db.detach() brings it back into memory.
  • src/jsvfs.c: a small VFS whose files are answered by JavaScript (OPFS sync access handles, or any FileSystem passed to fileStorage).
  • Native extension: unchanged unless base= is used. Writable bases, and snapshots of them, are never memory-mapped.

Sharing a database between tabs (@alinea/sqlite-wasm/shared)

shareDatabase(name, open), called in a dedicated Worker in every tab, has the Web Locks API elect one Worker to open the database. The others send their query, exec and transaction requests over a BroadcastChannel.

  • Requests run one at a time, and a request that leaves a transaction open is rolled back.
  • When the owning tab closes, the next Worker in line opens the database and the others resend what was not answered. A request the previous owner ran but never answered runs twice; the README says to make writes safe to repeat.
  • A Worker that fails to open the database tries again later, with backoff.

Speed and size

  • Reading rows: values are read through the raw Wasm exports instead of cwrap, and column names are cached per execution. getAsObject over 10k rows is about 5× faster.
  • strftime: a small replacement in the bridge for current_date and friends stops the C library's printf from being linked in, saving 11.7 KB of Wasm.

Benchmarks

Measured in TinyJoin's comparative benchmark: Chromium 141 on a 4-vCPU container, 5 samples per workload, all engines agreeing on every result.

  • Single-row commits: 1,000 of them take 551 ms with OPFS storage, against 1.43 s for the in-memory database stored in IndexedDB and 784 ms for TinyJoin.
  • Overall: OPFS storage is within 0.4–1.3× of the in-memory build across all 20 workloads, and faster than TinyJoin on 18 of 20.

Tests

  • Bun: opfs.test.js and shared.test.js run on an in-memory file system that simulates crashes and failing reads and writes, plus a fake Web Locks.
  • Browser: bun run test:browser runs test/browser in headless Chromium against real OPFS, Web Locks and BroadcastChannel, including terminating the owning Worker. CI installs Chromium for it with playwright-core 1.63, the first version that supports the runner's Ubuntu 26.04.
  • CI: the build against sqlite.org's official downloads, all 179 Bun tests (native tests included), the browser tests and the native builds for all six targets pass.

Notes

  • bun.lock was updated with the pinned bun 1.4.2 when adding playwright-core.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL

benmerckx and others added 14 commits October 2, 2026 21:57
…execution

getAsObject decoded every column name again for each row, and each value
went through a cwrap wrapper. Reading 10,000 five-column rows as objects
drops from about 31 ms to 7 ms in Node, and select-all in the TinyJoin
browser benchmark from about 90 ms to 30 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
With SQLITE_OMIT_DATETIME_FUNCS, SQLite's fallback for current_time,
current_date and current_timestamp calls strftime, which pulls in the C
library's whole printf. A strftime for the six fields SQLite uses shrinks
the Wasm by 11.7 KB (6.3 KB gzipped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
A JS VFS (jsvfs.c) hands SQLite's file operations to JavaScript, with
backends for OPFS sync access handles, IndexedDB awaited through JSPI, and
IndexedDB with retry on a cache miss, plus the Workers that add them to
TinyJoin's comparative benchmark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
The overlay can open its base file read-write through another VFS
(base=VFS) and write every commit back to it, through a redo journal next
to it, after which it drops the written pages and reads them from the base
again. Snapshots that still read from the base copy those pages first.

opfsStorage (@alinea/sqlite-wasm/opfs) keeps a database in an OPFS file
through the new "js" VFS, which hands file access to sync access handles,
so only SQLite's page cache and the open transaction are held in memory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
Writable base files, and the snapshots that read them, change under
existing mappings, so their pages are always read with xRead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
sqlite3_overlay_attach_base moves an overlay onto a writable base file,
writing its committed state as one write-back so the file holds either the
old or the new database; sqlite3_overlay_detach_base stores the last
commits, reads every chunk back into memory and closes the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
shareDatabase (@alinea/sqlite-wasm/shared) elects the Worker that opens a
database with the Web Locks API and relays the statements of the others
over a BroadcastChannel: requests run one at a time, transactions run in
one go, and the next Worker in line takes over when the owner goes away,
answering what was left unanswered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
test/browser drives the package from dedicated Workers against real OPFS,
Web Locks and BroadcastChannel, including a Worker terminated mid-session;
script/test-browser.ts runs it in headless Chromium with playwright-core,
locally and in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
Write-backs now append a record to the redo journal and sync only that,
then write the chunks to the base file unsynced; the base is synced and
the journal started over every 4 MB and on close or detach. Records carry
a salt and sequence number so replay never picks up records from before a
checkpoint, and journal writes are buffered into one write per record.
1000 single-row commits to OPFS take about 400 ms instead of 715 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
- Snapshots of a stored database borrow its base file handle, so forks of
  forks read it too and native builds do not lock themselves out; they
  copy what they read before being stored elsewhere.
- Detaching stays attached if the database cannot be read into memory.
- Files are registered per file system, so equal names in different
  directories do not collide.
- Closing tries failed write-backs once more, and flush reports if that
  failed.
- A Worker that failed to open a shared database lines up again later,
  and the owner caches at most 100 prepared statements.
- Statement forgets column names when it finishes, and strftime accepts a
  result that exactly fills the buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
When a stored database closes, each fork that borrowed its file copies
what it reads and lets go of the file on its own; if copying fails, the
pages it lacks now fail to read with SQLITE_IOERR_READ.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
CI's runner is ubuntu-26.04, which playwright-core 1.56 cannot install
Chromium or its dependencies for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL
The experiments that led to OPFS storage are not part of the package.

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

get and getAsObject each bound and stepped on their own, and getAsObject
inlined the column-name cache. Both now go through stepWith and
cachedColumnNames, get fills a preallocated array, and the text decoder
returns early for long strings. Reading rows performs the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uFsKUBw3TWkDrpp16dJM7
@benmerckx
benmerckx merged commit e63bc42 into main Oct 5, 2026
7 checks passed
@benmerckx
benmerckx deleted the storage-prototypes branch October 5, 2026 11:25
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