diff --git a/content/assets/zero-alloc-call-sched/mainthread-naive/bench-2021-01-25-16:13:23.txt b/content/assets/zero-alloc-call-sched/mainthread-naive/bench-2021-01-25-16:13:23.txt new file mode 100644 index 0000000..b728765 --- /dev/null +++ b/content/assets/zero-alloc-call-sched/mainthread-naive/bench-2021-01-25-16:13:23.txt @@ -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 diff --git a/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread.go b/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread.go new file mode 100644 index 0000000..816f2a9 --- /dev/null +++ b/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread.go @@ -0,0 +1,47 @@ +// Copyright (c) 2021 The golang.design Initiative Authors. +// All rights reserved. +// +// The code below is produced by Changkun Ou . + +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 +} diff --git a/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread_test.go b/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread_test.go new file mode 100644 index 0000000..470b244 --- /dev/null +++ b/content/assets/zero-alloc-call-sched/mainthread-naive/mainthread_test.go @@ -0,0 +1,31 @@ +// Copyright (c) 2021 The golang.design Initiative Authors. +// All rights reserved. +// +// The code below is produced by Changkun Ou . + +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) + } + }) +}