Skip to content

Commit 323b0fb

Browse files
authored
Merge pull request #53 from apple/agnosticdev/InboundStopping
SwiftQUIC: Reduce CPU overhead in getting inbound stream data delivered to the stream
2 parents 942e416 + b62090c commit 323b0fb

4 files changed

Lines changed: 34 additions & 9 deletions

File tree

Sources/SwiftNetwork/Protocols/ManyToManyProtocol.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,15 @@ extension ManyToManyApplicationStreamProtocol where Flow: AutomaticUpperStreamPr
13881388
guard let flow = self.flow(for: flowID) else { throw NetworkError.posix(EINVAL) }
13891389
flow.serviceUpperReceiveQueue()
13901390
}
1391+
// Enqueue and delivery the stream data all in one shot
1392+
public func deliverInboundStreamData(
1393+
flow flowID: MultiplexedFlowIdentifier,
1394+
streamData: consuming FrameArray
1395+
) throws(NetworkError) {
1396+
guard var flow = self.flow(for: flowID) else { throw NetworkError.posix(EINVAL) }
1397+
try flow.addToUpperReceiveQueue(streamData)
1398+
flow.serviceUpperReceiveQueue()
1399+
}
13911400
}
13921401

13931402
@_spi(ProtocolProvider)
@@ -1581,6 +1590,16 @@ extension ManyToManyApplicationDatagramProtocol where Flow: AutomaticUpperDatagr
15811590
guard let flow = self.flow(for: flowID) else { throw NetworkError.posix(EINVAL) }
15821591
flow.serviceUpperReceiveQueue()
15831592
}
1593+
1594+
// Enqueue and delivery the datagrams all in one shot
1595+
public func deliverInboundDatagrams(
1596+
flow flowID: MultiplexedFlowIdentifier,
1597+
datagrams: consuming FrameArray
1598+
) throws(NetworkError) {
1599+
guard var flow = self.flow(for: flowID) else { throw NetworkError.posix(EINVAL) }
1600+
try flow.addToUpperReceiveQueue(datagrams)
1601+
flow.serviceUpperReceiveQueue()
1602+
}
15841603
}
15851604

15861605
@available(Network 0.1.0, *)
@@ -1613,6 +1632,16 @@ extension HeterogeneousManyToManyProtocolHandler where SecondaryFlow: AutomaticU
16131632
guard let flow = self.secondaryFlow(for: flowID) else { throw NetworkError.posix(EINVAL) }
16141633
flow.serviceUpperReceiveQueue()
16151634
}
1635+
1636+
// Enqueue and delivery the datagrams all in one shot
1637+
public func deliverInboundDatagrams(
1638+
flow flowID: MultiplexedFlowIdentifier,
1639+
datagrams: consuming FrameArray
1640+
) throws(NetworkError) {
1641+
guard var flow = self.secondaryFlow(for: flowID) else { throw NetworkError.posix(EINVAL) }
1642+
try flow.addToUpperReceiveQueue(datagrams)
1643+
flow.serviceUpperReceiveQueue()
1644+
}
16161645
}
16171646

16181647
@_spi(ProtocolProvider)

Sources/SwiftNetwork/QUIC/QUICConnection.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,8 +2374,7 @@ public final class QUICConnection: ManyToManyApplicationStreamProtocol,
23742374
continue
23752375
}
23762376

2377-
try? enqueueInboundStreamData(flow: flowID, streamData: frameArray)
2378-
try? deliverEnqueuedInboundStreamData(flow: flowID)
2377+
try? deliverInboundStreamData(flow: flowID, streamData: frameArray)
23792378

23802379
// When the stream is already in `resetReceived` state,
23812380
// it should be closed when we receive STOP_SENDING, so we
@@ -4317,8 +4316,7 @@ public final class QUICConnection: ManyToManyApplicationStreamProtocol,
43174316
}
43184317
if let frameArray = stream.dequeueReassembledData(connection: self) {
43194318
do {
4320-
try enqueueInboundStreamData(flow: flowID, streamData: frameArray)
4321-
try deliverEnqueuedInboundStreamData(flow: flowID)
4319+
try deliverInboundStreamData(flow: flowID, streamData: frameArray)
43224320
sendFrames()
43234321
} catch {
43244322
log.error("Error sending frames on stream close: \(error)")
@@ -5564,8 +5562,7 @@ extension QUICConnection {
55645562

55655563
var frame = frame.frame
55665564
frame.metadataComplete = true
5567-
try? enqueueInboundDatagrams(flow: matchingFlowIdentifier, datagrams: .init(frame: frame))
5568-
try? deliverEnqueuedInboundDatagrams(flow: matchingFlowIdentifier)
5565+
try? deliverInboundDatagrams(flow: matchingFlowIdentifier, datagrams: .init(frame: frame))
55695566
return true
55705567
}
55715568
}

Sources/SwiftNetwork/QUIC/QUICStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ struct StreamListMembership: OptionSet {
328328
// Also note that a flow identifier can exist in multiple lists at one time.
329329
@available(Network 0.1.0, *)
330330
struct QUICStreamList: ~Copyable {
331-
private var list: [MultiplexedFlowIdentifier] = []
331+
private var list = Deque<MultiplexedFlowIdentifier>()
332332
private let name: StaticString
333333
private let listType: StreamListMembership
334334

Tests/SwiftNetworkTests/TestMultiplexingProtocol.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ final class TestMultiplexingProtocol: ManyToManyApplicationDatagramProtocol, Man
9191
return
9292
}
9393
accessReceivedDatagrams(path: path) { frames in
94-
try? enqueueInboundDatagrams(flow: flow, datagrams: frames.drainArray())
95-
try? deliverEnqueuedInboundDatagrams(flow: flow)
94+
try? deliverInboundDatagrams(flow: flow, datagrams: frames.drainArray())
9695
}
9796
}
9897

0 commit comments

Comments
 (0)