Repository navigation
perf(relay): bulk copy loops yield every millisecond: echo RTT under load 55-63 ms to 20 ms - #131
Merged
Merged
Conversation
A relay loop of a bulk transfer on a loaded server never blocks, so the Go scheduler lets it run for its whole 10 ms time slice, and the goroutine of an interactive connection that the poller made runnable waits behind every such loop: with 8 downloads on 2 cores, about 40 ms per hop through the server. The copy loops (copyWait, copyAdaptive, both halves of copyBatched) now call runtime.Gosched once they have moved 256 KiB and run for 1 ms since their last yield. Both limits keep the yield rare: a Gosched also wakes an idle processor, which costs a lone transfer on idle cores when it happens every few hundred KiB, and a connection that moves little never yields.
This was referenced Oct 18, 2026
Merged
escapezn
added a commit
to escapezn/mihomo
that referenced
this pull request
Nov 16, 2026
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.
What the latency-under-load number measures
cmp measure,latphase: a 64-byte echo every 20 ms on an established tunnel, while 8 unpaced download streams run through the same server (2 vCPUs, saturated: ~1.9 cores busy). The echo goes client → server → sink and back, so it crosses the server's relay twice. Everything is on one Docker bridge (no shaping, no loss), and the client and sink have spare CPU.Where the time goes
It's the Go scheduler, not socket buffers. A bulk relay loop never blocks: the sink always has data and the client takes it. So it runs for a whole 10 ms time slice before the runtime preempts it. When the echo packet arrives, its goroutine becomes runnable. While every P is busy, that happens only when sysmon polls the network, about every 10 ms. It then waits in the global run queue behind the bulk loops, at 10 ms per turn on 2 Ps: about 40 ms per hop, about 80 ms per round trip. Xray (also Go) shows the same ~77 ms.
Experiments (pre-#124 dev, Trojan and SS2022, 3 reps):
TCP_NOTSENT_LOWAT16 KiB / 128 KiB on the inbound listenerruntime.Gosched()after every writeGOMAXPROCS=16(idle Ps keep a thread in netpoll)With yields, what's left is exactly 2 × the runtime's 10 ms network-poll interval while no P is idle. That's a runtime constant.
The byte-only yield has a cost:
Goschedalso callswakep, so on a lone transfer with idle cores a yield every 256 KiB woke an idle P that took the goroutine over. On current dev, SS2022 dl1 dropped from 1986 to 1079 MB/s. So the yield now needs both limits: 256 KiB moved and 1 ms run since the last yield. Interactive connections never reach it.Change
common/net/relay_yield.go:relayYield.add(n)is called after each write incopyWait,copyAdaptiveand both halves ofcopyBatched. It callsruntime.Gosched()once both limits are reached. There are no socket options and no config: the yield has no throughput trade-off to tune, andTCP_NOTSENT_LOWATdidn't earn a default.Before / after (current dev, 4-core Docker bench, server 2 vCPUs, REPS=1)
Throughput is within noise. CPU per GB is +3 to +8% at n=1. A 256 KiB-only first cut (n=1) also showed REALITY at 44 → 20 ms p50 and 130 → 39 ms p99, AnyTLS at 37 → 6 / 138 → 17, and XHTTP's p99 at 44 → 20. For reference, sing-box's SS2022 sits at about 47 ms p50.
Tests
TestRelayYieldLimits: no yield before either limit; the count starts over after a yield.TestCopyConnYields: withGOMAXPROCS(1), a copy that never blocks runs next to a goroutine that sleeps 100 µs in a loop. Without the yield that goroutine ran about 25 times in 500 ms; with it, 360–400 times (the test needs more than 150).go test ./common/net/..., plus builds for linux/darwin/windows/linux-arm64.