Conversation
Adds ProtoArrayWriterTools.writeVarInt(byte[], offset, end, value), the writer the generated byte-array codecs will call (docs/fast-codegen-plan.md section 5.1). One-byte varints are written inline at the call site. Varints of 2 to 8 bytes are spread into bytes with shifts and masks and written with a single 8-byte little-endian store, used only when at least 8 bytes remain before the end of the message being written, so no byte the caller owns is touched; otherwise the existing unrolled writer is used. VarIntWriterBench gains pbjByteArray (the unrolled writer) and pbjByteArraySingleStore cases. Fixes #988 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
VarIntSingleStoreVariantsBench compares variants of the single-store varint writer (divide, multiply, table and Long.expand size and spread) with the unrolled writer and the inline one-byte case, for fixed and mixed varint lengths. It is the investigation behind the decision on this PR: on AMD EPYC the single store was slower than the unrolled writer for multi-byte varints. Refs #988 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
Replaces the single 8-byte store of writeVarInt's multi-byte path, which lost to the unrolled writer on AMD EPYC, with exact-size bulk stores (2, 4 or 8 bytes, or 2 + 1, 4 + 1, 4 + 2, 4 + 2 + 1), the technique of draft PR #795's SteinbornBulkByteArray. It never writes past the varint. On HAPI block items (same code otherwise, two rounds) it is 23% faster than the unrolled writer for cached writes, 15% for freshly built ones and 12% for toBytes; signed transactions are unchanged. VarIntSingleStoreVariantsBench keeps the single-store variants as the record of why they were rejected and adds the shipped writer. VarIntWriterBench gains #795's writers (SteinbornBulk, SteinbornBlended, SmartNoDataDependency) and its varint-length parameter; VarIntReaderBench takes #795's version, parameterised by varint length. Refs #988 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
|
Review follow-up: the 8-byte single store is replaced by exact-size bulk stores.
Decision run. Three jars built from the same #997 code, differing only in the multi-byte path, run back to back on AMD EPYC 9275F, two interleaved rounds, 3 forks each. ns/op (round 1 / round 2):
Against the unrolled writer, the bulk stores are 23% faster for cached block-item writes, 15% for fresh ones and 12% for Other changes:
|
Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
anthony-swirldslabs
left a comment
There was a problem hiding this comment.
LGTM. Just one comment with a suggestion.
|
Only two minor comments: writeVarInt/writeVarIntWide doesn't have a bench, which is a different commit but seems relevant since this introduces ProtoArrayWriterToolsVarIntTest. Second is running |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Review follow-ups
Changes from lower in the stack are merged up into every PR above them. Across the series, every Verified on every branch: |
| * | ||
| * @param output The byte array to write to | ||
| * @param offset The offset to start writing at | ||
| * @param end The end of the message being written; no byte at or after it is modified |
There was a problem hiding this comment.
It isn't clear how this "end" arg is actually used. What if "writeVarInt(out, offset, offset + 1, value)" is called, but a value is more than 1 byte long? According to the JavaDoc, the method should not write past offset + 1, but in reality it will (as writeVarIntWide() ignores the boundary completely).
The current test doesn't cover this scenario
…e bytes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jasper Potts <1466205+jasperpotts@users.noreply.github.com>
54aac16 to
1a77625
Compare
6a9810b to
c8321e0
Compare
Performance (AMD EPYC 9275F): against the unrolled varint writer, writing HAPI block items is 23% faster with cached sizes (250 → 199 ns), 15% faster freshly built (391 → 328 ns) and 12% faster for
toBytes(425 → 373 ns); signed transactions are a tie. Tier A, on by default. The multi-byte path uses exact-size bulk stores; the 8-byte single store this PR first proposed lost on HAPI, and the sections below record that decision (final table in the review comments).What and why
Adds
ProtoArrayWriterTools.writeVarInt(byte[] output, int offset, int end, long value), the varint writer the generated byte-array writer calls (#989). It followsdocs/fast-codegen-plan.mdsection 5.1:writeVarIntstays well under HotSpot's 35-bytecode inline limit.end, the end of the message being written, so no byte the caller owns is ever touched.VarIntWriterBenchgainspbjByteArray(the unrolledwriteUnsignedVarInt) andpbjByteArraySingleStore.Generated code is unchanged in this PR; #989 is the first caller.
Tests
ProtoArrayWriterToolsVarIntTest, for values of every length from 1 to 10 bytes plus random and negative values:writeUnsignedVarInt;offset;end, for everyendfrom the varint's end to 8 bytes past it.pbj-core:spotlessApply qualityGate buildpasses.pbj-integration-tests:./gradlew buildand-PpbjPerformance=maxboth pass, 326,323 tests each.History: the rejected single store. AMD EPYC 9275F,
VarIntWriterBench, 3 forks, ns per varintwriteUnsignedVarInt)writeVarInt)This misses the issue's expectation. In isolation, the single store wins only for 1-byte varints. That win comes from the inline 1-byte case, and in protobuf every tag and most lengths are 1 byte. For 2 to 8 bytes it costs a flat ~2.9 ns per varint. In this benchmark every value has the same length, so the unrolled writer's
switchis perfectly predicted, and the position update forms a dependency chain through the size calculation.The plan's evidence (12-23% faster whole-message encoding) was about whole-message encoding, not isolated varints. So the decision is being made on real encoding: the same PR 5 code built three ways, differing only in the multi-byte path (single store, unrolled, or
Long.expand, which is PDEP on Zen 5), measured onBlockItemBenchwrites. The results and the decision are below.Generated code size
Unchanged (no generated-code change).
Stacked on #992.
Fixes #988
🤖 Generated with Claude Code
History: decision data, real HAPI encoding on AMD EPYC
Three jars were built from the same code (the #997 stack), differing only in the multi-byte path of
writeVarIntWide. All three keep the inline 1-byte case. They ran back to back on the decision platform: 3 forks, 3 × 1 s warmup, 5 × 1 s measurement,-prof gc.BlockItemBench(ns/op)writeUnsignedVarIntLong.expand(PDEP)All three allocate 0 B/op for cached writes.
Result: on HAPI block items, the single 8-byte store is 4-9% slower than the unrolled writer behind the same inline 1-byte case. This contradicts the plan's section 9 note (from a prototype on a different message set).
Proposal:
writeVarInt(output, offset, end, value)and its inline 1-byte case. That case is where the win is: 0.3 vs 1.0 ns per varint, and most tags and lengths are 1 byte.endparameter, so a future store-based path, for example in the reverse writer (plan step 3) where the buffer owns the bytes below each varint, needs no API change.This will be a follow-up commit on this branch, rebased up the stack, unless reviewers prefer otherwise. The plan document should also be updated so section 9 no longer states single-store as a general win.