Skip to content

crypto/tls: idle connections hold less (raw input sized by what is moving, WaitRead) - #2

Merged
Nemu-x merged 4 commits into
devfrom
perf/lazy-raw-input
Oct 18, 2026
Merged

Nemu-x merged 4 commits into
devfrom
perf/lazy-raw-input

Conversation

@Nemu-x

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

Copy link
Copy Markdown
Collaborator

Idle-connection memory of a server: what a TLS connection holds while its peer is silent.

Changes

  • Raw input sized by what is moving (rawinput.go). rawInput grew with bytes.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. Once Read has 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.
  • Wait for the next record in a small frame. readRecordOrCCS blocked in the header read with its whole frame (about 500 bytes) on the stack; the record processing moves to readRecordBody. 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 until Read can 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 like Read.
  • 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.

…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
Nemu-x force-pushed the perf/lazy-raw-input branch from 2fa281d to 0a54a49 Compare October 18, 2026 05:46
@Nemu-x
Nemu-x merged commit 6cd535e into dev Oct 18, 2026
4 checks passed
@Nemu-x
Nemu-x deleted the perf/lazy-raw-input branch October 18, 2026 11:07
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