Repository navigation
perf(zero-rust): sort the stored replica's rows on Zero::prepare's thread - #271
Merged
Merged
Conversation
…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
force-pushed
the
rust-perf-prepare-sort
branch
from
November 20, 2026 19:24
18886d1 to
fdecf05
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.
Follows #258 and #266, toward the open item from #258:
Zero::preparecouldn't take more of the launch off the app's thread, because the replica's map, the sources and the views are built onRc. Nothing public changes, and there's nounsafe.What changed. After #266, most of what
Zero::from_prepareddid 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 areObjects, whose fields are behindArc, so they're alreadySend.Zero::preparenow 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, whichZero::newuses, 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 throughapply.One comparator. The sort on prepare's thread and the sources' own comparator share
ivm::comparator, which is generic over anything that borrows anObject.make_comparatorisRc::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 nodesArcwould let prepare build the map too. On Linux that took another 0.3 ms offfrom_preparedat 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 startinzero-benchmarks(#263): zbugs' launch on a SQLite store. Medians of 5 interleaved runs of this branch's code, with the range across runs.from_preparedfrom_preparedZero::newprepareandfrom_preparedat oncemacOS 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
Rcfor each row, the sources' trees, the replica's map, and hydrating the first views.Tests.
load_loads_what_applying_adds_loadsloads a replica withloadand withapplyof an add for every entry, and compares every source's rows. It covers keys out of primary-key order (a number key,10before9), a two-column primary key, a table the schema doesn't have, a synced aggregate row, and keys outsidee/. With the sort removed, it fails.Run locally (Linux, 1.99.1):
cargo fmt --check, clippy-D warningsfor the workspace with and without--all-featuresand forzero-clientalone,cargo test --workspacewith and without--all-features, the 1.88 MSRV check, conformance with--require-generated(66,384 of 66,384 cases), and oxfmt.Patch changeset.