Skip to content

Commit 7b3059c

Browse files
committed
Use Deque for finalized output
Motivation: SwiftNetworkQUICConnection has a finalized output arrray which stores ByteBuffer's. Users must call 'nextOutboundPacket' to drain the array. However it pops from the front which is linear in the size of the array. Modifications: - Use a Deque and call 'popFront()' rather than checking the container is non-empty and then calling 'removeFirst()' - Remove the 'throws' from 'nextOutboundPacket' as it never throws Result: - Simpler, cheaper code
1 parent e04f0d6 commit 7b3059c

3 files changed

Lines changed: 16 additions & 26 deletions

File tree

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let package = Package(
8484
.product(name: "X509", package: "swift-certificates"),
8585
.product(name: "SwiftTLS", package: "swift-tls"),
8686
.product(name: "SwiftNetwork", package: "swift-network-evolution"),
87+
.product(name: "DequeModule", package: "swift-collections"),
8788
.target(name: "ChildChannelMultiplexer"),
8889
],
8990
swiftSettings: swiftSettings

Sources/NIOQUIC/QUICConnectionChildChannelStateMachine.swift

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -434,28 +434,19 @@ extension QUICConnectionChildChannelStateMachine {
434434
private mutating func drainFinalizedOutputAndHandleLifecycle(into actions: inout Actions) {
435435

436436
// Drain.
437-
do {
438-
self.logger.trace("QUICConnectionChildChannelStateMachine drainFinalizedOutputAndHandleLifecycle loop")
439-
var shouldFlush = false
440-
while let buffer = try self.quicConnection.nextOutboundPacket() {
441-
actions.append(
442-
.parentChannelWrite(
443-
message: AddressedEnvelope(remoteAddress: self.remoteAddress, data: buffer),
444-
promise: nil
445-
)
437+
self.logger.trace("QUICConnectionChildChannelStateMachine drainFinalizedOutputAndHandleLifecycle loop")
438+
var shouldFlush = false
439+
while let buffer = self.quicConnection.nextOutboundPacket() {
440+
actions.append(
441+
.parentChannelWrite(
442+
message: AddressedEnvelope(remoteAddress: self.remoteAddress, data: buffer),
443+
promise: nil
446444
)
447-
shouldFlush = true
448-
}
449-
if shouldFlush {
450-
actions.append(.childChannelFlush)
451-
}
452-
} catch {
453-
self.logger.trace(
454-
"QUICConnectionChildChannelStateMachine drainFinalizedOutputAndHandleLifecycle caught error",
455-
metadata: ["error": "\(error)"]
456445
)
457-
let promise = self.close()
458-
actions.append(.childChannelEncounterError(error: error, promise: promise))
446+
shouldFlush = true
447+
}
448+
if shouldFlush {
449+
actions.append(.childChannelFlush)
459450
}
460451

461452
// Lifecycle.

Sources/NIOQUIC/SwiftNetwork/SwiftNetworkQUICConnection.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Crypto
16+
import DequeModule
1617
import Logging
1718
@_spi(CustomByteBufferAllocator) import NIOCore
1819
import NIOQUICHelpers
@@ -105,7 +106,7 @@ final class SwiftNetworkQUICConnection {
105106

106107
private var connectionStateMachine = QUICConnectionStateMachine()
107108

108-
private var finalizedOutput: [NIOCore.ByteBuffer] = []
109+
private var finalizedOutput: Deque<ByteBuffer> = []
109110
private var inputPacketQueue: FrameArray = FrameArray(capacity: 10)
110111
private var newlyConnectedStreams: Set<QUICStreamID> = []
111112
private var networkContext: NetworkContext
@@ -1126,11 +1127,8 @@ final class SwiftNetworkQUICConnection {
11261127
///
11271128
@discardableResult
11281129
@inlinable
1129-
func nextOutboundPacket() throws -> NIOCore.ByteBuffer? {
1130-
guard !self.finalizedOutput.isEmpty else {
1131-
return nil
1132-
}
1133-
return self.finalizedOutput.removeFirst()
1130+
func nextOutboundPacket() -> ByteBuffer? {
1131+
self.finalizedOutput.popFirst()
11341132
}
11351133

11361134
/// Returns stream IDs for newly connected streams.

0 commit comments

Comments
 (0)