Skip to content

Commit 29d6727

Browse files
committed
all: optimize for zero allocation scheduling
name old time/op new time/op delta Call-16 2.98µs ±0% 2.90µs ±2% -2.61% (p=0.000 n=9+10) name old alloc/op new alloc/op delta Call-16 24.0B ±0% 0.0B -100.00% (p=0.000 n=10+10) name old allocs/op new allocs/op delta Call-16 1.00 ±0% 0.00 -100.00% (p=0.000 n=10+10)
1 parent 2718130 commit 29d6727

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

mainthread.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ import (
99
"sync"
1010
)
1111

12-
var funcQ = make(chan func(), runtime.GOMAXPROCS(0))
12+
var funcQ = make(chan funcData, runtime.GOMAXPROCS(0))
1313

1414
func init() {
1515
runtime.LockOSThread()
1616
}
1717

18+
type funcData struct {
19+
fn func()
20+
done chan struct{}
21+
}
22+
1823
// Init initializes the functionality for running arbitrary subsequent
1924
// functions on a main system thread.
2025
//
@@ -33,7 +38,12 @@ func Init(main func()) {
3338
for {
3439
select {
3540
case f := <-funcQ:
36-
f()
41+
func() {
42+
defer func() {
43+
f.done <- struct{}{}
44+
}()
45+
f.fn()
46+
}()
3747
case <-done:
3848
return
3949
}
@@ -45,12 +55,7 @@ func Call(f func()) {
4555
done := donePool.Get().(chan struct{})
4656
defer donePool.Put(done)
4757

48-
funcQ <- func() {
49-
defer func() {
50-
done <- struct{}{}
51-
}()
52-
f()
53-
}
58+
funcQ <- funcData{fn: f, done: done}
5459
<-done
5560
}
5661

0 commit comments

Comments
 (0)