go/src/sync
Austin Clements bb44c2b54e sync: implement OnceFunc, OnceValue, and OnceValues
This adds the three functions from #56102 to the sync package. These
provide a convenient API for the most common uses of sync.Once.

The performance of these is comparable to direct use of sync.Once:

$ go test -run ^$ -bench OnceFunc\|OnceVal -count 20 | benchstat -row .name -col /v
goos: linux
goarch: amd64
pkg: sync
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
          │     Once     │                Global                 │                Local                 │
          │    sec/op    │    sec/op     vs base                 │    sec/op     vs base                │
OnceFunc    1.3500n ± 6%   2.7030n ± 1%  +100.22% (p=0.000 n=20)   0.3935n ± 0%  -70.86% (p=0.000 n=20)
OnceValue   1.3155n ± 0%   2.7460n ± 1%  +108.74% (p=0.000 n=20)   0.5478n ± 1%  -58.35% (p=0.000 n=20)

The "Once" column represents the baseline of how code would typically
express these patterns using sync.Once. "Global" binds the closure
returned by OnceFunc/OnceValue to global, which is how I expect these
to be used most of the time. Currently, this defeats some inlining
opportunities, which roughly doubles the cost over sync.Once; however,
it's still *extremely* fast. Finally, "Local" binds the returned
closure to a local variable. This unlocks several levels of inlining
and represents pretty much the best possible case for these APIs, but
is also unlikely to happen in practice. In principle the compiler
could recognize that the global in the "Global" case is initialized in
place and never mutated and do the same optimizations it does in the
"Local" case, but it currently does not.

Fixes #56102

Change-Id: If7355eccd7c8de7288d89a4282ff15ab1469e420
Reviewed-on: https://go-review.googlesource.com/c/go/+/451356
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Caleb Spare <cespare@gmail.com>
Auto-Submit: Austin Clements <austin@google.com>
2023-03-31 20:01:17 +00:00
..
atomic all: add a few links in package godocs 2023-03-25 10:16:23 +00:00
cond.go sync: clarify Cond.Wait documentation 2022-09-26 21:58:42 +00:00
cond_test.go sync: fix TestCondSignalStealing flake 2023-03-22 18:52:42 +00:00
example_pool_test.go
example_test.go
export_test.go
map.go sync: document memory model for Swap/CompareAnd{Swap,Delete} in Map 2023-01-20 19:38:29 +00:00
map_bench_test.go sync: remove unused const 2022-12-12 14:30:12 +00:00
map_reference_test.go sync: add new Map method Swap, CompareAndSwap, CompareAndDelete 2022-11-15 17:35:42 +00:00
map_test.go sync: add new Map method Swap, CompareAndSwap, CompareAndDelete 2022-11-15 17:35:42 +00:00
mutex.go
mutex_test.go
once.go Revert "sync: convert Once.done to atomic type" 2022-09-07 13:22:04 +00:00
once_test.go
oncefunc.go sync: implement OnceFunc, OnceValue, and OnceValues 2023-03-31 20:01:17 +00:00
oncefunc_test.go sync: implement OnceFunc, OnceValue, and OnceValues 2023-03-31 20:01:17 +00:00
pool.go
pool_test.go
poolqueue.go
runtime.go runtime: make the wait reason for a g blocked on a mutex more specific 2022-09-16 16:32:27 +00:00
runtime2.go
runtime2_lockrank.go
runtime_sema_test.go
rwmutex.go runtime: make the wait reason for a g blocked on a mutex more specific 2022-09-16 16:32:27 +00:00
rwmutex_test.go
waitgroup.go
waitgroup_test.go