Skip to content

Commit 777d76c

Browse files
1911860538gopherbot
authored andcommitted
text/template: use sync.OnceValue for builtinFuncs
Replaced sync.Once with sync.OnceValue to simplify code and reduce globals. Change-Id: I0586df379b855950eacc5b98baad68f6ba0ba129 GitHub-Last-Rev: 7540b1e GitHub-Pull-Request: #73689 Reviewed-on: https://go-review.googlesource.com/c/go/+/672235 Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 0201524 commit 777d76c

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

src/text/template/funcs.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,13 @@ func builtins() FuncMap {
6262
}
6363
}
6464

65-
var builtinFuncsOnce struct {
66-
sync.Once
67-
v map[string]reflect.Value
68-
}
69-
70-
// builtinFuncsOnce lazily computes & caches the builtinFuncs map.
71-
// TODO: revert this back to a global map once golang.org/issue/2559 is fixed.
72-
func builtinFuncs() map[string]reflect.Value {
73-
builtinFuncsOnce.Do(func() {
74-
builtinFuncsOnce.v = createValueFuncs(builtins())
75-
})
76-
return builtinFuncsOnce.v
77-
}
78-
79-
// createValueFuncs turns a FuncMap into a map[string]reflect.Value
80-
func createValueFuncs(funcMap FuncMap) map[string]reflect.Value {
81-
m := make(map[string]reflect.Value)
65+
// builtinFuncs lazily computes & caches the builtinFuncs map.
66+
var builtinFuncs = sync.OnceValue(func() map[string]reflect.Value {
67+
funcMap := builtins()
68+
m := make(map[string]reflect.Value, len(funcMap))
8269
addValueFuncs(m, funcMap)
8370
return m
84-
}
71+
})
8572

8673
// addValueFuncs adds to values the functions in funcs, converting them to reflect.Values.
8774
func addValueFuncs(out map[string]reflect.Value, in FuncMap) {

0 commit comments

Comments
 (0)