Reserve repeated Vec across wire-tag decode loop

perfloop/prost · ALLOCATION HOT LOOP

https://perfloop.ai/t/oss/case_pfmqga57j8

Verdict

VERIFIED · settled 2026-08-09 · pull request opened as tokio-rs/prost#1454

What happened: The paired measurements met the required improvement.

Hypothesis

The indexed-symbol lookup located `prost/src.message.merge_repeated` in `prost/src/encoding.rs:829-843`; the accepted workload path `path_83zqcz7tb6` reaches its `merge_repeated -> message.merge` hop and identifies one repeated element construction and append per invocation. The submitted mechanism predicts that repeated wire-tag dispatches reuse the destination Vec and can trigger growth reallocations during high-cardinality repeated-message decode; allocation rate and copied bytes remain unmeasured. Proof target: benchmark/decode a message with increasing counts of repeated nested-message fields, recording allocator calls/allocated bytes (and, if available, realloc/copy behavior); the reserved variant should reduce allocation traffic while preserving decoded output.

Change to test: In message::merge_repeated, reserve capacity on the destination Vec before push using a conservative bound from buf.remaining() and a minimum encoded element size, so successive appends for one repeated field do not reallocate on nearly every wire occurrence.

Where it lives

perfloop/prost · prost/src/encoding.rs

Evidence

repeated-message-decode-64 · 10 sample pairs

metric baseline candidate paired median change confidence range required result
allocation_calls/op 5 1 −80% (−4) −4 to −4 < 0 PASSED
reallocation_calls/op 4 0 −100% (−4) −4 to −4 < 0 PASSED
requested_allocation_bytes/op 496 256 −48.4% (−240) −240 to −240 < 0 PASSED

repeated-message-decode-4096 · 10 sample pairs

metric baseline candidate paired median change confidence range required result
allocation_calls/op 11 3 −72.7% (−8) −8 to −8 < 0 PASSED
reallocation_calls/op 10 2 −80% (−8) −8 to −8 < 0 PASSED
requested_allocation_bytes/op 32752 28672 −12.5% (−4080) −4080 to −4080 < 0 PASSED

repeated-message-decode-latency-4096 · 10 sample pairs

metric baseline candidate paired median change confidence range required result
latency_ns/op 49315 51005 +3.4% (+1655) +1281 to +2316 ≤ 3000 PASSED

Checks: 8 of 8 passed. Verification: no defect found.

Timeline