diff --git a/tpl/internal/go_templates/htmltemplate/hugo_template.go b/tpl/internal/go_templates/htmltemplate/hugo_template.go index 117d85e4967..eba54fbbf25 100644 --- a/tpl/internal/go_templates/htmltemplate/hugo_template.go +++ b/tpl/internal/go_templates/htmltemplate/hugo_template.go @@ -24,6 +24,9 @@ package is auto generated. */ +// Export it so we can populate Hugo's func map with it, which makes it faster. +var GoFuncs = funcMap + // Prepare returns a template ready for execution. func (t *Template) Prepare() (*template.Template, error) { if err := t.escape(); err != nil { diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go index d881064e52c..37fa969da92 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template.go @@ -29,6 +29,9 @@ package is auto generated. */ +// Export it so we can populate Hugo's func map with it, which makes it faster. +var GoFuncs = builtinFuncs + // Preparer prepares the template before execution. type Preparer interface { Prepare() (*Template, error) diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 6be8aa8b7b4..88869940d01 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -108,7 +108,25 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec funcsv := make(map[string]reflect.Value) for k, v := range funcs { - funcsv[k] = reflect.ValueOf(v) + vv := reflect.ValueOf(v) + funcsv[k] = vv + } + + // Duplicate Go's internal funcs here for faster lookups. + for k, v := range template.GoFuncs { + if _, exists := funcsv[k]; !exists { + vv, ok := v.(reflect.Value) + if !ok { + vv = reflect.ValueOf(v) + } + funcsv[k] = vv + } + } + + for k, v := range texttemplate.GoFuncs { + if _, exists := funcsv[k]; !exists { + funcsv[k] = v + } } exeHelper := &templateExecHelper{