Reduce per-connection memory overhead#414
Merged
9seconds merged 2 commits into9seconds:masterfrom Mar 29, 2026
Merged
Conversation
- Use sync.Pool for relay buffers instead of stack-allocated arrays. A [16379]byte on the goroutine stack forces Go to grow it to 32KB (next power of two). Pooled buffers keep goroutine stacks small. - Same fix for doppelganger write buffer ([16384]byte in conn.start). - Replace idle goroutines with context.AfterFunc in proxy.ServeConn and relay.Relay. These goroutines existed only to wait on ctx.Done() and close connections. AfterFunc achieves the same without allocating a goroutine until the context is actually cancelled. Net effect: at 3000 concurrent connections on a 1-vCPU/961MB VPS, the unmodified binary drops 246 connections and falls to 10 MB/s. With these changes: zero failures, 63 MB/s, 31% lower RSS. Closes 9seconds#412
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.
Summary
var buf [16379]byteon the goroutine stack forces Go runtime to grow it to 32 KB (next power of two). Pooled buffers keep stacks small (~2-4 KB).[16384]byteinconn.start().proxy.ServeConnandrelay.Relaythat existed only to<-ctx.Done()and close connections.Benchmark (VPS, 1 vCPU, 961 MB RAM, real mtg binary, domain fronting to echo)
At moderate concurrency (~1000) the difference is within noise. At 3000 connections the unmodified binary breaks down: 246 connection timeouts, throughput drops to 10 MB/s. With this change: zero failures, 63 MB/s, 31% lower RSS.
Test plan
go test ./...passesCloses #412