Skip to content

Reserve the result once in StreamEndpointFlowProtocol.read(minimumBytes:maximumBytes:) - #376

Open
rnro wants to merge 2 commits into
apple:mainfrom
rnro:content-read-reserve
Open

rnro wants to merge 2 commits into
apple:mainfrom
rnro:content-read-reserve

Conversation

@rnro

@rnro rnro commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

The content form of receive built a fresh [UInt8] for every frame through Deserializer.buffer(_:length:), appended it to returnBuffer, and let returnBuffer double with no reserve. For >

Changed:

  • Reserved returnBuffer once from FrameArray.unclaimedLength before reading.
  • Appended each frame's bytes straight from Frame.bytes instead of through a per-frame array.
  • Returned nil for an empty FrameArray up front; a frame with no bytes still yields an empty array.
  • Added SwiftNetworkEndpointFlowContentReadTests covering frames of different sizes, a maximumBytes smaller than the data, and an empty write.
  • Lowered the EndpointFlowContentReceive_16KB thresholds in each toolchain directory under Thresholds/.

Result:

  • The read allocates one exactly sized array per call, in place of 16 for 12 frames; EndpointFlowContentReceive_16KB goes from 25 to 10 mallocCountTotal and from 24 to 9 objectAllocCount.
  • thresholds check fails if the read goes back to a per-frame array.
  • The returned bytes, their order, and the finalizing of every frame are unchanged.
  • The new tests pin that behavior; they do not measure allocations.

rnro added 2 commits November 27, 2026 12:23
`QUICStreamEcho_*` read at the QUIC stream with `readAndDrop()`, so nothing gated the allocations made by `NetworkConnection.receive(atLeast:atMost:)` as it runs `StreamEndpointFlowProtocol.read(minimumBytes:maximumBytes:in:)`.

Changed:
* Added the `EndpointFlowContentReceive_16KB` scenario to `QUICBenchmarks`.
* Added `ContentReceiveHarness`, a `NoTransport` connection over `CustomLink` on a virtual clock that is handed bytes frame by frame.
* Recorded the scenario's thresholds in each toolchain directory under `Thresholds/`.

Result:
* A 16 KB payload delivered as 12 frames of 1448 bytes is read with one `receive`; the measured window is that call and one turn of the scheduler.
* The counts did not vary across five runs on one toolchain, so a change in the allocations of the read beyond the default allowances fails `thresholds check`.
* The read allocates 25 times on macOS and 41 times on Linux. Every Linux window pays a fixed overhead of about 16 allocations, as `QUICIdle_10s` shows.
* The `latest-beta` thresholds were recorded on macOS and the other directories from CI.
…tes:maximumBytes:)`

The content form of `receive` built a fresh `[UInt8]` for every frame through `Deserializer.buffer(_:length:)`, appended it to `returnBuffer`, and let `returnBuffer` double with no reserve. For a 16 KB write at MTU 1500 this made 16 allocations on the reading side: 12 per-frame arrays plus 4 growths of `returnBuffer`.

Changed:
* Reserved `returnBuffer` once from `FrameArray.unclaimedLength` before reading.
* Appended each frame's bytes straight from `Frame.bytes` instead of through a per-frame array.
* Returned `nil` for an empty `FrameArray` up front; a frame with no bytes still yields an empty array.
* Added `SwiftNetworkEndpointFlowContentReadTests` covering frames of different sizes, a `maximumBytes` smaller than the data, and an empty write.
* Lowered the `EndpointFlowContentReceive_16KB` thresholds in each toolchain directory under `Thresholds/`.

Result:
* The read allocates one exactly sized array per call, in place of 16 for 12 frames; `EndpointFlowContentReceive_16KB` goes from 25 to 10 `mallocCountTotal` and from 24 to 9 `objectAllocCount` on macOS, and from 41 to 26 and from 40 to 25 on Linux.
* `thresholds check` fails if the read goes back to a per-frame array.
* The returned bytes, their order, and the finalizing of every frame are unchanged.
* The new tests pin that behavior; they do not measure allocations.
@rnro
rnro force-pushed the content-read-reserve branch from 57eb92e to a5c1cea Compare November 27, 2026 17:25
@rnro rnro added the 🔨 semver/patch No public API change. label Nov 27, 2026
@rnro
rnro marked this pull request as ready for review November 27, 2026 17:39
let sixteenKilobytePayload = [UInt8](repeating: 0xCD, count: 16 * 1024)

/// The payload of a TCP segment on a 1500 byte MTU with the timestamp option.
let tcpSegmentSize = 1448

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is the TCP segment size relevant in a QUIC benchmark?

} else {
returnBuffer?.append(contentsOf: buffer)
if let bytes = frame.bytes {
bytes.withUnsafeBytes { returnBuffer.append(contentsOf: $0) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One reason to use the read.buffer in the previous version was to avoid the unsafe here. Can we still do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants