perf(quic): round-4 perf-audit fixes — ACK gating, flow-control dirty-set, streams list view

Four perf wins from the round-4 audit, all in the steady-state hot path
(audio rooms run at ~50 datagrams/sec/participant).

Perf #1 — ACK frame gating (saves a frame + bandwidth on every drain):
  AckTracker.buildAckFrame now returns null when no new ack-eliciting
  packet has arrived since the last build. Pre-fix the writer emitted a
  redundant ACK frame on every outbound packet (~50/sec each direction)
  even when the only inbound traffic since last drain was ACK-only.
  RFC 9000 §13.2 only requires ACKs in response to ack-eliciting
  packets within max_ack_delay; gating on ackElicitingPending satisfies
  that without delay-timer machinery.

Perf #11 — AckTracker.purgeBelow short-circuit:
  Common case: peer ACKs a high PN, we already pruned below it. Pre-fix
  triggered a full ListIterator walk anyway. Now bails out when the
  tail's start is already above the threshold.

Perf #9 — Flow-control dirty-set:
  QuicStream gains a receiveDirtyForFlowControl flag set by the parser
  when readContiguous advances the frontier. The writer's
  appendFlowControlUpdates now skips per-direction-window lookup +
  threshold comparison for streams whose flag is unset. Big win for
  multi-stream sessions (audio rooms with N×M streams per participant).

Perf #10 — Streams list view:
  QuicConnection maintains a parallel insertion-ordered List<QuicStream>
  alongside the streams Map. Writer's round-robin scan reads the list
  directly instead of allocating `entries.toList()` per drain.
  No removal path exists today; the insert points (openBidi/UniStream,
  getOrCreatePeerStreamLocked) update both.

AckTrackerGatingTest pins the new gating contract: first build returns
a frame; second build without new reception returns null; subsequent
ack-eliciting reception re-arms; non-ack-eliciting receptions alone
don't.

https://claude.ai/code/session_01EC1tfXfap8k8GyKvrxkxZx
This commit is contained in:
Claude
2026-04-26 00:49:20 +00:00
parent 21da61ad64
commit 920b36cdd6
6 changed files with 180 additions and 8 deletions
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quic.recovery
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
/**
* Round-4 perf #1 regression: [AckTracker.buildAckFrame] must return null when
* nothing new ack-eliciting has arrived since the last call. Pre-fix every
* outbound packet (~50/sec for an audio room) carried a redundant ACK frame.
*
* The contract:
* 1. First ack-eliciting reception → buildAckFrame returns a frame; flag clears.
* 2. Second buildAckFrame without new reception → returns null.
* 3. Subsequent ack-eliciting reception → next buildAckFrame returns frame.
* 4. Non-ack-eliciting receptions (PADDING-only, ACK-only) DO NOT trigger a
* new ACK on their own.
*/
class AckTrackerGatingTest {
@Test
fun first_build_after_ack_eliciting_returns_frame() {
val tracker = AckTracker()
tracker.receivedPacket(packetNumber = 5L, ackEliciting = true, receivedAtMillis = 1000L)
assertNotNull(tracker.buildAckFrame(nowMillis = 1010L))
}
@Test
fun second_build_without_new_reception_returns_null() {
val tracker = AckTracker()
tracker.receivedPacket(packetNumber = 5L, ackEliciting = true, receivedAtMillis = 1000L)
tracker.buildAckFrame(nowMillis = 1010L) // first ack — clears flag
assertNull(
tracker.buildAckFrame(nowMillis = 1020L),
"no new ack-eliciting reception → no redundant ACK",
)
}
@Test
fun build_re_arms_after_subsequent_ack_eliciting_reception() {
val tracker = AckTracker()
tracker.receivedPacket(packetNumber = 5L, ackEliciting = true, receivedAtMillis = 1000L)
tracker.buildAckFrame(nowMillis = 1010L)
// New ack-eliciting reception arrives.
tracker.receivedPacket(packetNumber = 6L, ackEliciting = true, receivedAtMillis = 1100L)
val ack = tracker.buildAckFrame(nowMillis = 1110L)
assertNotNull(ack, "fresh ack-eliciting reception must re-arm the gate")
assertEquals(6L, ack.largestAcknowledged)
}
@Test
fun non_ack_eliciting_reception_alone_does_not_arm_the_gate() {
// Per RFC 9000 §13.2.1 we don't have to ACK ACK-only packets. The
// tracker still records the PN (so future ACKs cover it), but the
// gate doesn't open until something ack-eliciting arrives.
val tracker = AckTracker()
tracker.receivedPacket(packetNumber = 1L, ackEliciting = false, receivedAtMillis = 1000L)
assertNull(
tracker.buildAckFrame(nowMillis = 1010L),
"non-ack-eliciting reception alone must not produce an ACK",
)
}
@Test
fun empty_tracker_returns_null() {
val tracker = AckTracker()
assertNull(tracker.buildAckFrame(nowMillis = 1000L))
}
}