-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
content: add mainthread-naive implementation
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
content/assets/zero-alloc-call-sched/mainthread-naive/bench-2021-01-25-16:13:23.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
goos: darwin | ||
goarch: arm64 | ||
pkg: x/mainthread | ||
BenchmarkDirectCall-8 1000000000 0.9441 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9443 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9459 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9563 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9526 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9475 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9495 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9544 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9490 ns/op 0 B/op 0 allocs/op | ||
BenchmarkDirectCall-8 1000000000 0.9644 ns/op 0 B/op 0 allocs/op | ||
BenchmarkMainThreadCall-8 2673351 447.7 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2675954 446.5 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2675186 448.6 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2681828 447.4 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2674504 447.1 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2597884 457.2 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2670514 446.2 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2676504 449.6 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2672632 449.1 ns/op 120 B/op 2 allocs/op | ||
BenchmarkMainThreadCall-8 2668776 448.3 ns/op 120 B/op 2 allocs/op | ||
PASS | ||
ok x/mainthread 27.356s |
47 changes: 47 additions & 0 deletions
47
content/assets/zero-alloc-call-sched/mainthread-naive/mainthread.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2021 The golang.design Initiative Authors. | ||
// All rights reserved. | ||
// | ||
// The code below is produced by Changkun Ou <[email protected]>. | ||
|
||
package mainthread | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
func init() { | ||
runtime.LockOSThread() | ||
} | ||
|
||
var funcQ = make(chan func(), runtime.GOMAXPROCS(0)) | ||
|
||
// Init initializes the functionality of running arbitrary subsequent | ||
// functions be called on the main system thread. | ||
// | ||
// Init must be called in the main.main function. | ||
func Init(main func()) { | ||
done := make(chan struct{}) | ||
go func() { | ||
main() | ||
done <- struct{}{} | ||
}() | ||
|
||
for { | ||
select { | ||
case f := <-funcQ: | ||
f() | ||
case <-done: | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Call calls f on the main thread and blocks until f finishes. | ||
func Call(f func()) { | ||
done := make(chan struct{}) | ||
funcQ <- func() { | ||
f() | ||
done <- struct{}{} | ||
} | ||
<-done | ||
} |
31 changes: 31 additions & 0 deletions
31
content/assets/zero-alloc-call-sched/mainthread-naive/mainthread_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2021 The golang.design Initiative Authors. | ||
// All rights reserved. | ||
// | ||
// The code below is produced by Changkun Ou <[email protected]>. | ||
|
||
package mainthread_test | ||
|
||
import ( | ||
"testing" | ||
mainthread "x/mainthread-naive" | ||
) | ||
|
||
var f = func() {} | ||
|
||
func BenchmarkDirectCall(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
f() | ||
} | ||
} | ||
|
||
func BenchmarkMainThreadCall(b *testing.B) { | ||
mainthread.Init(func() { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
mainthread.Call(f) | ||
} | ||
}) | ||
} |