Repository navigation
Description
Problem
PBJ is planning a series of performance changes to its generated protobuf codecs and runtime (fast writer, faster
parsing, a reverse writer, zero-copy views, unboxed oneofs). The full specification is in
docs/fast-codegen-plan.md (added by this
issue's PR). A hand-written prototype of the generated code for the block-stream schema produced exactly PBJ's bytes
and was 2.6× faster to write, 2.9× faster to parse, and 5.8× faster to read every field than PBJ 0.16.4 on AMD EPYC.
Before any of that can be reviewed with confidence, the repo is missing the harness to prove each step is correct and
faster:
- No per-item block benchmark.
SampleBlockBenchparses and writes a whole block, reusing the same model objects.
PBJ cachesprotobufSize()in the model, so repeated writes of the same objects time only the write and never the
measuring pass. Block items are Hiero's hottest serialization path, and there is no benchmark that measures them
one at a time with realistic (freshly built, unmeasured) objects. - No allocation assertions. Nothing fails if a change adds garbage to the write or parse paths.
- No test of non-canonical field order. Every generated test feeds the parser protoc's canonical bytes (fields in
ascending order). Upcoming parser work adds in-order fast paths with a general fallback loop; the fallback is only
exercised by out-of-order input, and in the prototype only an out-of-order test found a bug there. - No way to run the integration suite with the new options on. Later steps are opt-in, so the 100k+ protoc
comparison tests, the MaxDepth / MaxSize / UnknownFields / Malformed / Truncated tests and the fuzz tests must also
run with every option enabled. - No recorded baseline on the decision platform (AMD EPYC, x86-64) to compare each later PR against.
Proposal
Harness only. No change to generated codecs or the runtime.
- Commit
docs/fast-codegen-plan.md, the specification for the series, so reviewers have the full context. BlockItemBench(JMH,pbj-integration-tests/src/jmh). Loads everyBlockItemfrom the two sample
src/jmh/resources/*.blk.gzfiles and measures, per item:- write to
byte[]with objects rebuilt before every invocation (@Setup(Level.Invocation)) soprotobufSizeis
not cached; toBytes;- parse from
Bytes; - the same parse and write for the signed transactions found in those blocks.
Later PRs addwriteReverseand view-reading cases.
- write to
- Generated non-canonical-order test.
TestGeneratoradds a test to every generated<Message>Test: take
protoc's bytes for each test object, reverse the order of its top-level fields and append an unknown field
(field 99, varint). Parsing must give the same object as the canonical bytes (with unknown fields ignored), and
strict mode must reject the unknown field. It runs against today'sparseImpland must pass now. AllocationTestscaffolding in the integration tests: a helper that warms up (10k iterations) and measures
com.sun.management.ThreadMXBean.getCurrentThreadAllocatedBytes()around 10k iterations. First assertion:
write(T, byte[], int)of HAPI objects whose size is already cached. Today's figure is recorded; if it is not
already 0, the test records the current value as the ceiling and step 1 (PR 3) tightens it to 0.
Parse allocation per test message is recorded as a ceiling that must not grow.- Options-on build property.
./gradlew build -PpbjPerformance=maxinpbj-integration-tests. The options
themselves land in the next PR (option plumbing), so in this PR the property is accepted and wired up as far as
it can be; the next PR connects it topbj { performance = "max" }. CI runs the integration build in both
configurations. - Baseline numbers on AMD EPYC 9275F (Zen 5):
ProtobufObjectBench(Block and Everything),SampleBlockBench,
VarIntWriterBench,VarIntReaderBenchandBlockItemBench, 3 forks,-prof gc; plus the generated-code size of
the full HAPI build. These go in the PR and are the "before" for the next PR.
Generated code before and after for one HAPI message: unchanged in codecs and models; each generated test class
(for example BlockItemTest) gains the non-canonical-order test method.
API and compatibility
- Tier: none (test and benchmark harness only).
- New Gradle property for the integration-test build only:
-PpbjPerformance=max(default: off). - No effect on generated code, the runtime, or Hiero.
Scope
In: the plan document, BlockItemBench, the non-canonical-order generated test, AllocationTest scaffolding, the
options-on property and CI job, baseline numbers.
Left for later PRs: the option flags themselves (PR 1), every codec and runtime change (PRs 2-11), and the
writeReverse / view benchmark cases (added by the PRs that introduce them).
Acceptance criteria
cd pbj-core && ./gradlew spotlessApply build qualityGatepasses.cd pbj-integration-tests && ./gradlew buildpasses, including the new non-canonical-order test for every
generated message andAllocationTest../gradlew build -PpbjPerformance=maxpasses../gradlew jmhJarbuilds andBlockItemBenchruns.- The PR reports baseline tables measured on AMD EPYC (3 forks,
-prof gc, ns/op and B/op) and the HAPI generated
class size (find build/classes -name '*.class' -print0 | xargs -0 cat | wc -c). - No expected performance change (harness only); the generated
.classsize ofsrc/mainis unchanged.
Dependencies
None. First in the series described in docs/fast-codegen-plan.md section 8.
Activity
test: protoc and size cases, fresh-object allocation check, longer wa…
test: add ProtobufObjectArrayBench from draft PR #795
test: allocate in cached benchmarks' setup as fresh ones do
docs: update the plan with measured varint, step 1, GC and step 2b fi…
test: keep collections out of BlockItemBench's timed region
docs: record the BlockItemBench GC and cache-warmth findings in the plan