mirror of
				https://github.com/gohugoio/hugo.git
				synced 2024-05-11 05:54:58 +00:00 
			
		
		
		
	tpl/partials: Make sure a cached partial is invoked only once
This commit revises the locking strategy for `partialCached`. We have added a benchmark that may be a little artificial, but it should at least show that we're not losing any performance over this:
```bash
name              old time/op    new time/op    delta
IncludeCached-10    12.2ms ± 2%    11.3ms ± 1%   -7.36%  (p=0.029 n=4+4)
name              old alloc/op   new alloc/op   delta
IncludeCached-10    7.17MB ± 0%    5.09MB ± 0%  -29.00%  (p=0.029 n=4+4)
name              old allocs/op  new allocs/op  delta
IncludeCached-10      128k ± 1%       70k ± 0%  -45.42%  (p=0.029 n=4+4)
```
This commit also revises the template metrics hints logic a little, and add a test for it, which output is currently this:
```bash
 cumulative       average       maximum      cache  percent  cached  total
       duration      duration      duration  potential   cached   count  count  template
     ----------      --------      --------  ---------  -------  ------  -----  --------
      163.334µs     163.334µs     163.334µs          0        0       0      1  index.html
       23.749µs       5.937µs      19.916µs         25       50       2      4  partials/dynamic1.html
        9.625µs       4.812µs        6.75µs        100       50       1      2  partials/static1.html
        7.625µs       7.625µs       7.625µs        100        0       0      1  partials/static2.html
```
Some notes:
* The duration now includes the cached invocations (which should be very short)
* A cached template gets executed once before it gets cached, so the "percent cached" will never be 100.
Fixes #4086
Fixes #9506
			
			
This commit is contained in:
		@@ -24,6 +24,7 @@ import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
 | 
			
		||||
 | 
			
		||||
@@ -44,6 +45,13 @@ type partialCacheKey struct {
 | 
			
		||||
	variant interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (k partialCacheKey) templateName() string {
 | 
			
		||||
	if !strings.HasPrefix(k.name, "partials/") {
 | 
			
		||||
		return "partials/" + k.name
 | 
			
		||||
	}
 | 
			
		||||
	return k.name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// partialCache represents a cache of partials protected by a mutex.
 | 
			
		||||
type partialCache struct {
 | 
			
		||||
	sync.RWMutex
 | 
			
		||||
@@ -211,6 +219,7 @@ func createKey(name string, variants ...interface{}) (partialCacheKey, error) {
 | 
			
		||||
var errUnHashable = errors.New("unhashable")
 | 
			
		||||
 | 
			
		||||
func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (result interface{}, err error) {
 | 
			
		||||
	start := time.Now()
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if r := recover(); r != nil {
 | 
			
		||||
			err = r.(error)
 | 
			
		||||
@@ -226,9 +235,20 @@ func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (resu
 | 
			
		||||
	ns.cachedPartials.RUnlock()
 | 
			
		||||
 | 
			
		||||
	if ok {
 | 
			
		||||
		if ns.deps.Metrics != nil {
 | 
			
		||||
			ns.deps.Metrics.TrackValue(key.templateName(), p, true)
 | 
			
		||||
			// The templates that gets executed is measued in Execute.
 | 
			
		||||
			// We need to track the time spent in the cache to
 | 
			
		||||
			// get the totals correct.
 | 
			
		||||
			ns.deps.Metrics.MeasureSince(key.templateName(), start)
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		return p, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ns.cachedPartials.Lock()
 | 
			
		||||
	defer ns.cachedPartials.Unlock()
 | 
			
		||||
 | 
			
		||||
	var name string
 | 
			
		||||
	name, p, err = ns.include(key.name, context)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -236,14 +256,7 @@ func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (resu
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ns.deps.Metrics != nil {
 | 
			
		||||
		ns.deps.Metrics.TrackValue(name, p, true)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ns.cachedPartials.Lock()
 | 
			
		||||
	defer ns.cachedPartials.Unlock()
 | 
			
		||||
	// Double-check.
 | 
			
		||||
	if p2, ok := ns.cachedPartials.p[key]; ok {
 | 
			
		||||
		return p2, nil
 | 
			
		||||
		ns.deps.Metrics.TrackValue(name, p, false)
 | 
			
		||||
	}
 | 
			
		||||
	ns.cachedPartials.p[key] = p
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user