Commit 5498fa4
committed
perf(rt): add ByteBuffer-backed stream to avoid Data copies on the NIO 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.1 parent 78da1cd commit 5498fa4
4 files changed
Lines changed: 669 additions & 18 deletions
File tree
- Sources
- SmithySwiftNIO
- Smithy
- Tests/SmithySwiftNIOTests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
53 | 73 | | |
54 | 74 | | |
55 | 75 | | |
56 | 76 | | |
57 | 77 | | |
58 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
59 | 89 | | |
60 | 90 | | |
61 | 91 | | |
| |||
0 commit comments