Provide an opt-in zero-copy response body view - #2322
Open
pavel-ptashyts wants to merge 2 commits into
Open
Conversation
Byte-array consumers currently pay for an aggregate copy even when a response has a single body part. Add an explicit read-only view accessor so callers can opt into sharing while the existing accessor retains its defensive-copy contract. Reuse the view for string decoding and cover eager, lazy, multipart, and third-party Response implementations. Refs AsyncHttpClient#2321 Codex on behalf of Pavel Ptashyts Co-Authored-By: OpenAI Codex <noreply@openai.com>
Mockito 4 cannot invoke interface default methods through CALLS_REAL_METHODS on JDK 21 and newer. Invoke the Response default method explicitly so delegation remains covered on every supported JDK. Refs AsyncHttpClient#2321 Codex on behalf of Pavel Ptashyts Co-Authored-By: OpenAI Codex <noreply@openai.com>
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
Response#getResponseBodyAsBytesView()as a compatible default methodwith an explicit possibly-shared, read-only contract
aggregate copy without exposing
ByteBufstoragegetResponseBodyAsBytes(),getResponseBodyAsByteBuffer(), andgetResponseBodyAsStream()centralized
Closes #2321.
Compatibility and safety
The default implementation delegates to
getResponseBodyAsBytes(), so existingthird-party
Responseimplementations keep their current behavior. Netty usesthe optimized path only for exactly one body part. Empty and multipart bodies
continue through the existing aggregation path.
Tests cover eager identity and repeated access, defensive-copy isolation,
heap and direct lazy slices, reader/writer indices, reference counts, empty
bodies, multipart ordering and split UTF-8 characters, stream isolation, and
default-method delegation.
Verification
mvnw.cmd -B -ntp -pl client -Dtest=NettyAsyncResponseTest testmvnw.cmd clean verifyAllocation benchmark
JMH 1.37, Corretto 11.0.32.1, one fork, three 500 ms warmups, five
500 ms measurements,
gcprofiler. Values are allocated bytes per operation,rounded to whole bytes; zero means below the profiler's resolution.
getResponseBodyAsBytes()getResponseBodyAsBytesView()The eager single-part view measured about 2.3 ns/op regardless of body size,
versus 322 ns/op for 4 KiB and 11.0 us/op for 128 KiB defensive copies. Lazy
single-part allocation is halved as expected. Multipart bodies retain the
payload-sized aggregation allocation.
Codex on behalf of Pavel Ptashyts