Repository navigation
perf(zero-swift): share each synced row between the replica and the IVM sources - #277
Merged
Merged
Conversation
…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>
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.
The replica kept each synced row as a JSON object, and each IVM source kept its own
Rowbuilt 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/asRows 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(.jsonor.row) is the replica's value type, inKVMap,KVDiffOp,KVWriteandStorageOp.ReplicaStore.apply,SQLiteStore.loadGroupandReplicaDatabase.initClienttake the branch's layouts (IVMBranch.layouts) and keep each table's rows in them. Aggregates undera/and other keys stay JSON.IVMBranch.applypushes the replica's rows. ATransactionwrites the sameRowit pushes to its source, which replaces theTransaction.rowshand-off.JSONSerializer.stringify(_: Row)). The first version built aJSONObjectfrom 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,StorageOpand 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 thejson-parseobjects and all 67json-stringify-stringsvectors used as column names and values, plus nested values, NaN and an array-index key. It gives the same text asstringify(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.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 aRow, 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) andswift format lint --stricton the changed files.Patch changeset.