Optimize decode finalize to skip the full per-field scan#93
Merged
Conversation
After decoding a message, finalize() ran a full O(nFields) second pass on every message to set defaults for absent fields and convert repeated/map builders. perfasm showed this accounting for ~10% of encode+decode samples (third-hottest method) on SimpleBenchmark. Replace the scan with a missing-bits walk on the bitmask path: defaults are set only for absent fields (fullMask & ~visitedBits, via numberOfTrailingZeros) and builders are converted only for the precomputed builderFieldIndices. The >64-field path keeps the linear scan. Behavior is unchanged; the per-field default and builder-conversion logic is factored into setDefaultField and convertBuilderField. finalize disappears from the perfasm profile and round-trip throughput on SimpleBenchmark improves ~5% (2.457 -> 2.587 ops/us, non-overlapping error bars); allocation is unchanged at 704 B/op.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After decoding a message,
finalize()ran a fullO(nFields)second pass over every message to (a) set defaults for absent fields and (b) convert repeated/map builders to their final collections.perfasmprofiling ofSimpleBenchmarkshowed this as the third-hottest method (~10% of encode+decode samples) — a non-obvious cost, since most fields are typically present and need no finalize work.Change
missing = fullMask & ~visitedBits, iterated viaLong.numberOfTrailingZeros— so defaults are set only for genuinely absent fields. Builders are converted only for a precomputedbuilderFieldIndiceslist. When all fields are present (the common case), the default loop runs zero iterations.builderFieldIndicesis not built for it).setDefaultField/convertBuilderField. Behavior is identical to the previous scan.Results
Measured with JMH (
SimpleBenchmark.proteus, encode→decode round-trip), 2 forks × 6 iterations:finalizeno longer appears in theperfasmhot-method profile.gc.alloc.rate.norm= 704 B/op.All 137 core tests pass.