Reuse the received frame deque - #64
Merged
Merged
Conversation
Motivation: Each Packet holds its own deque of parsed frames. The parser also reserved enough capacity for one frame, so every packet allocs at least once and then reallocs if packets contain subsequent frames. The frames only live between parsing and calling handleInboundPacket, so the storage doesn't need to be tied to a Packet. Modifications: - Move the deque of recevied frames from Packet to PacketParser and reserve its capacity once per connection. - Collapse the two copies of the frame-finalizing drain loop into PacketParser.cleanupReceivedFrames(). - Shrink the deque back down once drained if a packet grew it beyond 128 frames (by default), so that a peer sending a packet of single byte frames can't pin that storage for the lifetime of the connection. Result: Fewer allocations
glbrntt
requested review from
agnosticdev,
kkuk24,
rpaulo and
tfpauly
as code owners
August 3, 2026 11:13
Contributor
Author
|
This reduced the runtime of |
agnosticdev
reviewed
Aug 3, 2026
| originalLength: originalLength | ||
| ) | ||
| } | ||
| packet.framesReceived.reserveCapacity(1) |
Collaborator
There was a problem hiding this comment.
Thank you! This will show a pretty nice perf impact here. Last time I measure should be around 133 megacycles.
rpaulo
reviewed
Aug 3, 2026
| var framesReceived = NetworkUniqueDeque<QUICFrame>() | ||
|
|
||
| // Initial capacity for `framesReceived`. | ||
| private static var framesReceivedCapacity: Int { 16 } |
Contributor
There was a problem hiding this comment.
This is probably too high because most QUIC packets have only a handful of frames. What about starting with 4 ?
glbrntt
commented
Aug 3, 2026
rpaulo
approved these changes
Aug 3, 2026
kkuk24
approved these changes
Aug 3, 2026
agnosticdev
approved these changes
Aug 4, 2026
|
|
||
| func testLayoutPacket() { | ||
| let packetSize = 188 | ||
| let packetSize = 156 |
Contributor
Author
There was a problem hiding this comment.
FWIW, and I didn't look at this in any depth, but it looked like shorthandFrames may be a candidate for a similar optimization here.
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.
Motivation:
Each Packet holds its own deque of parsed frames. The parser also reserved enough capacity for one frame, so every packet allocs at least once and then reallocs if packets contain subsequent frames.
The frames only live between parsing and calling handleInboundPacket, so the storage doesn't need to be tied to a Packet.
Modifications:
Result:
Fewer allocations