Skip to content

Commit

Permalink
tpl: Use xxHash instead of MD5 to hash the deferred templates
Browse files Browse the repository at this point in the history
Motivation is performance. These templates are typically very small, so the win is minor, I guess.
  • Loading branch information
bep committed Jul 17, 2024
1 parent f0ed91c commit 4d8bfa7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"unicode"
"unicode/utf8"

"github.com/cespare/xxhash/v2"
"github.com/spf13/afero"

"github.com/jdkato/prose/transform"
Expand Down Expand Up @@ -257,6 +258,14 @@ func SliceToLower(s []string) []string {
return l
}

// XxHashString takes a string and returns its xxHash hash.
func XxHashString(f string) string {
h := xxhash.New()
h.WriteString(f)
hash := h.Sum(nil)
return hex.EncodeToString(hash)
}

// MD5String takes a string and returns its MD5 hash.
func MD5String(f string) string {
h := md5.New()
Expand Down
2 changes: 1 addition & 1 deletion tpl/tplimpl/template_ast_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.MD5String(s)
innerHash := helpers.XxHashString(s)
deferredID := tpl.HugoDeferredTemplatePrefix + innerHash

c.deferNodes[deferredID] = inner
Expand Down

0 comments on commit 4d8bfa7

Please sign in to comment.