Registry Mirror
Skip to content

Conversation

@jasperpotts

@jasperpotts jasperpotts commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

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 follows docs/fast-codegen-plan.md section 5.1:

  • One-byte varints are written inline at the call site. writeVarInt stays well under HotSpot's 35-bytecode inline limit.
  • 2- to 8-byte varints are spread into bytes with shifts and masks and written with one 8-byte little-endian store. That happens only when at least 8 bytes remain before end, the end of the message being written, so no byte the caller owns is ever touched.
  • Everything else (9- and 10-byte varints, or less than 8 bytes of room) uses the existing unrolled writer.

VarIntWriterBench gains pbjByteArray (the unrolled writeUnsignedVarInt) and pbjByteArraySingleStore.

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:
    • output identical to writeUnsignedVarInt;
    • correct returned length;
    • nothing written before offset;
    • nothing written at or after end, for every end from the varint's end to 8 bytes past it.
  • pbj-core: spotlessApply qualityGate build passes.
  • pbj-integration-tests: ./gradlew build and -PpbjPerformance=max both pass, 326,323 tests each.

History: the rejected single store. AMD EPYC 9275F, VarIntWriterBench, 3 forks, ns per varint

Varint length Unrolled (writeUnsignedVarInt) Single store (writeVarInt)
1 byte 1.0 0.3
2 bytes 1.2 2.9
3 bytes 1.4 2.9
4 bytes 1.8 2.9
5 bytes 2.0 2.9
8 bytes 2.6 2.9

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 switch is 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 on BlockItemBench writes. 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) Single store (this PR) Unrolled writeUnsignedVarInt Long.expand (PDEP)
block item write, sizes cached 278.6 ± 8.1 252.3 ± 4.2 (-9.4%) 253.3 ± 5.1 (-9.1%)
block item write, freshly built 410.0 ± 9.8 383.7 ± 12.9 (-6.4%) 397.7 ± 18.3 (-3.0%)
signed transaction write, cached 33.5 ± 0.7 31.1 ± 0.2 (-7.0%) 33.3 ± 1.1
signed transaction write, fresh 46.2 ± 1.3 44.3 ± 1.0 (-4.2%) 46.1 ± 0.8

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:

  • Keep 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.
  • Make the multi-byte path the existing unrolled writer, and drop the 8-byte store.
  • Keep the end parameter, 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.

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>
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

JUnit Test Report

   550 files  +  1     550 suites  +1   47s ⏱️ +2s
 2 051 tests +256   2 047 ✅ +256   4 💤 ±0  0 ❌ ±0 
11 583 runs  +512  11 555 ✅ +512  28 💤 ±0  0 ❌ ±0 

Results for commit 1a77625. ± Comparison against base commit 6a9810b.

♻️ This comment has been updated with latest results.

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>
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Integration Test Report

    621 files  ±0      621 suites  ±0   45m 4s ⏱️ + 2m 17s
163 855 tests ±0  163 855 ✅ ±0  0 💤 ±0  0 ❌ ±0 
326 429 runs  ±0  326 429 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 1a77625. ± Comparison against base commit 6a9810b.

♻️ This comment has been updated with latest results.

jasperpotts and others added 3 commits October 29, 2026 19:42
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>
@jasperpotts

Copy link
Copy Markdown
Member Author

Review follow-up: the 8-byte single store is replaced by exact-size bulk stores.

writeVarInt(output, offset, end, value) keeps its inline one-byte case. For two or more bytes it now uses stores of exactly the varint's size (2, 4 or 8 bytes, or 2 + 1, 4 + 1, 4 + 2, 4 + 2 + 1): the bulk technique from draft PR #795's SteinbornBulkByteArray. It never writes past the varint, so the end guard that was the main suspect for the single store's loss is no longer needed. The parameter stays, so a store-based writer can use it later.

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):

BlockItemBench single store unrolled exact-size bulk
block item write, cached 272 / 275 250 / 264 199 / 197
block item write, fresh 409 / 408 391 / 381 328 / 328
block item toBytes, fresh 460 / 455 425 / 423 373 / 369
signed transaction write, cached 34 / 36 31 / 32 32 / 33
signed transaction write, fresh 45 / 46 46 / 47 46 / 46

Against the unrolled writer, the bulk stores are 23% faster for cached block-item writes, 15% for fresh ones and 12% for toBytes; signed transactions are a tie. They also matched the unrolled writer's output byte for byte on 2,000,000 random values.

Other changes:

  • VarIntSingleStoreVariantsBench keeps the single-store variants, as the record of why they were rejected, and adds the shipped writer.
  • VarIntWriterBench gains 789 varint perf #795's SteinbornBulkByteArray, SteinbornBlendedByteArray and SmartNoDataDependencyByteArray, and its varint-length parameter.
  • VarIntReaderBench takes 789 varint perf #795's version, parameterised by varint length.
  • The plan (section 5.1, section 9) now records these results.

ProtoArrayWriterToolsVarIntTest still covers every length from 1 to 10 bytes and the neverWritesAtOrPastEnd sweep; thanks for calling it out. All of it passes against the bulk writer.

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>
This was referenced Nov 2, 2026

@anthony-swirldslabs anthony-swirldslabs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just one comment with a suggestion.

@ldintr

ldintr commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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 ./gradlew jmh -Pjmh.includes='VarIntReaderBench. I'm not sure if that was used during development but it takes 8hrs to run on my mac. Another variant that takes only a few minutes to try out optimizations would be nice. Code wise everything seems fine

@jasperpotts

Copy link
Copy Markdown
Member Author

Review follow-ups

  • writeVarIntWide computes the second byte only after the 2-byte return.

Changes from lower in the stack are merged up into every PR above them. Across the series, every var the PRs added is now an explicit type; each review thread has a reply saying what changed.

Verified on every branch: pbj-core qualityGate build, plus the integration build with options off and with -PpbjPerformance=max, and the JMH sources compiled in both configurations. A few gRPC socket tests (GrpcCloseClientConnectionTest, PbjGrpcWebServiceHardeningTest, DyingServerTest) failed now and then on this machine and passed when re-run, on branches with and without these changes.

*
* @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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@jasperpotts
jasperpotts force-pushed the 988-single-store-varint branch from 54aac16 to 1a77625 Compare November 30, 2026 22:22
@jasperpotts
jasperpotts force-pushed the 987-fast-codegen-options branch from 6a9810b to c8321e0 Compare December 1, 2026 19:04
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.

4 participants