From: 1911860538 Date: Wed, 14 May 2025 15:25:40 +0000 (+0000) Subject: text/template: use sync.OnceValue for builtinFuncs X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=777d76c4f2392b782418a867172e7e951c0278f8;p=gostls13.git 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: 7540b1efba864901c1dc00833a72cb9ed306c52f GitHub-Pull-Request: golang/go#73689 Reviewed-on: https://go-review.googlesource.com/c/go/+/672235 Reviewed-by: Cherry Mui Auto-Submit: Sean Liao LUCI-TryBot-Result: Go LUCI Reviewed-by: Sean Liao Reviewed-by: Dmitri Shuralyov --- diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go index 4d733135fe..c28c3ea200 100644 --- a/src/text/template/funcs.go +++ b/src/text/template/funcs.go @@ -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) {