conn: skip timeout callbacks for background contexts#566
Merged
Conversation
kylecarbs
approved these changes
Jun 15, 2026
c326b67 to
9e4b7cd
Compare
Every frame read and write registered a context.AfterFunc callback so a context cancellation could close the connection. context.Background cannot be canceled because its Done channel is nil, but the code still constructed, stored, stopped, and cleared a callback registration for each operation. Skip that work when ctx.Done() is nil, and only clear a timeout when one was actually installed. BenchmarkConn also joins its writer goroutine after timing so repeated benchmark runs finish cleanly. The results below compare parent 7039364 with this commit on an Apple M4 Max using GOMAXPROCS=1 over 10 samples. A temporary in-package harness, not included in this commit, repeatedly called one internal frame read using either context.Background or an uncanceled context.WithCancel. Header reads parse a minimal frame header; payload reads copy 512 bytes from a buffered repeating reader. Before this change, setupReadTimeout called context.AfterFunc even for context.Background. Avoiding that dead registration reduces background header reads from 121.95 to 29.94 ns/op and payload reads from 124.17 to 29.15 ns/op. Both fall from 152 B/op and 4 allocs/op to zero. Cancelable contexts still need an AfterFunc registration, so they remain at 152 B/op and 4 allocs/op. BenchmarkConn/disabledCompress uses a cancelable context as well, so it remains at 1219 B/op and 32 allocs/op.
9e4b7cd to
97a20d0
Compare
Contributor
Author
|
Rebased. |
kylecarbs
approved these changes
Jun 15, 2026
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.
Skip per-operation timeout callback setup when the context cannot be canceled, such as
context.Background().Timeout setup now reports whether it installed a callback, so callers only clear callbacks that exist. The benchmark also joins its writer goroutine to make repeated runs reliable.
Previously, reads and writes called
context.AfterFuncfor every operation, even whenctx.Done()wasnil. This had a measurable cost.Similar to #565, I know this is a micro-optimization, but its basic and understandable and it produced measurable results in some of my actual workloads that were cheap!
Benchmarks
Each operation avoids 4 allocations and 152 bytes from the unnecessary
context.AfterFuncregistration.Cancelable contexts are unchanged because they still require a cancellation callback. Accordingly,
BenchmarkConn/disabledCompress, which uses a cancelable context, showed no statistically significant change.