diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-07-30 12:52:54 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-07-31 16:44:06 +0200 |
commit | d5eda13cb2e57998210b66e080dc96e95b38e5f0 (patch) | |
tree | b52bc254cf9d6dcb768a491d73c725d7104dd9cd /tpl | |
parent | 8b5d796989cf0798ee61003159ba8b332675bdf2 (diff) | |
download | hugo-d5eda13cb2e57998210b66e080dc96e95b38e5f0.tar.gz hugo-d5eda13cb2e57998210b66e080dc96e95b38e5f0.zip |
Replace the MD5 hashing of images with xxHash
Note that we only use this for change detection.
The previous implementation invoked `MD5FromReaderFast` that created a MD5 has from 8 64 bytes chunks in the file, which is obviously very fast. The new implementation creates the hash from the entire file and ... seems to be even more effective:
```
name old time/op new time/op delta
HashImage-10 9.45µs ±21% 10.89µs ± 1% ~ (p=0.343 n=4+4)
name old alloc/op new alloc/op delta
HashImage-10 144B ± 0% 8B ± 0% -94.44% (p=0.029 n=4+4)
name old allocs/op new allocs/op delta
HashImage-10 4.00 ± 0% 1.00 ± 0% -75.00% (p=0.029 n=4+4)
```
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/hash/hash.go | 12 | ||||
-rw-r--r-- | tpl/tplimpl/template_ast_transformers.go | 4 |
2 files changed, 4 insertions, 12 deletions
diff --git a/tpl/hash/hash.go b/tpl/hash/hash.go index d4a80b342..00df4e3cd 100644 --- a/tpl/hash/hash.go +++ b/tpl/hash/hash.go @@ -16,10 +16,9 @@ package hash import ( "context" - "encoding/hex" "hash/fnv" - "github.com/cespare/xxhash/v2" + "github.com/gohugoio/hugo/common/hashing" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/tpl/internal" "github.com/spf13/cast" @@ -51,14 +50,7 @@ func (ns *Namespace) XxHash(v any) (string, error) { return "", err } - hasher := xxhash.New() - - _, err = hasher.WriteString(conv) - if err != nil { - return "", err - } - hash := hasher.Sum(nil) - return hex.EncodeToString(hash), nil + return hashing.XxHashFromStringHexEncoded(conv), nil } const name = "hash" diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go index 9eee64235..f95335779 100644 --- a/tpl/tplimpl/template_ast_transformers.go +++ b/tpl/tplimpl/template_ast_transformers.go @@ -18,12 +18,12 @@ import ( "fmt" "strings" - "github.com/gohugoio/hugo/helpers" htmltemplate "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" + "github.com/gohugoio/hugo/common/hashing" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/tpl" "github.com/mitchellh/mapstructure" @@ -254,7 +254,7 @@ func (c *templateContext) handleDefer(withNode *parse.WithNode) { c.err = errors.New("resources.PostProcess cannot be used in a deferred template") return } - innerHash := helpers.XxHashString(s) + innerHash := hashing.XxHashFromStringHexEncoded(s) deferredID := tpl.HugoDeferredTemplatePrefix + innerHash c.deferNodes[deferredID] = inner |