1
0
mirror of https://github.com/golang/go synced 2024-11-05 11:56:12 -07:00

runtime: do not allocate on every time.Sleep

It's common for some goroutines to loop calling time.Sleep.
Allocate once per goroutine, not every time.
This comes up in runtime/pprof's background reader.

Change-Id: I89d17dc7379dca266d2c9cd3aefc2382f5bdbade
Reviewed-on: https://go-review.googlesource.com/37162
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Russ Cox 2017-02-17 10:17:42 -05:00
parent a1ea91219f
commit a1261b8b0a
3 changed files with 8 additions and 1 deletions

View File

@ -2330,6 +2330,7 @@ func goexit0(gp *g) {
gp.waitreason = ""
gp.param = nil
gp.labels = nil
gp.timer = nil
// Note that gp's stack scan is now "valid" because it has no
// stack.

View File

@ -376,6 +376,7 @@ type g struct {
waiting *sudog // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
cgoCtxt []uintptr // cgo traceback context
labels unsafe.Pointer // profiler labels
timer *timer // cached timer for time.Sleep
// Per-G GC state

View File

@ -50,7 +50,12 @@ func timeSleep(ns int64) {
return
}
t := new(timer)
t := getg().timer
if t == nil {
t = new(timer)
getg().timer = t
}
*t = timer{}
t.when = nanotime() + ns
t.f = goroutineReady
t.arg = getg()