Skip to content

perf(anytls): queued receive, download frames in place - #130

Merged
Nemu-x merged 1 commit into
devfrom
perf/anytls
Oct 18, 2026
Merged

Nemu-x merged 1 commit into
devfrom
perf/anytls

Conversation

@Nemu-x

@Nemu-x Nemu-x commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • Synchronous pipe per stream. recvLoop handed 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.read against tls.(*Conn).Read writing into the same buffer).
  • A copy into a new frame buffer on every download write.

Changes

  • transport/anytls/session/recv.go: data frames are queued on their stream, and the frame's buffer is handed over to the reader.
    • recvLoop keeps decrypting while the readers write. A session holds at most 512 KiB for its readers, then stops reading, so a slow reader still pushes back as before, with bounded memory.
    • A reader that waits is woken, and recvLoop yields the processor to it (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%.
    • The stream is an N.ReadWaiter: the relay takes the frame's own buffer without a copy. WaitReadBuffers hands over every queued frame at once, and the new copyWaitBatched in common/net/relay_copy.go writes 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.
    • Frames that arrive before a peer's FIN are still read. The unused pipe/io_pipe.go is removed.
  • Stream.WriteBuffer + FrontHeadroom: a download buffer becomes a frame in place.
  • Dropped since the first version of this PR:

Numbers

test/bench/compare from 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.

metric sing-box 1.14.3 dev (2f77941) this PR
dl 1 stream, MB/s 1222 1286 1235
ul 1 stream, MB/s 1540 1938 1748
dl 32 streams, MB/s 2580 1867 3909
ul 32 streams, MB/s 3280 2914 3576
CPU s/GB, dl 32 0.73 0.93 0.51
CPU s/GB, ul 32 0.57 0.68 0.56
new conns/s (128 clients) 15841 15076 (14444–15423) 14711 (13948–14768)
server CPU ms per new conn 0.123 0.130 0.132
TTFB p99, ms 18.3 20.1 20.7
idle RSS per tunnel @5000, KiB 69.5 55.8 54.7
echo RTT under load p99, ms 164.6 27.9 24.0
bulk alongside the latency probe, MB/s 2595 1804 3976

What is better, what is not:

  • Multi-stream bulk roughly doubles download and adds 23% upload, at much lower CPU per GB, ahead of sing-box on every bulk row. Latency under load is no worse while carrying twice the bulk.
  • Idle memory: not worse than dev (54.7 against 55.8 KiB; spread 50.2–55.6 against 55.0–55.9).
  • New connections / TTFB: within dev's spread (−2.4% at the median), still behind sing-box.
  • ul 1 stream is 10% below dev (1748 against 1938), though still above sing-box (1540). dev gained about 14% here from perf: idle tunnels hold less (stacks, TLS raw input, relay buffer) #129/perf(relay): bulk copy loops yield every millisecond: echo RTT under load 55-63 ms to 20 ms #131 since the first measurement of this PR. The yield after a wake-up brought this branch from about 1580 to its current level. A short spin before parking and waking the reader on every frame were tried and were worse. I found no change that closes the rest without giving up the 32-stream gains.
  • dl 1 stream: −4% against dev, about equal to sing-box.

Races

go test -race in golang:1.26.9 (Linux, CGO_ENABLED=1):

  • transport/anytls/..., common/net, listener/anytls with -count 10: pass.
  • -run TestInboundAnyTLS_TLS$ ./listener/inbound/ with -count 20 still 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:
    • The AnyTLS client pipe race (pipe.(*pipe).read against tls.(*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).
    • Every report on this branch, and on dev, names the same goroutine for both accesses: TLS handshake buffers, 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.
  • In the full -run AnyTLS set, the restls-client-go reports (Restls, JLS and ShadowTLS tests) are the same on dev.

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.
@Nemu-x Nemu-x changed the title perf(anytls): queued receive, in-place frames, reused stream goroutines, deferred SYNACK perf(anytls): queued receive, download frames in place Oct 18, 2026
@Nemu-x
Nemu-x merged commit daa2522 into dev Oct 18, 2026
12 checks passed
@Nemu-x
Nemu-x deleted the perf/anytls branch October 18, 2026 18:11
Nemu-x added a commit that referenced this pull request Oct 20, 2026
docs(site): changelog for #130-#138, roadmap after the post-beta.2 work
@Nemu-x Nemu-x mentioned this pull request Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant