Skip to content

perf(zero-swift): share each synced row between the replica and the IVM sources - #277

Merged
tmm merged 1 commit into
mainfrom
swift-perf-shared-rows
Nov 22, 2026
Merged

tmm merged 1 commit into
mainfrom
swift-perf-shared-rows

Conversation

@tmm

@tmm tmm commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

The replica kept each synced row as a JSON object, and each IVM source kept its own Row built from it, so every synced row lived in memory twice. The memory benchmark's 100,000 rows of 10 columns took 127 MB, against 83 MB in zero-rust.

The replica now keeps the rows under e/ as Rows in their table's layout, the one the sources use. The sources take those rows as they are, so the replica and the sources share each row.

  • KVValue (.json or .row) is the replica's value type, in KVMap, KVDiffOp, KVWrite and StorageOp. ReplicaStore.apply, SQLiteStore.loadGroup and ReplicaDatabase.initClient take the branch's layouts (IVMBranch.layouts) and keep each table's rows in them. Aggregates under a/ and other keys stay JSON.
  • IVMBranch.apply pushes the replica's rows. A Transaction writes the same Row it pushes to its source, which replaces the Transaction.rows hand-off.
  • SQLite writes a row straight from its columns (JSONSerializer.stringify(_: Row)). The first version built a JSONObject from each row first, which made a 100-row poke with its write 16.5% slower.
  • Row(object, layout:) tries the column after the last one it found before the layout's hashed lookup. Stored rows now come back in layout order, so at open almost every column hits. Building a row from an object in layout order went from 32.7 to 17.8 µs per 100 rows, and objects in other orders stay where they were (about 31 µs).

No public API changes: KVValue, StorageOp and the new initializers are internal or package.

Same behavior. Stored rows are written in layout order rather than in the server's key order. Objects compare without regard to key order, and the text reads back as the same object. A scratch test (not committed) checked stringify(_: Row) against the json-parse objects and all 67 json-stringify-strings vectors used as column names and values, plus nested values, NaN and an array-index key. It gives the same text as stringify(JSONValue(row)) when the row's layout follows the object's order, and text that parses back to an equal object under a reversed layout or one holding half the columns.

Numbers. ZeroBenchmarks, Release, on a Mac satellite (M4 Pro). Main at 1d67ff0 (4432dd0 after it only bumps versions) and this branch ran in turns, 3 rounds of --time 0.5. The ranges are the min and max of the 3 round medians; * means they don't overlap.

Benchmark main this PR change
memory: 100,000 rows × 10 columns, with a view 127.0 MB 74.9 MB −41.0% *
pokes: put 1 changed row, 1000-row view 13.00 µs 12.50 µs −3.8% *
pokes: put 100 changed rows, 1000-row view 374.25 µs 331.79 µs −11.3% *
pokes: put 1000 changed rows, 1000-row view 3.30 ms 2.82 ms −14.5% *
mutations: optimistic update, 1000-row view 9.71 µs 8.62 µs −11.2% *
mutations: round trip, 1000-row view 29.79 µs 28.67 µs −3.8% *
issue list: optimistic update, 1 issue's title 25.71 µs 23.67 µs −7.9% *
issue list: poke, 1 issue's title 26.83 µs 26.00 µs −3.1% *
sqlite: optimistic update and its write 50.46 µs 48.08 µs −4.7% *
sqlite: poke of 100 rows and its write 550.38 µs 558.33 µs +1.4%
sqlite: open, Zero.init and the issue list's page 4.89 ms 5.01 ms +2.5%
sqlite: first sync, 2058 rows, and its write 18.81 ms 19.55 ms +3.9%
first sync: no view, 2058 rows 6.03 ms 6.25 ms +3.7%
first sync: issueListV2 view, 2058 rows 11.55 ms 11.80 ms +2.1%
zero.bench.ts: lower 500 rows 42.46 µs 43.83 µs +3.2%
hydration: materialize 1000 / 10,000 / 100,000 rows 32.81 µs / 284.46 µs / 3.38 ms 32.27 µs / 289.33 µs / 3.42 ms −1.7% / +1.7% / +1.1%

The slowdowns are all within run-to-run ranges. An earlier run of this PR without the Row(object, layout:) change had open at +5.5–6% and first sync without a view at +3.8–5%, with ranges that didn't overlap. That cost came from converting every synced row into a Row, even in tables no query reads, and freeing its JSON object. A parser that writes rows straight into their layout would remove it, but that is a separate change.

Tests. Linux: swift test --skip E2ETests (380 tests in 61 suites, conformance vectors included) and swift format lint --strict on the changed files.

Patch changeset.

…VM sources

The replica kept each synced row as a JSON object, and each IVM source
kept its own `Row` of it, so 100,000 rows of 10 columns took 127 MB. The
replica now keeps the rows under `e/` as `Row`s in their table's layout,
the one the sources use, and the sources take those rows as they are.

- `KVValue` (`.json` or `.row`) is the replica's value. `ReplicaStore.apply`,
  `SQLiteStore.loadGroup` and `ReplicaDatabase.initClient` take the
  branch's layouts and keep a table's rows in them.
- `IVMBranch.apply` pushes the replica's rows, and a `Transaction` writes
  the row it pushes, so `Transaction.rows` is gone.
- SQLite writes a row straight from its columns
  (`JSONSerializer.stringify(_: Row)`), not made into an object first.
- `Row(object, layout:)` tries the column after the last one it found
  before the layout's lookup: stored rows now come back in layout order.

Stored rows are written in layout order. Objects compare without regard
to key order, so what reads back is the same.

Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf
Co-authored-by: tmm <tmm@tmm.dev>
@tmm
tmm merged commit 06c2aa1 into main Nov 22, 2026
22 checks passed
@tmm
tmm deleted the swift-perf-shared-rows branch November 22, 2026 22:13
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.

2 participants