Skip to content

Commit bff3ce4

Browse files
aclementseric
authored andcommitted
sync: implement OnceFunc, OnceValue, and OnceValues
This adds the three functions from golang#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 golang#56102 Change-Id: If7355eccd7c8de7288d89a4282ff15ab1469e420 Reviewed-on: https://go-review.googlesource.com/c/go/+/451356 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Andrew Gerrand <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Caleb Spare <[email protected]> Auto-Submit: Austin Clements <[email protected]>
1 parent a1e38ad commit bff3ce4

File tree

4 files changed

+374
-0
lines changed

4 files changed

+374
-0
lines changed

api/next/56102.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pkg sync, func OnceFunc(func()) func() #56102
2+
pkg sync, func OnceValue[$0 interface{}](func() $0) func() $0 #56102
3+
pkg sync, func OnceValues[$0 interface{}, $1 interface{}](func() ($0, $1)) func() ($0, $1) #56102

src/cmd/compile/internal/test/inl_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ func TestIntendedInlining(t *testing.T) {
176176
"net": {
177177
"(*UDPConn).ReadFromUDP",
178178
},
179+
"sync": {
180+
// Both OnceFunc and its returned closure need to be inlinable so
181+
// that the returned closure can be inlined into the caller of OnceFunc.
182+
"OnceFunc",
183+
"OnceFunc.func2", // The returned closure.
184+
// TODO(austin): It would be good to check OnceValue and OnceValues,
185+
// too, but currently they aren't reported because they have type
186+
// parameters and aren't instantiated in sync.
187+
},
179188
"sync/atomic": {
180189
// (*Bool).CompareAndSwap handled below.
181190
"(*Bool).Load",

src/sync/oncefunc.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package sync
6+
7+
// OnceFunc returns a function that invokes f only once. The returned function
8+
// may be called concurrently.
9+
//
10+
// If f panics, the returned function will panic with the same value on every call.
11+
func OnceFunc(f func()) func() {
12+
var (
13+
once Once
14+
valid bool
15+
p any
16+
)
17+
// Construct the inner closure just once to reduce costs on the fast path.
18+
g := func() {
19+
defer func() {
20+
p = recover()
21+
if !valid {
22+
// Re-panic immediately so on the first call the user gets a
23+
// complete stack trace into f.
24+
panic(p)
25+
}
26+
}()
27+
f()
28+
valid = true // Set only if f does not panic
29+
}
30+
return func() {
31+
once.Do(g)
32+
if !valid {
33+
panic(p)
34+
}
35+
}
36+
}
37+
38+
// OnceValue returns a function that invokes f only once and returns the value
39+
// returned by f. The returned function may be called concurrently.
40+
//
41+
// If f panics, the returned function will panic with the same value on every call.
42+
func OnceValue[T any](f func() T) func() T {
43+
var (
44+
once Once
45+
valid bool
46+
p any
47+
result T
48+
)
49+
g := func() {
50+
defer func() {
51+
p = recover()
52+
if !valid {
53+
panic(p)
54+
}
55+
}()
56+
result = f()
57+
valid = true
58+
}
59+
return func() T {
60+
once.Do(g)
61+
if !valid {
62+
panic(p)
63+
}
64+
return result
65+
}
66+
}
67+
68+
// OnceValues returns a function that invokes f only once and returns the values
69+
// returned by f. The returned function may be called concurrently.
70+
//
71+
// If f panics, the returned function will panic with the same value on every call.
72+
func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
73+
var (
74+
once Once
75+
valid bool
76+
p any
77+
r1 T1
78+
r2 T2
79+
)
80+
g := func() {
81+
defer func() {
82+
p = recover()
83+
if !valid {
84+
panic(p)
85+
}
86+
}()
87+
r1, r2 = f()
88+
valid = true
89+
}
90+
return func() (T1, T2) {
91+
once.Do(g)
92+
if !valid {
93+
panic(p)
94+
}
95+
return r1, r2
96+
}
97+
}

src/sync/oncefunc_test.go

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package sync_test
6+
7+
import (
8+
"bytes"
9+
"runtime"
10+
"runtime/debug"
11+
"sync"
12+
"testing"
13+
)
14+
15+
// We assume that the Once.Do tests have already covered parallelism.
16+
17+
func TestOnceFunc(t *testing.T) {
18+
calls := 0
19+
f := sync.OnceFunc(func() { calls++ })
20+
allocs := testing.AllocsPerRun(10, f)
21+
if calls != 1 {
22+
t.Errorf("want calls==1, got %d", calls)
23+
}
24+
if allocs != 0 {
25+
t.Errorf("want 0 allocations per call, got %v", allocs)
26+
}
27+
}
28+
29+
func TestOnceValue(t *testing.T) {
30+
calls := 0
31+
f := sync.OnceValue(func() int {
32+
calls++
33+
return calls
34+
})
35+
allocs := testing.AllocsPerRun(10, func() { f() })
36+
value := f()
37+
if calls != 1 {
38+
t.Errorf("want calls==1, got %d", calls)
39+
}
40+
if value != 1 {
41+
t.Errorf("want value==1, got %d", value)
42+
}
43+
if allocs != 0 {
44+
t.Errorf("want 0 allocations per call, got %v", allocs)
45+
}
46+
}
47+
48+
func TestOnceValues(t *testing.T) {
49+
calls := 0
50+
f := sync.OnceValues(func() (int, int) {
51+
calls++
52+
return calls, calls + 1
53+
})
54+
allocs := testing.AllocsPerRun(10, func() { f() })
55+
v1, v2 := f()
56+
if calls != 1 {
57+
t.Errorf("want calls==1, got %d", calls)
58+
}
59+
if v1 != 1 || v2 != 2 {
60+
t.Errorf("want v1==1 and v2==2, got %d and %d", v1, v2)
61+
}
62+
if allocs != 0 {
63+
t.Errorf("want 0 allocations per call, got %v", allocs)
64+
}
65+
}
66+
67+
func testOncePanicX(t *testing.T, calls *int, f func()) {
68+
testOncePanicWith(t, calls, f, func(label string, p any) {
69+
if p != "x" {
70+
t.Fatalf("%s: want panic %v, got %v", label, "x", p)
71+
}
72+
})
73+
}
74+
75+
func testOncePanicWith(t *testing.T, calls *int, f func(), check func(label string, p any)) {
76+
// Check that the each call to f panics with the same value, but the
77+
// underlying function is only called once.
78+
for _, label := range []string{"first time", "second time"} {
79+
var p any
80+
panicked := true
81+
func() {
82+
defer func() {
83+
p = recover()
84+
}()
85+
f()
86+
panicked = false
87+
}()
88+
if !panicked {
89+
t.Fatalf("%s: f did not panic", label)
90+
}
91+
check(label, p)
92+
}
93+
if *calls != 1 {
94+
t.Errorf("want calls==1, got %d", *calls)
95+
}
96+
}
97+
98+
func TestOnceFuncPanic(t *testing.T) {
99+
calls := 0
100+
f := sync.OnceFunc(func() {
101+
calls++
102+
panic("x")
103+
})
104+
testOncePanicX(t, &calls, f)
105+
}
106+
107+
func TestOnceValuePanic(t *testing.T) {
108+
calls := 0
109+
f := sync.OnceValue(func() int {
110+
calls++
111+
panic("x")
112+
})
113+
testOncePanicX(t, &calls, func() { f() })
114+
}
115+
116+
func TestOnceValuesPanic(t *testing.T) {
117+
calls := 0
118+
f := sync.OnceValues(func() (int, int) {
119+
calls++
120+
panic("x")
121+
})
122+
testOncePanicX(t, &calls, func() { f() })
123+
}
124+
125+
func TestOnceFuncPanicNil(t *testing.T) {
126+
calls := 0
127+
f := sync.OnceFunc(func() {
128+
calls++
129+
panic(nil)
130+
})
131+
testOncePanicWith(t, &calls, f, func(label string, p any) {
132+
switch p.(type) {
133+
case nil, *runtime.PanicNilError:
134+
return
135+
}
136+
t.Fatalf("%s: want nil panic, got %v", label, p)
137+
})
138+
}
139+
140+
func TestOnceFuncGoexit(t *testing.T) {
141+
// If f calls Goexit, the results are unspecified. But check that f doesn't
142+
// get called twice.
143+
calls := 0
144+
f := sync.OnceFunc(func() {
145+
calls++
146+
runtime.Goexit()
147+
})
148+
var wg sync.WaitGroup
149+
for i := 0; i < 2; i++ {
150+
wg.Add(1)
151+
go func() {
152+
defer wg.Done()
153+
defer func() { recover() }()
154+
f()
155+
}()
156+
wg.Wait()
157+
}
158+
if calls != 1 {
159+
t.Errorf("want calls==1, got %d", calls)
160+
}
161+
}
162+
163+
func TestOnceFuncPanicTraceback(t *testing.T) {
164+
// Test that on the first invocation of a OnceFunc, the stack trace goes all
165+
// the way to the origin of the panic.
166+
f := sync.OnceFunc(onceFuncPanic)
167+
168+
defer func() {
169+
if p := recover(); p != "x" {
170+
t.Fatalf("want panic %v, got %v", "x", p)
171+
}
172+
stack := debug.Stack()
173+
want := "sync_test.onceFuncPanic"
174+
if !bytes.Contains(stack, []byte(want)) {
175+
t.Fatalf("want stack containing %v, got:\n%s", want, string(stack))
176+
}
177+
}()
178+
f()
179+
}
180+
181+
func onceFuncPanic() {
182+
panic("x")
183+
}
184+
185+
var (
186+
onceFunc = sync.OnceFunc(func() {})
187+
188+
onceFuncOnce sync.Once
189+
)
190+
191+
func doOnceFunc() {
192+
onceFuncOnce.Do(func() {})
193+
}
194+
195+
func BenchmarkOnceFunc(b *testing.B) {
196+
b.Run("v=Once", func(b *testing.B) {
197+
b.ReportAllocs()
198+
for i := 0; i < b.N; i++ {
199+
// The baseline is direct use of sync.Once.
200+
doOnceFunc()
201+
}
202+
})
203+
b.Run("v=Global", func(b *testing.B) {
204+
b.ReportAllocs()
205+
for i := 0; i < b.N; i++ {
206+
// As of 3/2023, the compiler doesn't recognize that onceFunc is
207+
// never mutated and is a closure that could be inlined.
208+
// Too bad, because this is how OnceFunc will usually be used.
209+
onceFunc()
210+
}
211+
})
212+
b.Run("v=Local", func(b *testing.B) {
213+
b.ReportAllocs()
214+
// As of 3/2023, the compiler *does* recognize this local binding as an
215+
// inlinable closure. This is the best case for OnceFunc, but probably
216+
// not typical usage.
217+
f := sync.OnceFunc(func() {})
218+
for i := 0; i < b.N; i++ {
219+
f()
220+
}
221+
})
222+
}
223+
224+
var (
225+
onceValue = sync.OnceValue(func() int { return 42 })
226+
227+
onceValueOnce sync.Once
228+
onceValueValue int
229+
)
230+
231+
func doOnceValue() int {
232+
onceValueOnce.Do(func() {
233+
onceValueValue = 42
234+
})
235+
return onceValueValue
236+
}
237+
238+
func BenchmarkOnceValue(b *testing.B) {
239+
// See BenchmarkOnceFunc
240+
b.Run("v=Once", func(b *testing.B) {
241+
b.ReportAllocs()
242+
for i := 0; i < b.N; i++ {
243+
if want, got := 42, doOnceValue(); want != got {
244+
b.Fatalf("want %d, got %d", want, got)
245+
}
246+
}
247+
})
248+
b.Run("v=Global", func(b *testing.B) {
249+
b.ReportAllocs()
250+
for i := 0; i < b.N; i++ {
251+
if want, got := 42, onceValue(); want != got {
252+
b.Fatalf("want %d, got %d", want, got)
253+
}
254+
}
255+
})
256+
b.Run("v=Local", func(b *testing.B) {
257+
b.ReportAllocs()
258+
onceValue := sync.OnceValue(func() int { return 42 })
259+
for i := 0; i < b.N; i++ {
260+
if want, got := 42, onceValue(); want != got {
261+
b.Fatalf("want %d, got %d", want, got)
262+
}
263+
}
264+
})
265+
}

0 commit comments

Comments
 (0)