Repository navigation
OPFS storage, sharing databases between tabs, and faster row reads - #5
Merged
Merged
Conversation
…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
force-pushed
the
storage-prototypes
branch
from
October 5, 2026 11:24
eeea43b to
014fbf2
Compare
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.
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.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.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 anyFileSystempassed tofileStorage).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 theirquery,execandtransactionrequests over aBroadcastChannel.Speed and size
cwrap, and column names are cached per execution.getAsObjectover 10k rows is about 5× faster.strftime: a small replacement in the bridge forcurrent_dateand 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.
Tests
opfs.test.jsandshared.test.jsrun on an in-memory file system that simulates crashes and failing reads and writes, plus a fake Web Locks.bun run test:browserrunstest/browserin headless Chromium against real OPFS, Web Locks and BroadcastChannel, including terminating the owning Worker. CI installs Chromium for it withplaywright-core1.63, the first version that supports the runner's Ubuntu 26.04.Notes
bun.lockwas updated with the pinned bun 1.4.2 when addingplaywright-core.🤖 Generated with Claude Code
https://claude.ai/code/session_01ApMziyJxWCSYAzPSXE8LpL