Rework template handling for function and map lookups

This is a big commit, but it deletes lots of code and simplifies a lot.

* Resolving the template funcs at execution time means we don't have to create template clones per site
* Having a custom map resolver means that we can remove the AST lower case transformation for the special lower case Params map

Not only is the above easier to reason about, it's also faster, especially if you have more than one language, as in the benchmark below:

```
name                          old time/op    new time/op    delta
SiteNew/Deep_content_tree-16    53.7ms ± 0%    48.1ms ± 2%  -10.38%  (p=0.029 n=4+4)

name                          old alloc/op   new alloc/op   delta
SiteNew/Deep_content_tree-16    41.0MB ± 0%    36.8MB ± 0%  -10.26%  (p=0.029 n=4+4)

name                          old allocs/op  new allocs/op  delta
SiteNew/Deep_content_tree-16      481k ± 0%      410k ± 0%  -14.66%  (p=0.029 n=4+4)
```

This should be even better if you also have lots of templates.

Closes #6594
This commit is contained in:
Bjørn Erik Pedersen
2019-12-10 19:56:44 +01:00
parent 167c01530b
commit a03c631c42
41 changed files with 1194 additions and 1898 deletions

View File

@@ -14,9 +14,7 @@
package tplimpl
import (
"github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
"strings"
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
@@ -27,19 +25,6 @@ import (
"github.com/pkg/errors"
)
// decl keeps track of the variable mappings, i.e. $mysite => .Site etc.
type decl map[string]string
const (
paramsIdentifier = "Params"
)
// Containers that may contain Params that we will not touch.
var reservedContainers = map[string]bool{
// Aka .Site.Data.Params which must stay case sensitive.
"Data": true,
}
type templateType int
const (
@@ -49,7 +34,6 @@ const (
)
type templateContext struct {
decl decl
visited map[string]bool
notFound map[string]bool
lookupFn func(name string) *parse.Tree
@@ -89,7 +73,6 @@ func newTemplateContext(lookupFn func(name string) *parse.Tree) *templateContext
return &templateContext{
Info: tpl.Info{Config: tpl.DefaultConfig},
lookupFn: lookupFn,
decl: make(map[string]string),
visited: make(map[string]bool),
notFound: make(map[string]bool)}
}
@@ -219,11 +202,6 @@ func (c *templateContext) applyTransformations(n parse.Node) (bool, error) {
}
case *parse.PipeNode:
c.collectConfig(x)
if len(x.Decl) == 1 && len(x.Cmds) == 1 {
// maps $site => .Site etc.
c.decl[x.Decl[0].Ident[0]] = x.Cmds[0].String()
}
for i, cmd := range x.Cmds {
keep, _ := c.applyTransformations(cmd)
if !keep {
@@ -237,17 +215,8 @@ func (c *templateContext) applyTransformations(n parse.Node) (bool, error) {
for _, elem := range x.Args {
switch an := elem.(type) {
case *parse.FieldNode:
c.updateIdentsIfNeeded(an.Ident)
case *parse.VariableNode:
c.updateIdentsIfNeeded(an.Ident)
case *parse.PipeNode:
c.applyTransformations(an)
case *parse.ChainNode:
// site.Params...
if len(an.Field) > 1 && an.Field[0] == paramsIdentifier {
c.updateIdentsIfNeeded(an.Field)
}
}
}
return keep, c.err
@@ -262,19 +231,6 @@ func (c *templateContext) applyTransformationsToNodes(nodes ...parse.Node) {
}
}
func (c *templateContext) updateIdentsIfNeeded(idents []string) {
index := c.decl.indexOfReplacementStart(idents)
if index == -1 {
return
}
for i := index; i < len(idents); i++ {
idents[i] = strings.ToLower(idents[i])
}
}
func (c *templateContext) hasIdent(idents []string, ident string) bool {
for _, id := range idents {
if id == ident {
@@ -376,160 +332,3 @@ func (c *templateContext) collectReturnNode(n *parse.CommandNode) bool {
return false
}
// indexOfReplacementStart will return the index of where to start doing replacement,
// -1 if none needed.
func (d decl) indexOfReplacementStart(idents []string) int {
l := len(idents)
if l == 0 {
return -1
}
if l == 1 {
first := idents[0]
if first == "" || first == paramsIdentifier || first[0] == '$' {
// This can not be a Params.x
return -1
}
}
var lookFurther bool
var needsVarExpansion bool
for _, ident := range idents {
if ident[0] == '$' {
lookFurther = true
needsVarExpansion = true
break
} else if ident == paramsIdentifier {
lookFurther = true
break
}
}
if !lookFurther {
return -1
}
var resolvedIdents []string
if !needsVarExpansion {
resolvedIdents = idents
} else {
var ok bool
resolvedIdents, ok = d.resolveVariables(idents)
if !ok {
return -1
}
}
var paramFound bool
for i, ident := range resolvedIdents {
if ident == paramsIdentifier {
if i > 0 {
container := resolvedIdents[i-1]
if reservedContainers[container] {
// .Data.Params.someKey
return -1
}
}
paramFound = true
break
}
}
if !paramFound {
return -1
}
var paramSeen bool
idx := -1
for i, ident := range idents {
if ident == "" || ident[0] == '$' {
continue
}
if ident == paramsIdentifier {
paramSeen = true
idx = -1
} else {
if paramSeen {
return i
}
if idx == -1 {
idx = i
}
}
}
return idx
}
func (d decl) resolveVariables(idents []string) ([]string, bool) {
var (
replacements []string
replaced []string
)
// An Ident can start out as one of
// [Params] [$blue] [$colors.Blue]
// We need to resolve the variables, so
// $blue => [Params Colors Blue]
// etc.
replacements = []string{idents[0]}
// Loop until there are no more $vars to resolve.
for i := 0; i < len(replacements); i++ {
if i > 20 {
// bail out
return nil, false
}
potentialVar := replacements[i]
if potentialVar == "$" {
continue
}
if potentialVar == "" || potentialVar[0] != '$' {
// leave it as is
replaced = append(replaced, strings.Split(potentialVar, ".")...)
continue
}
replacement, ok := d[potentialVar]
if !ok {
// Temporary range vars. We do not care about those.
return nil, false
}
if !d.isKeyword(replacement) {
continue
}
replacement = strings.TrimPrefix(replacement, ".")
if replacement == "" {
continue
}
if replacement[0] == '$' {
// Needs further expansion
replacements = append(replacements, strings.Split(replacement, ".")...)
} else {
replaced = append(replaced, strings.Split(replacement, ".")...)
}
}
return append(replaced, idents[1:]...), true
}
func (d decl) isKeyword(s string) bool {
return !strings.ContainsAny(s, " -\"")
}