Architecture decision record · ADR-0020

The IronCalc WASI module: a CI artifact with a batch call

The shim is a Rust crate in wasm/ironcalc-wasi, not a spike.

Status
Accepted
Deciders
Joe Gaudet
Created
Updated
PRD
PRD
Related
None

Context

  • Changes: ADR 0007, items 2 and 3 of its decision.
  • Code: wasm/ironcalc-wasi, ruby/lattice-grid/lib/lattice/grid/collab/iron_calc*.rb, python/lattice-grid/src/lattice_grid/collab/ironcalc.py.

ADR 0007 put one IronCalc module, built for wasm32-wasip1, in the Ruby and Python servers. The module was the spike shim in spikes/ironcalc/wasi-shim. Its C ABI had no call to insert or delete rows and columns, to rename a sheet, to read the cell type or to free a workbook. Thus the Ruby and Python collab servers loaded the full doc into a new instance on each submit, with one call for each cell. At 10,000 cells, one submit took 1.4 s in Python. The Ruby gem had a copy of the .wasm file (3.6 MB) that nothing built again.

Decision drivers

Not recorded in the original ADR.

Options considered

Not recorded in the original ADR.

Decision

  1. The shim is a Rust crate in wasm/ironcalc-wasi, not a spike. It uses ironcalc_base 0.9.1 from crates.io and pins Rust 1.90.0 in rust-toolchain.toml. The safe logic is in src/book.rs, with Rust unit tests. The ic_* exports in src/lib.rs are thin wrappers.
  2. The .wasm file is not in git (.gitignore). The wasm job in .github/workflows/ci.yml runs cargo test and builds the file. It caches the cargo registry and target/ by the hash of Cargo.lock, and always builds, so a change in src/ never gives an old file. The ruby, python and python-postgres jobs download the file and test with it.
  3. The gem and the wheel include the file that CI builds:
    • Gem: wasm/ironcalc_wasi.wasm is in spec.files. gem build stops with a clear message when the file is not there. Bundler also reads the gemspec, so only gem build checks.
    • Wheel and sdist: lattice_grid/collab/ironcalc.wasm goes in through hatch artifacts. hatch_build.py stops a sdist or wheel build without the file. An editable install does not need it.
  4. wasmtime stays a soft dependency: the Gemfile development group for Ruby, the collab extra for Python. LATTICE_IRONCALC_WASM still overrides the path.
  5. New exports: ic_close, ic_insert_rows, ic_insert_cols, ic_delete_rows, ic_delete_cols, ic_rename_sheet, ic_remove_sheet, ic_cell_type and ic_apply_batch. ic_get_raw gives e:<code> for an error, so a host does not read the text "#N/A" as an error.
  6. ic_apply_batch takes a JSON array of ops (set, insertRows, insertCols, deleteRows, deleteCols, addSheet, renameSheet, removeSheet, setName), evaluates one time and gives [[sheet, row, col, typed], ...] for each cell whose value changed. The module keeps the last value of each cell and moves these keys on insert, delete and sheet removal. Thus one submit is one call, and the reply holds only the changed cells.
  7. The Ruby IronCalc and the Python IronCalcEngine keep the workbook across submits. They port #apply of packages/formula-ironcalc/src/engine.ts: their own row and column IDs, the range shrink on delete, and the real sheet names. apply takes only the ops, as the TypeScript FormulaEngine does. After an engine error, the doc server loads the doc again.
  8. Action Cable runs the commands of one connection on a pool of threads, so a message can run before subscribed, and a submit can run before the join that the client sent first. ChannelHandler runs the messages of one channel one at a time under a lock. It holds the messages that arrive before the first join (100 at most) and sends them on after the join. The Python ASGI server reads the messages of one socket in order, and Connection.receive chains each message after the one before. It does not have the problem, and a test shows this.

Consequences

  • A local run of the Ruby or Python collab tests needs the module first: cargo build --release --target wasm32-wasip1 in wasm/ironcalc-wasi. The test setup finds the file in target/ there.
  • A cold CI build of the crate takes some minutes. The cache makes the next builds fast.
  • The release file is about 3.8 MB (the spike file was 3.6 MB).
  • A client that sends messages and never joins gets no reply for its first 100 messages on Action Cable. After that, the server rejects them with not_joined.
  • Not done: the precompiled module cache of ADR 0007 item 3. Each process still compiles the module one time at first use (about 1 s).

Confirmation

Evidence

The benchmark is one setCell submit on a 1,000 × 10 doc (9,000 numbers and 1,000 SUM range formulas), through the full doc server. It is the median of 10 submits after one warm-up. The specs assert less than 250 ms, so a slow CI runner does not fail.

Server Before (load on each submit) After (ic_apply_batch)
Ruby 1,368 ms 5 ms to 22 ms
Python 3,910 ms 4.5 ms

The host had a high load during the "before" runs. A one-off check of structural submits on the same doc (process CPU time, because the host load made wall time not useful) gave about 13 ms (Ruby) and 20 ms (Python) for one insertRows, and about 50 ms (Ruby) and 38 ms (Python) for one deleteRows. In Ruby, about 20 ms of a delete is in the engine and about 13 ms is in the doc model. Run the benchmarks:

cd ruby/lattice-grid && bundle exec rspec spec/lattice/grid/collab/doc_server_benchmark_spec.rb
cd python/lattice-grid && .venv/bin/pytest tests/test_collab_bench.py -s

The cross-implementation fixtures (ruby/lattice-grid/spec/fixtures/collab/collab.json and python/lattice-grid/tests/fixtures/collab.json) pass with the new engines.

Interview record

Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.

QuestionAnswerSource
Where does the text of this page come from?From the Markdown file docs/adr/0020-ironcalc-wasi-module.md at commit 4c77ac4. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so.Document: docs/adr/0020-ironcalc-wasi-module.md

Changelog

DateAuthorChange
2026-12-02Joe GaudetStatus in the Markdown file: accepted (maintainer decisions, 2026-12-02)
2026-12-03Joe GaudetCommit 7ce4af2: Lattice 1.0 (#1)
2026-12-04Joe Gaudet (agent)Converted from docs/adr/0020-ironcalc-wasi-module.md to this page. The text is the text of the Markdown file.

The adr skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.