Skip to content

Avoid extra byte array copying when downloading to memory with AsyncResponseTransformer #6348

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

Merged

Conversation

davidh44
Copy link
Contributor

Motivation and Context

Currently ByteArrayAsyncResponseTransformer invokes ResponseBytes.fromByteArray() once all the data is downloaded, which copies the contents of the byte array when creating ResponseBytes

Modifications

replace ResponseBytes.fromByteArray() with ResponseBytes.fromByteArrayUnsafe(), which creates ResponseBytes without copying the contents of the byte array

Testing

Performance tests showed increased throughput and decrease in latency

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

@davidh44 davidh44 requested a review from a team as a code owner August 14, 2025 18:16
@@ -47,7 +47,7 @@ public final class ByteArrayAsyncResponseTransformer<ResponseT> implements
@Override
public CompletableFuture<ResponseBytes<ResponseT>> prepare() {
cf = new CompletableFuture<>();
return cf.thenApply(arr -> ResponseBytes.fromByteArray(response, arr));
return cf.thenApply(arr -> ResponseBytes.fromByteArrayUnsafe(response, arr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we add a comment over here on why we are using fromByteArrayUnsafe for future references (while doing code walks)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, added comment

@davidh44 davidh44 enabled auto-merge August 14, 2025 18:28
@davidh44 davidh44 added the api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team label Aug 14, 2025
Copy link

@davidh44 davidh44 added this pull request to the merge queue Aug 14, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 14, 2025
@davidh44 davidh44 added this pull request to the merge queue Aug 14, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 14, 2025
@davidh44 davidh44 added this pull request to the merge queue Aug 14, 2025
Merged via the queue into master with commit 0473e13 Aug 14, 2025
47 of 49 checks passed
Copy link

This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2025
@davidh44 davidh44 deleted the hdavidh/async-response-transformer-to-bytes-avoid-extra-copy branch August 14, 2025 21:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants