perf(rt): add ByteBuffer-backed stream to avoid Data copies on the NI…#1106
Open
jbelkins wants to merge 1 commit into
Open
perf(rt): add ByteBuffer-backed stream to avoid Data copies on the NI…#1106jbelkins wants to merge 1 commit into
jbelkins wants to merge 1 commit into
Conversation
…O path The SwiftNIO transport round-trips every streamed byte through Foundation.Data, which forces extra copies that NIO-native SDKs avoid. On download the response bridge copies ByteBuffer -> [UInt8] -> Data -> BufferedStream (3 copies/chunk); on upload it copies Data -> a freshly allocated ByteBuffer (1 alloc + 1 copy/chunk). The write side is also synchronous and unbounded, so a fast producer cannot be throttled and the in-memory buffer can grow without limit. This change is additive only; no existing public API changes: - Smithy: add WriteableStream.writeAsync(contentsOf:) as a protocol-extension default that bridges to the existing synchronous write, so every current conformer compiles unchanged. (A distinct name is used rather than an async overload of write(contentsOf:), which would be source-breaking because an async context would prefer the overload and force existing call sites to await.) - SmithySwiftNIO: add ByteBufferStream, a Stream-conforming type backed by a FIFO of NIOCore.ByteBuffers. Reads vend ByteBuffer slices via readSlice (advancing the readerIndex, no memmove); writes keep the producer's ByteBuffer (copy-on-write). The async write applies high/low-watermark backpressure so the buffer stays bounded. The Data-returning protocol methods still work, performing a single boundary copy only for legacy consumers. - SmithySwiftNIO: SwiftNIOHTTPClientStreamBridge uses ByteBufferStream on the response path and prefers a zero-copy ByteBuffer slice on the request path via an `as?` downcast, leaving the default Data path unchanged. Measured with an identical-work consumer (release build): the download path goes from ~1230 MiB/s to ~6640 MiB/s (5.4x), within ~5% of the zero-copy ceiling; upload improves ~1.4x. Known follow-ups (not in this change): task-cancellation handlers on the async suspensions, and migrating the checksum/chunked middlewares that currently hard-code BufferedStream.
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.
…O path
The SwiftNIO transport round-trips every streamed byte through Foundation.Data, which forces extra copies that NIO-native SDKs avoid. On download the response bridge copies ByteBuffer -> [UInt8] -> Data -> BufferedStream (3 copies/chunk); on upload it copies Data -> a freshly allocated ByteBuffer (1 alloc + 1 copy/chunk). The write side is also synchronous and unbounded, so a fast producer cannot be throttled and the in-memory buffer can grow without limit.
This change is additive only; no existing public API changes:
as?downcast, leaving the default Data path unchanged.Measured with an identical-work consumer (release build): the download path goes from ~1230 MiB/s to ~6640 MiB/s (5.4x), within ~5% of the zero-copy ceiling; upload improves ~1.4x.
Known follow-ups (not in this change): task-cancellation handlers on the async suspensions, and migrating the checksum/chunked middlewares that currently hard-code BufferedStream.
Issue #
Description of changes
Scope
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.