-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Read InputStream bodies straight into the buffer, no staging array #2233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pavel-ptashyts
wants to merge
3
commits into
AsyncHttpClient:main
Choose a base branch
from
maygemdev:perf/inputstream-body-no-staging-copy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5f49e19
Read InputStream bodies straight into the buffer, no staging array
pavel-ptashyts 98ab3a1
Merge branch 'main' into perf/inputstream-body-no-staging-copy
pavel-ptashyts 730f158
Address review: drop the writableBytes() - 10 margin, fix docs and tests
pavel-ptashyts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
122 changes: 122 additions & 0 deletions
122
...rc/test/java/org/asynchttpclient/request/body/generator/InputStreamBodyGeneratorTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright (c) 2026 AsyncHttpClient Project. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.asynchttpclient.request.body.generator; | ||
|
|
||
| import io.github.artsok.RepeatedIfExceptionsTest; | ||
| import io.netty.buffer.ByteBuf; | ||
| import io.netty.buffer.Unpooled; | ||
| import org.asynchttpclient.request.body.Body; | ||
| import org.asynchttpclient.request.body.Body.BodyState; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.Random; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Covers {@link InputStreamBodyGenerator}'s {@link Body#transferTo(ByteBuf)}, which now reads straight from | ||
| * the stream into the target buffer (dropping the per-call staging {@code byte[]} and copy for heap buffers). | ||
| * The whole stream must still be transferred byte-for-byte, CONTINUE while data remains and STOP at EOF. | ||
| */ | ||
| public class InputStreamBodyGeneratorTest { | ||
|
|
||
| private static final int CHUNK_SIZE = 1024 * 8; | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void streamsAllBytesAcrossMultipleReads() throws IOException { | ||
| final byte[] src = new byte[3 * CHUNK_SIZE + 42]; | ||
| new Random().nextBytes(src); | ||
|
|
||
| Body body = new InputStreamBodyGenerator(new ByteArrayInputStream(src)).createBody(); | ||
| ByteBuf chunkBuffer = Unpooled.buffer(CHUNK_SIZE); | ||
| ByteArrayOutputStream collected = new ByteArrayOutputStream(); | ||
| try { | ||
| BodyState state; | ||
| while ((state = body.transferTo(chunkBuffer)) != BodyState.STOP) { | ||
| assertEquals(BodyState.CONTINUE, state, "a stream with data left must report CONTINUE"); | ||
| byte[] b = new byte[chunkBuffer.readableBytes()]; | ||
| chunkBuffer.readBytes(b); | ||
| collected.write(b); | ||
| chunkBuffer.clear(); | ||
| } | ||
| assertArrayEquals(src, collected.toByteArray(), "the whole stream must be transferred unchanged"); | ||
| } finally { | ||
| chunkBuffer.release(); | ||
| body.close(); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void singleReadDrainsASmallStream() throws IOException { | ||
| final byte[] src = new byte[CHUNK_SIZE - 100]; // fits in one writable region, so one read drains it | ||
| new Random().nextBytes(src); | ||
|
|
||
| Body body = new InputStreamBodyGenerator(new ByteArrayInputStream(src)).createBody(); | ||
| ByteBuf chunkBuffer = Unpooled.buffer(CHUNK_SIZE); | ||
| try { | ||
| assertEquals(BodyState.CONTINUE, body.transferTo(chunkBuffer)); | ||
| assertEquals(src.length, chunkBuffer.readableBytes(), "one read should drain a small stream"); | ||
| chunkBuffer.clear(); | ||
| assertEquals(BodyState.STOP, body.transferTo(chunkBuffer), "body at EOF"); | ||
| } finally { | ||
| chunkBuffer.release(); | ||
| body.close(); | ||
| } | ||
| } | ||
|
|
||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void emptyStreamStopsImmediately() throws IOException { | ||
| Body body = new InputStreamBodyGenerator(new ByteArrayInputStream(new byte[0])).createBody(); | ||
| ByteBuf chunkBuffer = Unpooled.buffer(CHUNK_SIZE); | ||
| try { | ||
| assertEquals(BodyState.STOP, body.transferTo(chunkBuffer), "an empty stream must STOP immediately"); | ||
| assertEquals(0, chunkBuffer.readableBytes(), "nothing should be written for an empty stream"); | ||
| } finally { | ||
| chunkBuffer.release(); | ||
| body.close(); | ||
| } | ||
| } | ||
|
|
||
| // Locks the removal of the old "writableBytes() - 10" margin: with a writable region of 10 or fewer bytes the | ||
| // margin made the transfer length 0 (or negative), so it silently STOPped without writing / threw. The stream | ||
| // must now still be drained through a tiny target buffer. | ||
| @RepeatedIfExceptionsTest(repeats = 5) | ||
| public void smallWritableRegionStillTransfers() throws IOException { | ||
| final byte[] src = new byte[25]; | ||
| new Random().nextBytes(src); | ||
|
|
||
| Body body = new InputStreamBodyGenerator(new ByteArrayInputStream(src)).createBody(); | ||
| ByteBuf chunkBuffer = Unpooled.buffer(10, 10); // writableBytes() == 10, the old margin's boundary | ||
| ByteArrayOutputStream collected = new ByteArrayOutputStream(); | ||
| try { | ||
| BodyState state; | ||
| while ((state = body.transferTo(chunkBuffer)) != BodyState.STOP) { | ||
| assertEquals(BodyState.CONTINUE, state, "a stream with data left must report CONTINUE"); | ||
| byte[] b = new byte[chunkBuffer.readableBytes()]; | ||
| chunkBuffer.readBytes(b); | ||
| collected.write(b); | ||
| chunkBuffer.clear(); | ||
| } | ||
| assertArrayEquals(src, collected.toByteArray(), "the whole stream must drain through a tiny buffer"); | ||
| } finally { | ||
| chunkBuffer.release(); | ||
| body.close(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: the existing tests in this package (
ByteArrayBodyGeneratorTestandFeedableBodyGeneratorTest) use@RepeatedIfExceptionsTest(repeats = 5)on every test method, so using plain@Testbreaks the local convention.ByteArrayBodyGeneratorTestalso already has the same single-read and multi-read scaffold (same chunk size, same3 * CHUNK_SIZE + 42sizing, and the same drain loop), whileFeedableBodyGeneratorTestprovides areadFromBodyhelper that covers the short-stream cases. It would be worth aligning with the existing annotation and reusing the existing test patterns rather than introducing a second drain idiom to the package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done