Skip to content

Commit 558c2a9

Browse files
author
1911860538
committed
text/template: use sync.OnceValue for builtinFuncs
1 parent 1e756dc commit 558c2a9

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-
7065
// 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)
66+
var builtinFuncsOnce = 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) {
@@ -149,7 +136,7 @@ func findFunction(name string, tmpl *Template) (v reflect.Value, isBuiltin, ok b
149136
return fn, false, true
150137
}
151138
}
152-
if fn := builtinFuncs()[name]; fn.IsValid() {
139+
if fn := builtinFuncsOnce()[name]; fn.IsValid() {
153140
return fn, true, true
154141
}
155142
return reflect.Value{}, false, false

0 commit comments

Comments
 (0)