Skip to content

Commit 180662f

Browse files
eliasnaurdeadprogram
authored andcommitted
runtime: avoid an allocation in (*time.Timer).Reset
1 parent 2da9d26 commit 180662f

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

src/runtime/scheduler.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,17 @@ func timerQueueAdd(tn *timerNode) {
3939
*q = tn
4040
}
4141

42-
func timerQueueRemove(t *timer) bool {
43-
removedTimer := false
42+
func timerQueueRemove(t *timer) *timerNode {
4443
for q := &timerQueue; *q != nil; q = &(*q).next {
4544
if (*q).timer == t {
4645
scheduleLog("removed timer")
46+
n := *q
4747
*q = (*q).next
48-
removedTimer = true
49-
break
48+
return n
5049
}
5150
}
52-
if !removedTimer {
53-
scheduleLog("did not remove timer")
54-
}
55-
return removedTimer
51+
scheduleLog("did not remove timer")
52+
return nil
5653
}
5754

5855
// Goexit terminates the currently running goroutine. No other goroutines are affected.

src/runtime/scheduler_cooperative.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ func addTimer(tim *timerNode) {
117117

118118
// removeTimer is the implementation of time.stopTimer. It removes a timer from
119119
// the timer queue, returning true if the timer is present in the timer queue.
120-
func removeTimer(tim *timer) bool {
120+
func removeTimer(tim *timer) *timerNode {
121121
mask := interrupt.Disable()
122-
removedTimer := timerQueueRemove(tim)
122+
n := timerQueueRemove(tim)
123123
interrupt.Restore(mask)
124-
return removedTimer
124+
return n
125125
}
126126

127127
func schedulerRunQueue() *task.Queue {

src/runtime/scheduler_none.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func addTimer(tim *timerNode) {
5656
runtimePanic("timers not supported without a scheduler")
5757
}
5858

59-
func removeTimer(tim *timer) bool {
59+
func removeTimer(tim *timer) *timerNode {
6060
runtimePanic("timers not supported without a scheduler")
61-
return false
61+
return nil
6262
}
6363

6464
func schedulerRunQueue() *task.Queue {

src/runtime/scheduler_threads.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ func addTimer(tim *timerNode) {
110110
timerQueueLock.Unlock()
111111
}
112112

113-
func removeTimer(tim *timer) bool {
113+
func removeTimer(tim *timer) *timerNode {
114114
timerQueueLock.Lock()
115-
removed := timerQueueRemove(tim)
115+
n := timerQueueRemove(tim)
116116
timerQueueLock.Unlock()
117-
return removed
117+
return n
118118
}
119119

120120
func schedulerRunQueue() *task.Queue {

src/runtime/time_go122.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func startTimer(tim *timer) {
5353

5454
//go:linkname stopTimer time.stopTimer
5555
func stopTimer(tim *timer) bool {
56-
return removeTimer(tim)
56+
return removeTimer(tim) != nil
5757
}
5858

5959
//go:linkname resetTimer time.resetTimer
6060
func resetTimer(tim *timer, when int64) bool {
6161
tim.when = when
62-
removed := removeTimer(tim)
62+
n := removeTimer(tim)
6363
startTimer(tim)
64-
return removed
64+
return n != nil
6565
}

src/runtime/time_go123.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg
5252

5353
//go:linkname stopTimer time.stopTimer
5454
func stopTimer(tim *timeTimer) bool {
55-
return removeTimer(&tim.timer)
55+
return removeTimer(&tim.timer) != nil
5656
}
5757

5858
//go:linkname resetTimer time.resetTimer
5959
func resetTimer(t *timeTimer, when, period int64) bool {
6060
t.timer.when = when
6161
t.timer.period = period
62-
removed := removeTimer(&t.timer)
63-
addTimer(&timerNode{
64-
timer: &t.timer,
65-
callback: timerCallback,
66-
})
62+
n := removeTimer(&t.timer)
63+
removed := n != nil
64+
if n == nil {
65+
n = new(timerNode)
66+
}
67+
n.timer = &t.timer
68+
n.callback = timerCallback
69+
addTimer(n)
6770
return removed
6871
}

0 commit comments

Comments
 (0)