Skip to content

perf(zero-swift): read pokes' rows straight into the sources' layout - #279

Merged
tmm merged 1 commit into
mainfrom
swift-perf-parse-rows
Nov 23, 2026
Merged

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

Conversation

@tmm

@tmm tmm commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Option B of the perf sweep, after #277. A poke's rows were parsed into JSON objects, decoded into RowPatchOp.puts, and then turned into Rows 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.Reader reads 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 same maxDepth. Reader.row(_:) reads an object into a layout. It tries the next column in the layout before the hashed lookup, as Row(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.
  • PokeRowReader reads a put the way zero-cache writes one ({"op":"put","tableName":…,"value":{…}}) as RowPatchOp.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, and Downstream(text:) or PokeChunk.decode reads the message from the start, so errors and their messages are unchanged.
  • KVPatchOp.put takes a KVValue, so a row read into its layout goes into the replica, and from there into the sources, as it is.
  • SQLiteStore.loadGroup reads each stored row under e/ into its table's layout the same way, and falls back to the parser for anything else.

No public API changes: PokeRowReader and Reader are internal, and RowPatchOp and KVPatchOp are package or internal.

Same behavior. PokeRowReaderTests (kept) runs the reader against the decoders on every protocol-downstream and protocol-poke-chunks vector, the protocol-downstream-fuzz, harden-downstream-text, protocol-poke-chunks-fuzz and harden-poke-bytes corpora, 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 to maxDepth reads 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.

Benchmark main this PR change
pokes: put 1 changed row, 1000-row view 12.12 µs 10.83 µs −10.7% *
pokes: put 100 changed rows, 1000-row view 313.38 µs 263.33 µs −16.0% *
pokes: put 1000 changed rows, 1000-row view 2.65 ms 2.14 ms −19.1% *
first sync: issueListV2 view, 2058 rows in 21 parts 10.72 ms 9.76 ms −8.9% *
first sync: no view, 2058 rows in 21 parts 5.73 ms 4.77 ms −16.7% *
first sync: issueListV2 view, 2058 rows in binary chunks 10.56 ms 9.82 ms −7.0% *
first sync: no view, 2058 rows in binary chunks 5.64 ms 4.79 ms −15.2% *
issue list: poke, 1 issue's title 24.08 µs 22.88 µs −5.0% *
sqlite: open, Zero.init and the issue list's page 4.45 ms 3.99 ms −10.3% *
sqlite: first sync, 2058 rows 10.01 ms 9.02 ms −9.8% *
sqlite: first sync, 2058 rows, and its write 17.00 ms 16.39 ms −3.6% *
sqlite: poke of 100 rows and its write 507.54 µs 465.29 µs −8.3% *
memory: 100,000 rows × 10 columns, with a view 75.0 MB 74.9 MB —

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 parseValue takes a starting depth.

Tests. Linux: swift test --skip E2ETests (383 tests in 62 suites, with the generated fuzz corpora from pnpm --filter zero-conformance run test), and swift format lint --strict on the changed files.

Patch changeset.

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>
@tmm
tmm merged commit 82ea01b into main Nov 23, 2026
22 checks passed
@tmm
tmm deleted the swift-perf-parse-rows branch November 23, 2026 00:29
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