Repository navigation
perf(zero-swift): read pokes' rows straight into the sources' layout - #279
Merged
Merged
Conversation
A poke's rows were parsed into JSON objects, decoded, and then turned into `Row`s in their table's layout for the replica and the IVM sources. Now `PokeRowReader` reads a `put` as zero-cache writes it straight into the table's layout, with no object in between, for pokes sent as text and as binary chunks. - `JSONParser.Reader` reads JSON a part at a time: the brackets, keys and strings a decoder expects, and the values between them with the parser, counting toward the same nesting limit. `Reader.row` reads an object into a layout, trying the next column before the lookup. - `PokeRowReader` reads `put`s of tables whose columns have the same names on the server and whose values only have the table's columns (`RowPatchOp.putRow`), and every other op as the decoder does. For any text it doesn't read as a poke part that decodes, it returns nil, and the decoder reads the message from the start, with its errors. - `KVPatchOp.put` takes a `KVValue`, so a row read into its layout goes into the replica as it is. - `SQLiteStore.loadGroup` reads a stored row into its layout the same way. `PokeRowReaderTests` checks the reader against the decoders on every downstream and poke-chunk vector and fuzz corpus, with layouts in and out of the vectors' column order. 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.
Option B of the perf sweep, after #277. A poke's rows were parsed into JSON objects, decoded into
RowPatchOp.puts, and then turned intoRows in their table's layout, the rows the replica and the IVM sources share since #277. This change reads them straight into that layout, with no object in between, for pokes sent as text frames and as binary chunks (protocol 52+), and for rows read back from a stored replica.JSONParser.Readerreads JSON a part at a time: a decoder reads the brackets, keys and strings it expects itself, and the values between them with the parser, which counts them toward the samemaxDepth.Reader.row(_:)reads an object into a layout. It tries the next column in the layout before the hashed lookup, asRow(object, layout:)does since perf(zero-swift): share each synced row between the replica and the IVM sources #277, and frees a value replaced by a repeated key without recursion.PokeRowReaderreads aputthe way zero-cache writes one ({"op":"put","tableName":…,"value":{…}}) asRowPatchOp.putRow. It does this when the table's columns have the same names on the server and the value has only the table's columns. It reads every other op, and every other key of a part, as the decoder does. For any text it doesn't read as a poke part that decodes, it returns nil, andDownstream(text:)orPokeChunk.decodereads the message from the start, so errors and their messages are unchanged.KVPatchOp.puttakes aKVValue, so a row read into its layout goes into the replica, and from there into the sources, as it is.SQLiteStore.loadGroupreads each stored row undere/into its table's layout the same way, and falls back to the parser for anything else.No public API changes:
PokeRowReaderandReaderare internal, andRowPatchOpandKVPatchOpare package or internal.Same behavior.
PokeRowReaderTests(kept) runs the reader against the decoders on everyprotocol-downstreamandprotocol-poke-chunksvector, theprotocol-downstream-fuzz,harden-downstream-text,protocol-poke-chunks-fuzzandharden-poke-bytescorpora, and hand-written edge cases: keys out of order, given twice or escaped, unknown columns, trailing text, a BOM, and malformed bodies. It uses three layouts: every column in the vectors' order, reversed, and half the columns. Whenever the reader reads a message, the decoder must accept it and give the same parts. A scratch run over the vectors and corpora (before the hand-written cases were added) read 2,577 text messages and 4,851 chunk sets, 4,079 of their rows into layouts. It also checks that a row value nested tomaxDepthreads on a 512 KB thread, and that one level more is left to the decoder, which rejects it. Mutating the reader showed the test catches the mistakes that matter: accepting trailing text, taking the next column without comparing its name, and leaving an op object open.Numbers.
ZeroBenchmarks, Release, on a Mac satellite (M4 Pro). Main (5097b0d) and this branch ran in turns, 3 rounds of--time 0.5. The ranges are the min and max of the round medians;*means they don't overlap. The binary-chunk first-sync benchmarks are new in this PR: zero-cache sends this client's pokes that way.A 5-round run of pokes, first sync and SQLite open and first sync gave the same picture: −8.5% to −16.7% on pokes, −8.8% to −16.6% on first sync, −9.4% on open and −9.8% on SQLite first sync, all with ranges that don't overlap.
Benchmarks that run none of this code (views materialized from clients filled by mutations) came out +3% to +5.5% in the 3-round run: zero.bench's lower 500 rows, hydration of 1000 rows, and hydration of 100,000 rows as structs. In 5 rounds of just those groups, every range overlapped main's, with medians from −0.9% to +6.7%. The largest, zero.bench's lower 500 rows, has swung between −0.5% and +7% across this sweep's runs. The only change near them is that the parser's
parseValuetakes a starting depth.Tests. Linux:
swift test --skip E2ETests(383 tests in 62 suites, with the generated fuzz corpora frompnpm --filter zero-conformance run test), andswift format lint --stricton the changed files.Patch changeset.