runtime: avoid an allocation in (*time.Timer).Reset

This commit is contained in:
Elias Naur
2025-05-09 15:52:48 +02:00
committed by Ron Evans
parent 2da9d26e21
commit 180662f038
6 changed files with 25 additions and 25 deletions
+5 -8
View File
@@ -39,20 +39,17 @@ func timerQueueAdd(tn *timerNode) {
*q = tn *q = tn
} }
func timerQueueRemove(t *timer) bool { func timerQueueRemove(t *timer) *timerNode {
removedTimer := false
for q := &timerQueue; *q != nil; q = &(*q).next { for q := &timerQueue; *q != nil; q = &(*q).next {
if (*q).timer == t { if (*q).timer == t {
scheduleLog("removed timer") scheduleLog("removed timer")
n := *q
*q = (*q).next *q = (*q).next
removedTimer = true return n
break
} }
} }
if !removedTimer { scheduleLog("did not remove timer")
scheduleLog("did not remove timer") return nil
}
return removedTimer
} }
// Goexit terminates the currently running goroutine. No other goroutines are affected. // Goexit terminates the currently running goroutine. No other goroutines are affected.
+3 -3
View File
@@ -117,11 +117,11 @@ func addTimer(tim *timerNode) {
// removeTimer is the implementation of time.stopTimer. It removes a timer from // removeTimer is the implementation of time.stopTimer. It removes a timer from
// the timer queue, returning true if the timer is present in the timer queue. // the timer queue, returning true if the timer is present in the timer queue.
func removeTimer(tim *timer) bool { func removeTimer(tim *timer) *timerNode {
mask := interrupt.Disable() mask := interrupt.Disable()
removedTimer := timerQueueRemove(tim) n := timerQueueRemove(tim)
interrupt.Restore(mask) interrupt.Restore(mask)
return removedTimer return n
} }
func schedulerRunQueue() *task.Queue { func schedulerRunQueue() *task.Queue {
+2 -2
View File
@@ -56,9 +56,9 @@ func addTimer(tim *timerNode) {
runtimePanic("timers not supported without a scheduler") runtimePanic("timers not supported without a scheduler")
} }
func removeTimer(tim *timer) bool { func removeTimer(tim *timer) *timerNode {
runtimePanic("timers not supported without a scheduler") runtimePanic("timers not supported without a scheduler")
return false return nil
} }
func schedulerRunQueue() *task.Queue { func schedulerRunQueue() *task.Queue {
+3 -3
View File
@@ -110,11 +110,11 @@ func addTimer(tim *timerNode) {
timerQueueLock.Unlock() timerQueueLock.Unlock()
} }
func removeTimer(tim *timer) bool { func removeTimer(tim *timer) *timerNode {
timerQueueLock.Lock() timerQueueLock.Lock()
removed := timerQueueRemove(tim) n := timerQueueRemove(tim)
timerQueueLock.Unlock() timerQueueLock.Unlock()
return removed return n
} }
func schedulerRunQueue() *task.Queue { func schedulerRunQueue() *task.Queue {
+3 -3
View File
@@ -53,13 +53,13 @@ func startTimer(tim *timer) {
//go:linkname stopTimer time.stopTimer //go:linkname stopTimer time.stopTimer
func stopTimer(tim *timer) bool { func stopTimer(tim *timer) bool {
return removeTimer(tim) return removeTimer(tim) != nil
} }
//go:linkname resetTimer time.resetTimer //go:linkname resetTimer time.resetTimer
func resetTimer(tim *timer, when int64) bool { func resetTimer(tim *timer, when int64) bool {
tim.when = when tim.when = when
removed := removeTimer(tim) n := removeTimer(tim)
startTimer(tim) startTimer(tim)
return removed return n != nil
} }
+9 -6
View File
@@ -52,17 +52,20 @@ func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg
//go:linkname stopTimer time.stopTimer //go:linkname stopTimer time.stopTimer
func stopTimer(tim *timeTimer) bool { func stopTimer(tim *timeTimer) bool {
return removeTimer(&tim.timer) return removeTimer(&tim.timer) != nil
} }
//go:linkname resetTimer time.resetTimer //go:linkname resetTimer time.resetTimer
func resetTimer(t *timeTimer, when, period int64) bool { func resetTimer(t *timeTimer, when, period int64) bool {
t.timer.when = when t.timer.when = when
t.timer.period = period t.timer.period = period
removed := removeTimer(&t.timer) n := removeTimer(&t.timer)
addTimer(&timerNode{ removed := n != nil
timer: &t.timer, if n == nil {
callback: timerCallback, n = new(timerNode)
}) }
n.timer = &t.timer
n.callback = timerCallback
addTimer(n)
return removed return removed
} }