Skip to content

Commit

Permalink
context: store first canceler child in separate field
Browse files Browse the repository at this point in the history
Benchmark results:

goos: windows
goarch: amd64
pkg: context
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz

name                                          old time/op  new time/op  delta
CommonParentCancel-12                          627ns ± 2%   586ns ± 2%   -6.58%  (p=0.008 n=5+5)
WithTimeout/concurrency=40-12                 1.08µs ±16%  1.10µs ± 4%     ~     (p=0.222 n=5+5)
WithTimeout/concurrency=4000-12                947ns ± 6%   993ns ± 4%     ~     (p=0.056 n=5+5)
WithTimeout/concurrency=400000-12              877ns ±15%   868ns ± 1%     ~     (p=0.151 n=5+5)
CancelTree/depth=1/Root=Background-12         61.9ns ± 0%  68.9ns ± 0%  +11.37%  (p=0.008 n=5+5)
CancelTree/depth=1/Root=OpenCanceler-12        439ns ± 1%   297ns ± 0%  -32.30%  (p=0.008 n=5+5)
CancelTree/depth=1/Root=ClosedCanceler-12      219ns ± 0%   229ns ± 0%   +4.74%  (p=0.008 n=5+5)
CancelTree/depth=10/Root=Background-12        2.45µs ± 2%  1.45µs ± 0%  -40.91%  (p=0.008 n=5+5)
CancelTree/depth=10/Root=OpenCanceler-12      3.43µs ± 2%  1.98µs ± 0%  -42.34%  (p=0.008 n=5+5)
CancelTree/depth=10/Root=ClosedCanceler-12    1.23µs ± 0%  1.32µs ± 0%   +6.86%  (p=0.008 n=5+5)
CancelTree/depth=100/Root=Background-12       26.0µs ± 1%  15.1µs ± 1%  -41.91%  (p=0.008 n=5+5)
CancelTree/depth=100/Root=OpenCanceler-12     33.0µs ± 1%  18.7µs ± 0%  -43.25%  (p=0.016 n=5+4)
CancelTree/depth=100/Root=ClosedCanceler-12   11.3µs ± 1%  12.1µs ± 1%   +6.32%  (p=0.008 n=5+5)
CancelTree/depth=1000/Root=Background-12       262µs ± 1%   152µs ± 0%  -41.93%  (p=0.008 n=5+5)
CancelTree/depth=1000/Root=OpenCanceler-12     331µs ± 0%   188µs ± 0%  -43.11%  (p=0.008 n=5+5)
CancelTree/depth=1000/Root=ClosedCanceler-12   112µs ± 1%   119µs ± 0%   +6.12%  (p=0.008 n=5+5)
CheckCanceled/Err-12                          11.7ns ± 1%  11.7ns ± 1%     ~     (p=0.738 n=5+5)
CheckCanceled/Done-12                         5.61ns ± 0%  5.60ns ± 1%     ~     (p=0.905 n=4+5)
ContextCancelDone-12                          0.91ns ± 6%  0.91ns ± 6%     ~     (p=0.841 n=5+5)
  • Loading branch information
tdakkota committed Aug 18, 2021
1 parent 717894c commit 1c4f3b2
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/context/context.go
Expand Up @@ -267,10 +267,15 @@ func propagateCancel(parent Context, child canceler) {
// parent has already been canceled
child.cancel(false, p.err)
} else {
if p.children == nil {
p.children = make(map[canceler]struct{})
if p.firstChild == nil {
p.firstChild = child
} else {
if p.children == nil {
p.children = make(map[canceler]struct{})
p.children[p.firstChild] = struct{}{}
}
p.children[child] = struct{}{}
}
p.children[child] = struct{}{}
}
p.mu.Unlock()
} else {
Expand Down Expand Up @@ -317,6 +322,9 @@ func removeChild(parent Context, child canceler) {
return
}
p.mu.Lock()
if p.firstChild == child {
p.firstChild = nil
}
if p.children != nil {
delete(p.children, child)
}
Expand All @@ -342,10 +350,11 @@ func init() {
type cancelCtx struct {
Context

mu sync.Mutex // protects following fields
done atomic.Value // of chan struct{}, created lazily, closed by first cancel call
children map[canceler]struct{} // set to nil by the first cancel call
err error // set to non-nil by the first cancel call
mu sync.Mutex // protects following fields
done atomic.Value // of chan struct{}, created lazily, closed by first cancel call
firstChild canceler
children map[canceler]struct{} // set to nil by the first cancel call
err error // set to non-nil by the first cancel call
}

func (c *cancelCtx) Value(key interface{}) interface{} {
Expand Down Expand Up @@ -410,6 +419,10 @@ func (c *cancelCtx) cancel(removeFromParent bool, err error) {
} else {
close(d)
}
if c.firstChild != nil {
c.firstChild.cancel(false, err)
}
c.firstChild = nil
for child := range c.children {
// NOTE: acquiring the child's lock while holding parent's lock.
child.cancel(false, err)
Expand Down

0 comments on commit 1c4f3b2

Please sign in to comment.