diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-07-13 14:47:32 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-07-13 15:49:00 +0200 |
commit | 9c24b86e4b634d7db761a3e57c1b64fead880aff (patch) | |
tree | e3f65eeac6cec1f6cd78f8709e3625ff84204e76 /tpl | |
parent | 223bf2800488ad5d38854bbb595d789bc35ebe32 (diff) | |
download | hugo-9c24b86e4b634d7db761a3e57c1b64fead880aff.tar.gz hugo-9c24b86e4b634d7db761a3e57c1b64fead880aff.zip |
Cache when not found in LookupLayout
Very visible when using the pprof mutex profiler.
```bash
name old time/op new time/op delta
Baseline-10 58.4ms ± 1% 51.6ms ± 0% -11.56% (p=0.029 n=4+4)
name old alloc/op new alloc/op delta
Baseline-10 64.3MB ± 0% 64.2MB ± 0% ~ (p=0.114 n=4+4)
name old allocs/op new allocs/op delta
Baseline-10 649k ± 0% 649k ± 0% ~ (p=0.229 n=4+4)
```
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/tplimpl/template.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index c79605cbc..81c898b2f 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -153,7 +153,7 @@ func newTemplateExec(d *deps.Deps) (*templateExec, error) { Deps: d, layoutHandler: output.NewLayoutHandler(), layoutsFs: d.BaseFs.Layouts.Fs, - layoutTemplateCache: make(map[layoutCacheKey]tpl.Template), + layoutTemplateCache: make(map[layoutCacheKey]layoutCacheEntry), templateUsageTracker: templateUsageTracker, } @@ -328,7 +328,7 @@ type templateHandler struct { layoutHandler *output.LayoutHandler - layoutTemplateCache map[layoutCacheKey]tpl.Template + layoutTemplateCache map[layoutCacheKey]layoutCacheEntry layoutTemplateCacheMu sync.RWMutex *deps.Deps @@ -357,6 +357,12 @@ type templateHandler struct { templateUsageTrackerMu sync.Mutex } +type layoutCacheEntry struct { + found bool + templ tpl.Template + err error +} + // AddTemplate parses and adds a template to the collection. // Templates with name prefixed with "_text" will be handled as plain // text templates. @@ -382,7 +388,7 @@ func (t *templateHandler) LookupLayout(d output.LayoutDescriptor, f output.Forma t.layoutTemplateCacheMu.RLock() if cacheVal, found := t.layoutTemplateCache[key]; found { t.layoutTemplateCacheMu.RUnlock() - return cacheVal, true, nil + return cacheVal.templ, cacheVal.found, cacheVal.err } t.layoutTemplateCacheMu.RUnlock() @@ -390,12 +396,10 @@ func (t *templateHandler) LookupLayout(d output.LayoutDescriptor, f output.Forma defer t.layoutTemplateCacheMu.Unlock() templ, found, err := t.findLayout(d, f) - if err == nil && found { - t.layoutTemplateCache[key] = templ - return templ, true, nil - } + cacheVal := layoutCacheEntry{found: found, templ: templ, err: err} + t.layoutTemplateCache[key] = cacheVal + return cacheVal.templ, cacheVal.found, cacheVal.err - return nil, false, err } // This currently only applies to shortcodes and what we get here is the |