Skip to content

perf(zero-rust): sort the stored replica's rows on Zero::prepare's thread - #271

Merged
tmm merged 1 commit into
mainfrom
rust-perf-prepare-sort
Nov 20, 2026
Merged

tmm merged 1 commit into
mainfrom
rust-perf-prepare-sort

Conversation

@tmm

@tmm tmm commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Follows #258 and #266, toward the open item from #258: Zero::prepare couldn't take more of the launch off the app's thread, because the replica's map, the sources and the views are built on Rc. Nothing public changes, and there's no unsafe.

What changed. After #266, most of what Zero::from_prepared did on the app's thread was loading the replica into the query sources: taking each table's rows out of the map and sorting them by primary key. Rows are Objects, whose fields are behind Arc, so they're already Send. Zero::prepare now does that work on its own thread, from the rows it read (SortedRows). The app's thread puts each table's sorted rows into its source's tree in one pass (IvmBranch::load_sorted, MemorySource::push_sorted_adds) and makes the replica's map as before. IvmBranch::load, which Zero::new uses, sorts the same way first, so there is one way to load. A table the schema doesn't have, and the synced aggregates' rows, still load through apply.

One comparator. The sort on prepare's thread and the sources' own comparator share ivm::comparator, which is generic over anything that borrows an Object. make_comparator is Rc::new(comparator::<Row>(..)), so the sources compare rows with the same code as before. A first version wrapped a comparator of objects in one of rows. That made updating a live query of 100,000 structs after an optimistic update 5.9% slower on macOS (196 → 208 µs, every run) and hydrating 1,000 and 10,000 rows 2–3% slower. With the generic comparator these measure 197 µs, −2.0% and −0.4%.

An alternative I measured and dropped. Making KvMap's nodes Arc would let prepare build the map too. On Linux that took another 0.3 ms off from_prepared at 8,097 rows, and on macOS nothing. But it made pokes and mutations 3–8% slower on Linux (ranges apart), because every write's path copy pays atomic reference counts.

Numbers. cold start in zero-benchmarks (#263): zbugs' launch on a SQLite store. Medians of 5 interleaved runs of this branch's code, with the range across runs.

Replica macOS before macOS after Linux before Linux after
8,097 rows from_prepared 3.83 ms [3.74–4.10] 2.06 ms [1.90–2.09] 9.61 ms 6.16 ms
2,058 rows from_prepared 2.17 ms [1.89–2.29] 1.62 ms [1.33–1.87] 3.65 ms 2.80 ms
8,097 rows Zero::new 22.7 ms [22.6–22.8] 22.6 ms [22.5–22.8] 38.8 ms 36.6 ms
8,097 rows prepare and from_prepared at once 22.8 ms 22.8 ms 38.7 ms 36.1 ms

macOS is a Mac satellite (M4 Pro VM), and Linux is an orb, which is noisier. On macOS, every other benchmark (hydration, pokes, mutations, the first sync and the issue list) is within ±1.5% of main, and none has its range apart from main's. On Linux they're within ±2% in both directions.

What stays on the app's thread at 8,097 rows: an Rc for each row, the sources' trees, the replica's map, and hydrating the first views.

Tests. load_loads_what_applying_adds_loads loads a replica with load and with apply of an add for every entry, and compares every source's rows. It covers keys out of primary-key order (a number key, 10 before 9), a two-column primary key, a table the schema doesn't have, a synced aggregate row, and keys outside e/. With the sort removed, it fails.

Run locally (Linux, 1.99.1): cargo fmt --check, clippy -D warnings for the workspace with and without --all-features and for zero-client alone, cargo test --workspace with and without --all-features, the 1.88 MSRV check, conformance with --require-generated (66,384 of 66,384 cases), and oxfmt.

Patch changeset.

…read

Zero::from_prepared spent most of the app's thread loading the replica
into the query sources: taking each table's rows out of the map and
sorting them by primary key. Zero::prepare now does both on its own
thread, from the rows it read, which are already Send (SortedRows). The
app's thread puts each table's sorted rows into its source's tree in one
pass, and makes the replica's map as before.

IvmBranch::load, which Zero::new uses, sorts the same way first, so
there is one way to load: it leaves the sources as applying an add of
every entry does. A table the schema doesn't have, and the synced
aggregates' rows, still load through apply.

The sort on prepare's thread and the sources' own comparator are one
comparator, generic over rows and objects, so the sources compare rows
with the same code as before. A first version wrapped a comparator of
objects in one of rows, which made a live query of 100,000 rows 6%
slower to update on macOS.

The map stays on Rc. Making its nodes Arc, so that prepare could build
it too, took 0.3 ms more off from_prepared at 8,097 rows on Linux and
nothing on macOS, but made pokes and mutations 3-8% slower on Linux.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f9b1-9025-7230-acfd-43f5d849f594
Co-authored-by: tmm <tmm@tmm.dev>
@tmm
tmm force-pushed the rust-perf-prepare-sort branch from 18886d1 to fdecf05 Compare November 20, 2026 19:24
@tmm
tmm merged commit e79729e into main Nov 20, 2026
19 checks passed
@tmm
tmm deleted the rust-perf-prepare-sort branch November 20, 2026 19:37
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