Skip to content

improve(conn): reuse the timeout callbacks across frames - #605

Open
g4lb wants to merge 1 commit into
coder:masterfrom
g4lb:perf/timeout-callbacks
Open

g4lb wants to merge 1 commit into
coder:masterfrom
g4lb:perf/timeout-callbacks

Conversation

@g4lb

@g4lb g4lb commented Nov 21, 2026

Copy link
Copy Markdown

setupReadTimeout and setupWriteTimeout made a new context.AfterFunc callback for each frame. Now the code makes each callback once, on first use. readMu and writeFrameMu guard them. The behavior does not change.

Benchmark Before After Allocs
BenchmarkConn/disabledCompress 9.25 µs 9.07 µs 32 → 24
BenchmarkConn/compressContextTakeover 18.55 µs 18.36 µs 44 → 33
16 B message, context with timeout 5.01 µs 4.89 µs 20 → 15
10-frame message, context with timeout 22.45 µs 21.91 µs 128 → 96

I measured no slowdown on AMD EPYC 7763 (Go 1.27.4, 24 interleaved rounds, 3 code layouts). Each Conn is 32 bytes larger.

I did not make the callbacks in newConn, because that made newConn 6% to 14% slower.

Checks: go test -race, go vet and staticcheck pass, also for WASM.

setupReadTimeout and setupWriteTimeout made a new context.AfterFunc callback for each frame, so each frame read or write with a cancelable context allocated it. The callbacks do not depend on the frame, so each one is made on first use and reused under readMu or writeFrameMu.

This branch has not been deployed

No deployments
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