]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add fast path for (*timers).adjust
authorIan Lance Taylor <iant@golang.org>
Thu, 21 Mar 2024 18:33:24 +0000 (11:33 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 22 Mar 2024 16:33:57 +0000 (16:33 +0000)
Affected benchmark results, including new benchmark (some of these may
just be noise, of course):

AdjustTimers10000-12           797.7µ ±  2%   709.6µ ±   2%  -11.04% (p=0.000 n=10)
TickerResetNaive-12            62.69n ±  1%   63.56n ±   1%   +1.40% (p=0.018 n=10)
NowUnixMicro-12                29.95n ±  1%   30.25n ±   4%   +1.00% (p=0.024 n=10)
ParseDuration-12               81.88n ±  0%   81.45n ±   0%   -0.51% (p=0.006 n=10)
UnmarshalText-12               186.9n ±  1%   185.2n ±   1%   -0.88% (p=0.006 n=10)
geomean                        151.8n         151.2n          -0.40%

Change-Id: I3ef8356249c5d703b314498e34ee8095093671c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/573455
Reviewed-by: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>

src/runtime/time.go
src/time/sleep_test.go

index 31c83ca4e361776ead116d01f71187dfb44554bf..06a56bf7aedc2fb65e99f8d853d7ccbc21286e26 100644 (file)
@@ -778,6 +778,11 @@ func (ts *timers) adjust(now int64, force bool) {
                        throw("bad ts")
                }
 
+               if t.astate.Load()&(timerModified|timerZombie) == 0 {
+                       // Does not need adjustment.
+                       continue
+               }
+
                t.lock()
                if t.state&timerHeaped == 0 {
                        badTimer()
index 634a5c7a130d4d96307e9b387a8df3567b1b2c4a..29f56ef7520baa266a7b75ffbad9de277cdec0a3 100644 (file)
@@ -971,3 +971,21 @@ func doWork(dur Duration) {
        for Since(start) < dur {
        }
 }
+
+func BenchmarkAdjustTimers10000(b *testing.B) {
+       benchmark(b, func(pb *testing.PB) {
+               for pb.Next() {
+                       const n = 10000
+                       timers := make([]*Timer, 0, n)
+                       for range n {
+                               t := AfterFunc(Hour, func() {})
+                               timers = append(timers, t)
+                       }
+                       timers[n-1].Reset(Nanosecond)
+                       Sleep(Microsecond)
+                       for _, t := range timers {
+                               t.Stop()
+                       }
+               }
+       })
+}