Skip to content

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

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

Follow up to #6348

More optimizations in ByteArrayAsyncResponseTransformer to avoid unnecessary copying of data

Modifications

  • Replace future return type of byte[] to ByteBuffer in ByteArrayAsyncResponseTransformer
  • New subclass DirectAccessByteArrayOutputStream that extends ByteArrayOutputStream, with method toByteBuffer() to access the internal buffer directly without copying
  • New method ResponseBytes.fromByteBufferUnsafe() to create ResponseBytes from ByteBuffer without copying

ByteArrayAsyncResponseTransformer.BaosSubscriber

  • in onNext(), if byteBuffer.hasArray() is true, then we can safely invoke byteBuffer.array() and directly write the content to ByteArrayOutputStream instead of copying the bytes with BinaryUtils.copyBytesFrom()
  • in onComplete(), get the internal buffer from DirectAccessByteArrayOutputStream without copying, using toByteBuffer() instead of toByteArray()

Testing

Added unit tests
Existing tests passing
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 18, 2025 22:18
@davidh44 davidh44 enabled auto-merge August 19, 2025 00:39
@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 19, 2025
Copy link

@davidh44 davidh44 added this pull request to the merge queue Aug 19, 2025
Merged via the queue into master with commit 08bae37 Aug 19, 2025
40 of 41 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 19, 2025
@davidh44 davidh44 deleted the hdavidh/async-response-transformer-to-bytes-avoid-extra-copies branch August 19, 2025 02:39
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.

2 participants