Skip to content
gmitch215Public

Latest commit

Β 

History

295 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš‹ Folio

⚑ A toolkit for WebAssembly programs that outgrow a thread, a call or a memory limit

Folio runs larger and longer WebAssembly programs in JavaScript runtimes: Node, browsers, workerd and Cloudflare Workers with Durable Objects. It works on the running instance and leaves the program alone. It covers four things a Wasm program runs into:

  • Capacity. Memory can be stored as derivations, paged behind a running guest, and capped so an oversized allocation fails inside the guest.
  • State. An instance resets between jobs, starts from a booted heap, and checkpoints and continues across Durable Object alarm events.
  • Compute. One program can spread across cores over a shared memory and keep its serial result.
  • Reach. A portable client and a managed backend run the same programs in workerd and on Cloudflare.

Table of Contents

🧭 Why Folio

A Wasm program in a JavaScript host meets limits it knows nothing about. It runs on one thread, its memory ends at a host cap, its state disappears with the call or the isolate, and it pays its boot every time. A web app's codec, a Linux machine in a Durable Object and a PHP interpreter are the same thing here: one Wasm instance under one thread and one memory cap. Folio changes how that instance is run, stored and resumed, and the program stays as compiled.

  • Capacity. A heap can be held as the smallest frame that rebuilds each 4 KiB page: zero, the module's own bytes, a small edit of the earlier version, a word delta or compressed text. A translated PHP interpreter ran a 238 MiB heap in 22 MiB of frames plus a derived pool, with the same output as the all-resident run. Offline, derived checkpoints priced at 419.5x against 29.8x for page hashes, and the decoder is 71 KB of Wasm. The growth governor turns an oversized allocation into a failure the guest can report. A guest that is dropped and booted again in the same isolate can take its predecessor's memory, so the isolate holds one heap where it held two: a 100 MiB machine restored exact 6 of 6 times into its predecessor's memory and 0 of 6 into a new one.
  • State. Checkpoints restore a private instance between jobs. Boot images start a module from a booted memory. Continuations run a guest across Durable Object alarm events and commit only the pages that changed.
  • Compute. Isolates map one backing allocation, so four isolates attach to a touched 64 MiB backing for 3.871 MiB of additional resident memory, against 259.930 MiB for copied buffers. Uncertain dependencies run on private overlays and commit in original order, and the program's own Wasm stack suspends at joins, so branches and host effects behave as they do natively.
  • Reach. The client and the managed backend use fetch, WebSockets and Durable Objects, with no Node imports and no request-time Wasm compilation.

Accepted input does not guarantee a speedup, and a larger heap costs time. A program with a true serial dependency runs serially, a small job loses to dispatch overhead, and a paged guest pays for every page it brings back.

✨ Features

Area Feature What it does Module
Capacity Derived memory Store checkpoints, boot state and cold regions as derivations, and restore a heap into a smaller memory Derived
Capacity Growth governor, elastic regions, page directory and leases Refuse memory.grow past a byte cap, hold a byte range larger than its frames, and hold bounded pages Memory
Capacity Memory reuse Give a dropped guest's memory to its successor, and rewrite a module to import the memory it is given Memory
State Continuations Run a Wasm guest across Durable Object alarm events with checkpoints in SQLite, including a paged guest Continuation
State Baked boot images Write a booted memory into the module as data segments Boot
State Instance checkpoints and the execution host Restore a private instance between jobs, and serve a program over an authenticated fetch handler Core
Compute Region discovery, ordered validation, native guards and asynchronous host effects Runs one ordinary entry on several cores and keeps the serial result Core
Compute Task pool and slice pool Run named tasks in private contexts, or slices of a call range in place on one shared memory Pools
Compute Exact chunked recurrences and certified stream folds Split one CRC-32, Adler-32 or AES-CTR stream across workers and join the chunk states Folds and Certificates
Compute Ownership certificates and the router Prove that a record loop keeps its scratch private, and pick a route per call with a stated reason Folds and Certificates
Reach Managed Workers and portable client Run entirely in Workers and Durable Objects, with binary frames and learned byte ranges between lanes Workers
Reach Browser backend Attach Web Workers to one shared memory, with page leases for transports that copy bytes Browser
Tooling Profiling and the consumer harness Time worker, validation, commit and replay cost, and time any Wasm module against a native instance of it Core

πŸ“¦ Install

bun add @gmitch215/folio
# or
npm install @gmitch215/folio

The Node backend needs Node 26 or later. Async host imports and the managed backend need WebAssembly JSPI. The browser counted-loop backend works without JSPI, and page-owner transports are portable. Builds from the main branch publish to the snapshot tag.

πŸš€ Getting Started

import { readFileSync } from 'node:fs';
import { Folio } from '@gmitch215/folio';

const folio = await Folio.open(readFileSync('./build/fixture.wasm'), { workers: 4 });
try {
  new Uint32Array(folio.memory.buffer, 1024, 37).fill(1);
  const report = await folio.run('program', [1024, 37, 10000]);
  console.log(report.mode, report.workers, report.regions);
  console.log(folio.inspect('program'));
} finally {
  await folio.close();
}

Folio.open(input, options) accepts a Uint8Array, an ArrayBuffer, a WebAssembly.Module or the result of prepare(bytes). folio.run returns a report that names the mode (serial, speculative or native-shared), the workers that took part and the reason a call stayed serial. A region runs on a worker only when its estimated work pays for dispatch, joining and validation, so a small kernel, a memory-bound loop or a serial recurrence stays at native speed. Getting Started walks through the report and the choice between routes.

🧩 Examples

The examples run against the built package with node examples/<name>.ts after bun run build:c and bun run build:

  • basic runs one entry.
  • routing reads the report and shows capped and adaptive routing.
  • iteration runs a loop of per-record calls on workers over a shared memory and compares the digests with the serial run.
  • context-reset resets a private instance between jobs.
  • task-pool joins independent jobs in order.
  • slice-pool fills one shared memory from several threads and audits the run against the serial one.
  • boot-image bakes captured memory into a module.
  • growth-governor caps memory.grow on a guest's memory and shows the guest's failed allocation.
  • memory-reuse boots a guest into the memory of one it dropped.
  • continuation runs a guest across alarm events with checkpoints in storage, including a paged guest whose memory is larger than its WebAssembly.Memory.

Advanced Usage combines these for specialised cases.

πŸ—‚οΈ Modules

Folio has seven subpaths. Each one names a runtime boundary or a feature.

Import Runs on Purpose Page
@gmitch215/folio Node Folio, the compiler, folds, certificates, the router, task and slice pools, the execution host Core
@gmitch215/folio/workers Workers and Durable Objects ManagedFolio, FolioLane, the remote client RemoteFolio and the frame and value codecs Workers
@gmitch215/folio/browser Browsers without JSPI BrowserFolio, which runs one counted-loop call on shared memory Browser
@gmitch215/folio/memory Portable Page directories and leases, the growth governor, elastic regions and memory reuse Memory
@gmitch215/folio/boot Portable Boot images: bakeModule, captureImage, applyImage, checkBaked and the image errors Boot
@gmitch215/folio/continuation Portable Wasm guests across Durable Object alarm events, with page-hash checkpoints in Durable Object SQLite Continuation
@gmitch215/folio/derived Portable The page codec, derived checkpoint stores, holders, extent restore and pricing of a memory chain Derived

Modules helps choose between them, and Configuration lists every option. Subpaths from 1.1 that are not in this table are no longer exported, and the CHANGELOG lists where each of their names moved. The generated API reference is published to GitHub Pages from bun run docs:build.

⚠️ Limits

Folio keeps the serial result and does not promise a speedup. The automatic parallel routes found no gain on independent library kernels and on three unmodified outside programs, and the explicit routes win where the caller names the independent work. Derived memory holds state the host owns, such as checkpoints, at no cost to the guest. A running guest's heap reaches it only through a translated module and a pager, which cost time: the PHP interpreter has run that way exactly in Node and locally in workerd. Limits lists what does not parallelize, the gates that were not met and the platform limits. The technical report holds the figures, the instruments and the raw samples under results.

πŸ”§ Development

bun install
bun run build:c # compile the C fixtures in tests/c with LLVM
bun run typecheck
bun run test             # unit gate
bun run test:integration # local workerd probes and the managed lanes
bun run build
bun run example

Fixtures need clang and wasm-ld with the wasm32 target. Tests need Node 26 for JSPI. bun run test:coverage runs the unit and integration projects with coverage, and bun run test:e2e deploys the probes to a Cloudflare account when FREE_CLOUDFLARE_ACCOUNT_ID and FREE_CLOUDFLARE_API_TOKEN are set. The experiments also accept FOLIO_CLOUDFLARE_ACCOUNT_ID and FOLIO_CLOUDFLARE_API_TOKEN. Layout notes live in tests/, scripts/ and experiments/.

🀝 Contributing

Open an issue before a large change. Run bun run typecheck, bun run test and bun run prettier:check before sending a pull request. Changes to the compiler need a differential test against native execution, and a performance claim needs a paired measurement with its raw samples.

πŸ“„ License

MIT. See LICENSE. The documentation favicons in docs/ are the Fluent UI Emoji tram car from Microsoft, licensed under MIT.

Releases

Packages

Used by

Contributors

Languages