Repository navigation
crypto/tls: idle connections hold less (raw input sized by what is moving, WaitRead) - #2
Merged
Merged
Conversation
…hen drained rawInput grew with bytes.Buffer.Grow, which doubles it, and was kept for the life of the connection: a connection that once received a full record holds 16-40 KiB, and 2-4 KiB after the handshake alone (a large ClientHello), while it waits for its peer. A proxy that keeps thousands of mostly idle connections pays that for each of them. A read now takes a small buffer of its own (1 KiB) while the records are small and a pooled buffer that holds the largest record when one does not fit. Once Read has handed out everything that was read and the last record was a short one, the pooled buffer goes back to the pool and the small one takes over again. After a full record the pooled buffer stays, so a transfer keeps one read per record. The handshake message buffer is dropped once the handshake is complete.
readRecordOrCCS blocks in the read of the record header with its whole frame (close to 500 bytes) on the stack. An idle connection's goroutine spends its life there, and the runtime shrinks a goroutine stack at GC only when less than a quarter of it is in use: every frame above that read counts towards keeping an idle connection at a 16 KiB stack instead of 8 KiB. The record processing after the header moves to readRecordBody.
A proxy's relay keeps a read buffer for each connection while it waits in Read. WaitRead blocks until Read can return data without blocking (or until it would fail, with the same error), so that the caller takes its buffer only once data is there and a connection whose peer is silent holds none.
Nemu-x
force-pushed
the
perf/lazy-raw-input
branch
from
October 18, 2026 05:46
2fa281d to
0a54a49
Compare
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.
Idle-connection memory of a server: what a TLS connection holds while its peer is silent.
Changes
rawinput.go).rawInputgrew withbytes.Buffer.Grow, which doubles it, and was kept for the life of the connection: 2-4 KiB after the handshake alone (a large ClientHello), 16-40 KiB after a full record. A read now takes a small buffer of its own (1 KiB) while records are small and a pooled buffer that holds the largest record when one does not fit. OnceReadhas handed out everything read and the last record was short, the pooled buffer goes back to the pool and the small one takes over. After a full record the pooled buffer stays, so a transfer keeps one read per record (no extra syscalls). The handshake message buffer is dropped once the handshake is complete.readRecordOrCCSblocked in the header read with its whole frame (about 500 bytes) on the stack; the record processing moves toreadRecordBody. The runtime shrinks an idle goroutine's stack at GC only when less than a quarter of it is in use, so every frame above that read counts.Conn.WaitRead: blocks untilReadcan return data without blocking (or until it would fail, with the same error), so a relay can take its read buffer only once data is there.Tests
TestRawInputIdle: after the handshake, after a transfer of mixed record sizes (read with buffers of every size, data checked), after a short message: no pooled buffer held, at most 1 KiB of raw input, no handshake buffer.TestRawInputCloseNotify: the close_notify read ahead with the last data still ends the stream with EOF and leaves nothing held.TestWaitRead: returns once data is there, consumes nothing, reports EOF likeRead.BenchmarkRawInputThroughput(1 MiB writes, loopback, 3x3000 iterations each): before 612-830 MB/s, 1.3-1.9 KB and 49-62 allocs per MiB; after 633-702 MB/s, 1.6-1.9 KB and 64 allocs per MiB (the same within noise).Used by getmikan/Citrus (idle-tunnel memory PR), which pins this branch until a release.