device: avoid per-packet timestamp lookup in receive path
PATCH 1/1 device: avoid per-packet timestamp lookup in receive path
From: kurochan <[email protected]>
RoutineReceiveIncoming checks keypair expiry for every datagram in a receive batch. Capture the current time once after receiving the batch and reuse it for all expiry checks.
This removes one time.Now call per datagram while preserving the receive-path processing order. The expiry decision differs from the previous behavior by at most the time required to process the batch.
Benchmark: Linux arm64 Lima VM, Ubuntu 26.04, 4 vCPUs, TCP iperf3, 5-second runs over two userspace WireGuard interfaces.
With normal MSS, median throughput changed from 5.969 to 5.998 Gbps for one flow (+0.5%), and from 6.785 to 6.793 Gbps for four flows (+0.1%). In an alternating MSS 256 run, median throughput changed from 1.253 to 1.283 Gbps (+2.4%).
These are preliminary same-host VM results. They show no regression under normal MSS and a small improvement under higher packet-rate load.
--- device/receive.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
device/receive.go +2 -1
@@ -124,6 +124,7 @@ func (device *Device) RoutineReceiveIncoming(maxBatchSize int, recv conn.Receive return } deathSpiral = 0 + receivedAt := time.Now() // handle each packet in the batch for i, size := range sizes[:count] {
@@ -161,7 +162,7 @@ func (device *Device) RoutineReceiveIncoming(maxBatchSize int, recv conn.Receive // check keypair expiry - if keypair.created.Add(RejectAfterTime).Before(time.Now()) { + if keypair.created.Add(RejectAfterTime).Before(receivedAt) { continue }
base-commit: ecfc5a8d54462e18e13c72173e2623d16d8e25a0
Signature · 1 line
2.55.0