Commit 9e4b7cd
committed
conn: avoid frame timeout and cleanup overhead
#565
Every frame read registered a context.AfterFunc callback, even when the
context could not be canceled, and returned a cleanup closure that
forced the caller's error and captured state onto the heap. Skip
timeout setup for contexts with a nil Done channel and move read
cleanup into a direct method call, while tracking whether a callback
was installed so cancelable operations retain the same
close-on-cancellation behavior. Writes use the same background-context
fast path, and BenchmarkConn now joins its writer goroutine so repeated
runs finish cleanly.
On an Apple M4 Max with GOMAXPROCS=1, a 10-sample in-package
benchmark against d099e16 reduced background header reads from
168.36 to 22.85 ns/op and 512-byte payload reads from 175.04 to
26.65 ns/op. Both fell from 216 B/op and 6 allocs/op to zero.
Cancelable header reads improved from 203.40 to 160.50 ns/op and
payload reads from 215.18 to 162.52 ns/op, with both falling to
152 B/op and 4 allocs/op. Comparing against the closure-only change
confirms that skipping the dead timeout registration accounts for
exactly 152 bytes and 4 allocations on background reads without
changing cancelable allocations.
BenchmarkConn/disabledCompress averaged 3.968 us/op, 1539 B/op, and 42
allocs/op before the change and 3.817 us/op, 1219 B/op, and 32 allocs/op
after it. This is a 3.8 percent time reduction, 20.8 percent fewer
bytes, and 23.8 percent fewer allocations per operation.1 parent d099e16 commit 9e4b7cd
4 files changed
Lines changed: 55 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
172 | 176 | | |
173 | 177 | | |
174 | 178 | | |
175 | 179 | | |
176 | 180 | | |
| 181 | + | |
177 | 182 | | |
178 | 183 | | |
179 | 184 | | |
180 | 185 | | |
181 | 186 | | |
182 | 187 | | |
183 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
184 | 193 | | |
185 | 194 | | |
186 | 195 | | |
187 | 196 | | |
188 | 197 | | |
| 198 | + | |
189 | 199 | | |
190 | 200 | | |
191 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
592 | 592 | | |
593 | 593 | | |
594 | 594 | | |
595 | | - | |
596 | 595 | | |
| 596 | + | |
597 | 597 | | |
598 | 598 | | |
| 599 | + | |
599 | 600 | | |
600 | 601 | | |
601 | 602 | | |
| |||
650 | 651 | | |
651 | 652 | | |
652 | 653 | | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
653 | 661 | | |
654 | 662 | | |
655 | 663 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
| 220 | + | |
| 221 | + | |
227 | 222 | | |
228 | 223 | | |
229 | | - | |
| 224 | + | |
230 | 225 | | |
231 | 226 | | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
| 227 | + | |
247 | 228 | | |
248 | 229 | | |
249 | 230 | | |
250 | 231 | | |
251 | 232 | | |
252 | | - | |
253 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
254 | 237 | | |
255 | 238 | | |
256 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
257 | 258 | | |
258 | 259 | | |
259 | 260 | | |
260 | | - | |
| 261 | + | |
261 | 262 | | |
262 | 263 | | |
263 | 264 | | |
264 | | - | |
| 265 | + | |
265 | 266 | | |
266 | 267 | | |
267 | 268 | | |
| |||
272 | 273 | | |
273 | 274 | | |
274 | 275 | | |
275 | | - | |
| 276 | + | |
276 | 277 | | |
277 | 278 | | |
278 | 279 | | |
279 | | - | |
| 280 | + | |
280 | 281 | | |
281 | 282 | | |
282 | 283 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
321 | | - | |
322 | | - | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| |||
0 commit comments