1
0
mirror of https://github.com/golang/go synced 2024-09-29 13:14:28 -06:00

context: remove one allocation in timerCtx

Embed the cancelCtx into timerCtx, instead of allocating both separately.

name                               old time/op    new time/op    delta
WithTimeout/concurrency=40-16        2.21µs ±11%    2.08µs ± 5%   -5.76%  (p=0.011 n=10+10)
WithTimeout/concurrency=4000-16      1.94µs ± 6%    1.86µs ±10%     ~     (p=0.099 n=9+10)
WithTimeout/concurrency=400000-16    1.86µs ± 7%    1.83µs ±10%     ~     (p=0.353 n=10+10)

name                               old alloc/op   new alloc/op   delta
WithTimeout/concurrency=40-16        2.56kB ± 0%    2.40kB ± 0%   -6.25%  (p=0.001 n=8+9)
WithTimeout/concurrency=4000-16      2.56kB ± 0%    2.40kB ± 0%   -6.25%  (p=0.000 n=9+10)
WithTimeout/concurrency=400000-16    2.56kB ± 0%    2.40kB ± 0%   -6.26%  (p=0.000 n=9+9)

name                               old allocs/op  new allocs/op  delta
WithTimeout/concurrency=40-16          50.0 ± 0%      40.0 ± 0%  -20.00%  (p=0.000 n=10+10)
WithTimeout/concurrency=4000-16        50.0 ± 0%      40.0 ± 0%  -20.00%  (p=0.000 n=10+10)
WithTimeout/concurrency=400000-16      50.0 ± 0%      40.0 ± 0%  -20.00%  (p=0.000 n=10+10)

Change-Id: Ia0460db3b8412fbaa6f1539fd6b4fb1b873896c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/463999
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Carlo Alberto Ferraris 2023-02-02 11:07:34 +09:00 committed by Gopher Robot
parent c3b4c27fd3
commit 86c4b0a6ec

View File

@ -272,7 +272,7 @@ func withCancel(parent Context) *cancelCtx {
if parent == nil {
panic("cannot create context from nil parent")
}
c := newCancelCtx(parent)
c := &cancelCtx{Context: parent}
propagateCancel(parent, c)
return c
}
@ -292,11 +292,6 @@ func Cause(c Context) error {
return nil
}
// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) *cancelCtx {
return &cancelCtx{Context: parent}
}
// goroutines counts the number of goroutines ever created; for testing.
var goroutines atomic.Int32
@ -507,7 +502,7 @@ func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, Cance
return WithCancel(parent)
}
c := &timerCtx{
cancelCtx: newCancelCtx(parent),
cancelCtx: cancelCtx{Context: parent},
deadline: d,
}
propagateCancel(parent, c)
@ -530,7 +525,7 @@ func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, Cance
// implement Done and Err. It implements cancel by stopping its timer then
// delegating to cancelCtx.cancel.
type timerCtx struct {
*cancelCtx
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
@ -655,7 +650,7 @@ func value(c Context, key any) any {
c = ctx.Context
case *timerCtx:
if key == &cancelCtxKey {
return ctx.cancelCtx
return &ctx.cancelCtx
}
c = ctx.Context
case *emptyCtx: