Allocate ByteArrayBuilder past-block list lazily - #1675
Open
pjfanning wants to merge 3 commits into
Open
Conversation
Most instances never overflow the first block, so the list was allocated and never used. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0161vnbY6rGfPFAYBMnbv64c
pjfanning
force-pushed
the
perf-bytearraybuilder-3x
branch
from
August 24, 2026 09:52
d205e38 to
1b5ae50
Compare
Member
|
Sounds good, just needs |
Added new feature contributions and updates for version 3.3.0.
Member
Author
I have now added an entry to the file |
Contributor
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.
ByteArrayBuilderallocated_pastBlocks = new ArrayList<>()in its fieldinitializer, for every instance. The list is only needed once content overflows
the first block — which for most instances never happens (
ParserBase's binarydecoding buffer,
JsonStringEncoder'sfromInitial(...)slow path,Base64Variant.decode(String)), since the first block is 500 bytes or arecycled
BYTE_WRITE_CONCAT_BUFFER.Now created on first
_allocMore().reset(),toByteArray()and theisEmpty()check are null-guarded; once created, the list survivesreset()soa reused builder does not re-allocate it.
Size of the win — deliberately modest
Worth stating plainly:
new ArrayList<>()does not eagerly allocate abacking array — it uses the shared
DEFAULTCAPACITY_EMPTY_ELEMENTDATAsentineluntil the first
add(). So what is saved per instance is theArrayListobjectheader itself, ~24 bytes, not a 40-element array. This is an allocation-count /
young-gen-pressure change, not something a throughput benchmark will show
clearly. I have not benchmarked it and am not claiming a measurable speedup.
The case for it is that it is free: no API change, no behaviour change, and the
null checks are on paths that are either cold (
toByteArray(),reset()) oralready doing an allocation (
_allocMore()).Test
New test
testMultipleBlocksAndReuse()covers the paths this change alters:single block (list never created), overflow across several blocks, and reuse of
the same builder afterwards.
Full suite green (1837 tests).
No release-notes entry, as there is no issue number for this yet.