buffered: Use a raw function to avoid allocating structs

This commit is contained in:
Hajime Hoshi 2019-09-27 00:37:31 +09:00
parent fc3e8bebe4
commit 3291ec51bb

View File

@ -18,10 +18,6 @@ import (
"sync" "sync"
) )
type command struct {
f func()
}
var ( var (
delayedCommandsFlushable bool delayedCommandsFlushable bool
@ -30,7 +26,7 @@ var (
// sizes (#879). // sizes (#879).
// //
// TODO: Flush the commands only when necessary (#921). // TODO: Flush the commands only when necessary (#921).
delayedCommands []*command delayedCommands []func()
delayedCommandsM sync.Mutex delayedCommandsM sync.Mutex
) )
@ -42,9 +38,7 @@ func makeDelayedCommandFlushable() {
func enqueueDelayedCommand(f func()) { func enqueueDelayedCommand(f func()) {
delayedCommandsM.Lock() delayedCommandsM.Lock()
delayedCommands = append(delayedCommands, &command{ delayedCommands = append(delayedCommands, f)
f: f,
})
delayedCommandsM.Unlock() delayedCommandsM.Unlock()
} }
@ -57,7 +51,7 @@ func flushDelayedCommands() bool {
} }
for _, c := range delayedCommands { for _, c := range delayedCommands {
c.f() c()
} }
delayedCommands = delayedCommands[:0] delayedCommands = delayedCommands[:0]
return true return true