Repository navigation
perf(anytls): queued receive, download frames in place - #130
Merged
Merged
Conversation
This was referenced Oct 18, 2026
Merged
Server side of AnyTLS, measured against sing-box 1.14.3 (same client): - Received data frames are queued on their stream instead of going through a synchronous pipe. recvLoop used to wait for the reader of every frame (a goroutine switch each way, also while the stream's handler was still reading its destination) and the reader copied the frame out of a pool buffer that recvLoop then reused. Now the frame's buffer is handed over: recvLoop goes on decrypting while the readers write, and a session holds at most 512 KiB for its readers before it stops reading (backpressure as before, bounded). A reader that waits is woken, and recvLoop yields the processor to it; a busy reader is not woken. The stream is a read waiter: the relay takes the frame's own buffer, and all queued frames at once (WaitReadBuffers) in one writev to the destination, with the relay's yield. A peer's FIN no longer drops frames that arrived before it. - Stream.WriteBuffer with FrontHeadroom: a download buffer becomes a frame in place, without the copy into a new buffer.
Merged
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.
Server-side AnyTLS: received frames are queued instead of handed over through a synchronous pipe, and download frames are built in place. Wire format unchanged. Rebased on dev after #129, #131, #135 and #136.
Root causes
recvLoophanded every data frame to the stream's reader through an unbuffered pipe and waited until the reader had taken it. That cost a goroutine switch each way per frame and a copy out of recvLoop's pool buffer, and the whole session stalled while a stream's handler was still reading its destination. Uploads then went through a second read-ahead goroutine in the relay. The shared pool buffer was also behind the data race on the client side (pipe.readagainsttls.(*Conn).Readwriting into the same buffer).Changes
transport/anytls/session/recv.go: data frames are queued on their stream, and the frame's buffer is handed over to the reader.runtime.Gosched). A busy reader is not woken. Without the yield, the reader was taken over by another thread that had to be woken for every frame, and a single upload lost about 7%.N.ReadWaiter: the relay takes the frame's own buffer without a copy.WaitReadBuffershands over every queued frame at once, and the newcopyWaitBatchedincommon/net/relay_copy.gowrites them in one writev. It has the relay yield of perf(relay): bulk copy loops yield every millisecond: echo RTT under load 55-63 ms to 20 ms #131 like the other loops, and is used only for read waiters that implement it.pipe/io_pipe.gois removed.Stream.WriteBuffer+FrontHeadroom: a download buffer becomes a frame in place.Numbers
test/bench/comparefrom dev (pid-file kill), two server vCPUs, sing-box client. Medians of 3 runs; sing-box and the branch come from the same run, dev from a run 20 minutes earlier on the same idle host. The harness recorded no anomalies.What is better, what is not:
Races
go test -racein golang:1.26.9 (Linux, CGO_ENABLED=1):transport/anytls/...,common/net,listener/anytlswith-count 10: pass.-run TestInboundAnyTLS_TLS$ ./listener/inbound/with-count 20still fails on both dev and this branch, so the PR is not race-clean there, but none of the failing reports comes from this PR's code paths:pipe.(*pipe).readagainsttls.(*Conn).Read) shows up on dev (1 report in 10 runs, 1 in 20) and not on this branch (0 in 30 runs; the pipe is gone).tls.Client,context.withCancel, http2 framing. Their count is similar on both (16 against 18 in 20 runs). They look like an artifact of memory reuse inside the race runtime, not of this change.-run AnyTLSset, the restls-client-go reports (Restls, JLS and ShadowTLS tests) are the same on dev.