From 84b190ad3cfaa9e192f0980a58611db9f9286553 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 27 Mar 2024 22:44:52 -0400 Subject: [PATCH] runtime: avoid unnecessary netpoll wakeups during timer creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit goos: linux goarch: amd64 pkg: time cpu: AMD Ryzen 9 7950X 16-Core Processor │ s7base.txt │ s7.txt │ │ sec/op │ sec/op vs base │ AdjustTimers10000-32 315.1µ ± 4% 291.4µ ± 5% -7.52% (p=0.001 n=10) AdjustTimers10000SingleThread-32 1.728m ± 6% 1.728m ± 5% ~ (p=0.971 n=10) AdjustTimers10000NoReset-32 263.0µ ± 3% 253.1µ ± 6% -3.75% (p=0.023 n=10) AdjustTimers10000NoSleep-32 306.8µ ± 5% 277.3µ ± 3% -9.62% (p=0.000 n=10) AdjustTimers10000NoResetNoSleep-32 245.3µ ± 3% 241.4µ ± 2% ~ (p=0.529 n=10) goos: darwin goarch: arm64 pkg: time cpu: Apple M3 Pro │ m3base.txt │ m3.txt │ │ sec/op │ sec/op vs base │ AdjustTimers10000-12 385.5µ ± 3% 288.1µ ± 1% -25.26% (p=0.000 n=10) AdjustTimers10000SingleThread-12 1.213m ± 1% 1.195m ± 1% -1.45% (p=0.001 n=10) AdjustTimers10000NoReset-12 346.4µ ± 5% 280.8µ ± 1% -18.94% (p=0.000 n=10) AdjustTimers10000NoSleep-12 362.6µ ± 1% 292.5µ ± 1% -19.33% (p=0.000 n=10) AdjustTimers10000NoResetNoSleep-12 317.3µ ± 8% 279.8µ ± 2% -11.79% (p=0.000 n=10) Change-Id: Id3e10abfb1024f8e7883c8c366d941e4f1c2a894 Reviewed-on: https://go-review.googlesource.com/c/go/+/574738 Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Auto-Submit: Russ Cox --- src/runtime/time.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/runtime/time.go b/src/runtime/time.go index 7846542a80..96b7b76b07 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -607,15 +607,18 @@ func (t *timer) maybeAdd() { t.lock() t.trace("maybeAdd") when := int64(0) + wake := false if t.needsAdd() { t.state |= timerHeaped when = t.when + wakeTime := ts.wakeTime() + wake = wakeTime == 0 || when < wakeTime ts.addHeap(t) } t.unlock() ts.unlock() releasem(mp) - if when > 0 { + if wake { wakeNetPoller(when) } }