]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: use sync.OnceValue for builtinFuncs
author1911860538 <alxps1911@gmail.com>
Wed, 14 May 2025 15:25:40 +0000 (15:25 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 8 Aug 2025 19:53:12 +0000 (12:53 -0700)
Replaced sync.Once with sync.OnceValue to simplify code and reduce globals.

Change-Id: I0586df379b855950eacc5b98baad68f6ba0ba129
GitHub-Last-Rev: 7540b1efba864901c1dc00833a72cb9ed306c52f
GitHub-Pull-Request: golang/go#73689
Reviewed-on: https://go-review.googlesource.com/c/go/+/672235
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/text/template/funcs.go

index 4d733135fe5a85e0541f1f04f967a8d9fbdea63f..c28c3ea2002d212fbf2f5b243ddcb787d3d1a473 100644 (file)
@@ -62,26 +62,13 @@ func builtins() FuncMap {
        }
 }
 
-var builtinFuncsOnce struct {
-       sync.Once
-       v map[string]reflect.Value
-}
-
-// builtinFuncsOnce lazily computes & caches the builtinFuncs map.
-// TODO: revert this back to a global map once golang.org/issue/2559 is fixed.
-func builtinFuncs() map[string]reflect.Value {
-       builtinFuncsOnce.Do(func() {
-               builtinFuncsOnce.v = createValueFuncs(builtins())
-       })
-       return builtinFuncsOnce.v
-}
-
-// createValueFuncs turns a FuncMap into a map[string]reflect.Value
-func createValueFuncs(funcMap FuncMap) map[string]reflect.Value {
-       m := make(map[string]reflect.Value)
+// builtinFuncs lazily computes & caches the builtinFuncs map.
+var builtinFuncs = sync.OnceValue(func() map[string]reflect.Value {
+       funcMap := builtins()
+       m := make(map[string]reflect.Value, len(funcMap))
        addValueFuncs(m, funcMap)
        return m
-}
+})
 
 // addValueFuncs adds to values the functions in funcs, converting them to reflect.Values.
 func addValueFuncs(out map[string]reflect.Value, in FuncMap) {