Skip to content

Commit

Permalink
Rework template handling for function and map lookups
Browse files Browse the repository at this point in the history
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 gohugoio#6594
  • Loading branch information
bep committed Dec 12, 2019
1 parent 167c015 commit 029ba4f
Show file tree
Hide file tree
Showing 41 changed files with 1,171 additions and 1,828 deletions.
23 changes: 13 additions & 10 deletions commands/server.go
Expand Up @@ -16,6 +16,7 @@ package commands
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/url"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/pkg/errors"

"github.com/gohugoio/hugo/livereload"
"github.com/gohugoio/hugo/tpl"

"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
Expand Down Expand Up @@ -287,7 +287,7 @@ func getRootWatchDirsStr(baseDir string, watchDirs []string) string {
type fileServer struct {
baseURLs []string
roots []string
errorTemplate tpl.Template
errorTemplate func(err interface{}) (io.Reader, error)
c *commandeer
s *serverCmd
}
Expand Down Expand Up @@ -335,16 +335,15 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
err := f.c.getErrorWithContext()
if err != nil {
w.WriteHeader(500)
var b bytes.Buffer
err := f.errorTemplate.Execute(&b, err)
r, err := f.errorTemplate(err)
if err != nil {
f.c.logger.ERROR.Println(err)
}
port = 1313
if !f.c.paused {
port = f.c.Cfg.GetInt("liveReloadPort")
}
fmt.Fprint(w, injectLiveReloadScript(&b, port))
fmt.Fprint(w, injectLiveReloadScript(r, port))

return
}
Expand Down Expand Up @@ -422,11 +421,15 @@ func (c *commandeer) serve(s *serverCmd) error {
}

srv := &fileServer{
baseURLs: baseURLs,
roots: roots,
c: c,
s: s,
errorTemplate: templ,
baseURLs: baseURLs,
roots: roots,
c: c,
s: s,
errorTemplate: func(ctx interface{}) (io.Reader, error) {
b := &bytes.Buffer{}
err := c.hugo().Tmpl.Execute(templ, b, ctx)
return b, err
},
}

doLiveReload := !c.Cfg.GetBool("disableLiveReload")
Expand Down
4 changes: 2 additions & 2 deletions create/content_template_handler.go
Expand Up @@ -129,7 +129,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archety
archetypeTemplate = []byte(archetypeShortcodeReplacementsPre.Replace(string(archetypeTemplate)))

// Reuse the Hugo template setup to get the template funcs properly set up.
templateHandler := s.Deps.Tmpl.(tpl.TemplateHandler)
templateHandler := s.Deps.Tmpl.(tpl.TemplateManager)
templateName := "_text/" + helpers.Filename(archetypeFilename)
if err := templateHandler.AddTemplate(templateName, string(archetypeTemplate)); err != nil {
return nil, errors.Wrapf(err, "Failed to parse archetype file %q:", archetypeFilename)
Expand All @@ -138,7 +138,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archety
templ, _ := templateHandler.Lookup(templateName)

var buff bytes.Buffer
if err := templ.Execute(&buff, data); err != nil {
if err := templateHandler.Execute(templ, &buff, data); err != nil {
return nil, errors.Wrapf(err, "Failed to process archetype file %q:", archetypeFilename)
}

Expand Down
54 changes: 30 additions & 24 deletions deps/deps.go
Expand Up @@ -37,8 +37,8 @@ type Deps struct {
// Used to log warnings that may repeat itself many times.
DistinctWarningLog *helpers.DistinctLogger

// The templates to use. This will usually implement the full tpl.TemplateHandler.
Tmpl tpl.TemplateFinder `json:"-"`
// The templates to use. This will usually implement the full tpl.TemplateManager.
Tmpl tpl.TemplateHandler `json:"-"`

// We use this to parse and execute ad-hoc text templates.
TextTmpl tpl.TemplateParseFinder `json:"-"`
Expand Down Expand Up @@ -77,7 +77,10 @@ type Deps struct {
OutputFormatsConfig output.Formats

templateProvider ResourceProvider
WithTemplate func(templ tpl.TemplateHandler) error `json:"-"`
WithTemplate func(templ tpl.TemplateManager) error `json:"-"`

// Used in tests
OverloadedTemplateFuncs map[string]interface{}

translationProvider ResourceProvider

Expand Down Expand Up @@ -151,8 +154,8 @@ type ResourceProvider interface {
}

// TemplateHandler returns the used tpl.TemplateFinder as tpl.TemplateHandler.
func (d *Deps) TemplateHandler() tpl.TemplateHandler {
return d.Tmpl.(tpl.TemplateHandler)
func (d *Deps) TemplateHandler() tpl.TemplateManager {
return d.Tmpl.(tpl.TemplateManager)
}

// LoadResources loads translations and templates.
Expand Down Expand Up @@ -239,24 +242,25 @@ func New(cfg DepsCfg) (*Deps, error) {
distinctWarnLogger := helpers.NewDistinctLogger(logger.WARN)

d := &Deps{
Fs: fs,
Log: logger,
DistinctErrorLog: distinctErrorLogger,
DistinctWarningLog: distinctWarnLogger,
templateProvider: cfg.TemplateProvider,
translationProvider: cfg.TranslationProvider,
WithTemplate: cfg.WithTemplate,
PathSpec: ps,
ContentSpec: contentSpec,
SourceSpec: sp,
ResourceSpec: resourceSpec,
Cfg: cfg.Language,
Language: cfg.Language,
Site: cfg.Site,
FileCaches: fileCaches,
BuildStartListeners: &Listeners{},
Timeout: time.Duration(timeoutms) * time.Millisecond,
globalErrHandler: &globalErrHandler{},
Fs: fs,
Log: logger,
DistinctErrorLog: distinctErrorLogger,
DistinctWarningLog: distinctWarnLogger,
templateProvider: cfg.TemplateProvider,
translationProvider: cfg.TranslationProvider,
WithTemplate: cfg.WithTemplate,
OverloadedTemplateFuncs: cfg.OverloadedTemplateFuncs,
PathSpec: ps,
ContentSpec: contentSpec,
SourceSpec: sp,
ResourceSpec: resourceSpec,
Cfg: cfg.Language,
Language: cfg.Language,
Site: cfg.Site,
FileCaches: fileCaches,
BuildStartListeners: &Listeners{},
Timeout: time.Duration(timeoutms) * time.Millisecond,
globalErrHandler: &globalErrHandler{},
}

if cfg.Cfg.GetBool("templateMetrics") {
Expand Down Expand Up @@ -344,7 +348,9 @@ type DepsCfg struct {

// Template handling.
TemplateProvider ResourceProvider
WithTemplate func(templ tpl.TemplateHandler) error
WithTemplate func(templ tpl.TemplateManager) error
// Used in tests
OverloadedTemplateFuncs map[string]interface{}

// i18n handling.
TranslationProvider ResourceProvider
Expand Down
43 changes: 12 additions & 31 deletions hugolib/alias.go
Expand Up @@ -15,6 +15,7 @@ package hugolib

import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
Expand All @@ -31,27 +32,15 @@ import (
"github.com/gohugoio/hugo/tpl"
)

const (
alias = "<!DOCTYPE html><html><head><title>{{ .Permalink }}</title><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta name=\"robots\" content=\"noindex\"><meta charset=\"utf-8\" /><meta http-equiv=\"refresh\" content=\"0; url={{ .Permalink }}\" /></head></html>"
aliasXHtml = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>{{ .Permalink }}</title><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta name=\"robots\" content=\"noindex\"><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0; url={{ .Permalink }}\" /></head></html>"
)

var defaultAliasTemplates *template.Template

func init() {
//TODO(bep) consolidate
defaultAliasTemplates = template.New("")
template.Must(defaultAliasTemplates.New("alias").Parse(alias))
template.Must(defaultAliasTemplates.New("alias-xhtml").Parse(aliasXHtml))
}

type aliasHandler struct {
t tpl.TemplateFinder
t tpl.TemplateHandler
log *loggers.Logger
allowRoot bool
}

func newAliasHandler(t tpl.TemplateFinder, l *loggers.Logger, allowRoot bool) aliasHandler {
func newAliasHandler(t tpl.TemplateHandler, l *loggers.Logger, allowRoot bool) aliasHandler {
return aliasHandler{t, l, allowRoot}
}

Expand All @@ -60,33 +49,27 @@ type aliasPage struct {
page.Page
}

func (a aliasHandler) renderAlias(isXHTML bool, permalink string, p page.Page) (io.Reader, error) {
t := "alias"
if isXHTML {
t = "alias-xhtml"
}
func (a aliasHandler) renderAlias(permalink string, p page.Page) (io.Reader, error) {

var templ tpl.Template
var found bool

if a.t != nil {
templ, found = a.t.Lookup("alias.html")
}

templ, found = a.t.Lookup("alias.html")
if !found {
def := defaultAliasTemplates.Lookup(t)
if def != nil {
templ = &tpl.TemplateAdapter{Template: def}
// TODO(bep) consolidate
templ, found = a.t.Lookup("_internal/alias.html")
if !found {
return nil, errors.New("no alias template found")
}

}

data := aliasPage{
permalink,
p,
}

buffer := new(bytes.Buffer)
err := templ.Execute(buffer, data)
err := a.t.Execute(templ, buffer, data)
if err != nil {
return nil, err
}
Expand All @@ -100,16 +83,14 @@ func (s *Site) writeDestAlias(path, permalink string, outputFormat output.Format
func (s *Site) publishDestAlias(allowRoot bool, path, permalink string, outputFormat output.Format, p page.Page) (err error) {
handler := newAliasHandler(s.Tmpl, s.Log, allowRoot)

isXHTML := strings.HasSuffix(path, ".xhtml")

s.Log.DEBUG.Println("creating alias:", path, "redirecting to", permalink)

targetPath, err := handler.targetPathAlias(path)
if err != nil {
return err
}

aliasContent, err := handler.renderAlias(isXHTML, permalink, p)
aliasContent, err := handler.renderAlias(permalink, p)
if err != nil {
return err
}
Expand Down
74 changes: 0 additions & 74 deletions hugolib/case_insensitive_test.go
Expand Up @@ -14,7 +14,6 @@
package hugolib

import (
"fmt"
"path/filepath"
"testing"

Expand Down Expand Up @@ -232,76 +231,3 @@ Page2: {{ $page2.Params.ColoR }}
"index2|Site: yellow|",
)
}

// TODO1
func TestCaseInsensitiveConfigurationForAllTemplateEngines(t *testing.T) {
t.Parallel()

noOp := func(s string) string {
return s
}

for _, config := range []struct {
suffix string
templateFixer func(s string) string
}{
//{"amber", amberFixer},
{"html", noOp},
//{"ace", noOp},
} {
doTestCaseInsensitiveConfigurationForTemplateEngine(t, config.suffix, config.templateFixer)

}

}

func doTestCaseInsensitiveConfigurationForTemplateEngine(t *testing.T, suffix string, templateFixer func(s string) string) {
c := qt.New(t)
mm := afero.NewMemMapFs()

caseMixingTestsWriteCommonSources(t, mm)

cfg, err := LoadConfigDefault(mm)
c.Assert(err, qt.IsNil)

fs := hugofs.NewFrom(mm, cfg)

th := newTestHelper(cfg, fs, t)

t.Log("Testing", suffix)

templTemplate := `
p
|
| Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }}
| Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }}
| {{ .Content }}
`

templ := templateFixer(templTemplate)

t.Log(templ)

writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ)

sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})

if err != nil {
t.Fatalf("Failed to create sites: %s", err)
}

err = sites.Build(BuildCfg{})

if err != nil {
t.Fatalf("Failed to build sites: %s", err)
}

th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"),
"Page Colors: red|heavenly",
"Site Colors: green|yellow",
"Shortcode Page: red|heavenly",
"Shortcode Site: green|yellow",
)

}

0 comments on commit 029ba4f

Please sign in to comment.