From: Bjørn Erik Pedersen Date: Fri, 13 Feb 2026 08:53:10 +0000 (+0100) Subject: tpl/collections: Add some more benchmarks for where and sort X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=2e5132d8d20fcb7b0c545ddf7c18161837a7ef56;p=brevno-suite%2Fhugo tpl/collections: Add some more benchmarks for where and sort --- diff --git a/tpl/collections/collections_integration_test.go b/tpl/collections/collections_integration_test.go index a1a148524..ee003e049 100644 --- a/tpl/collections/collections_integration_test.go +++ b/tpl/collections/collections_integration_test.go @@ -14,9 +14,13 @@ package collections_test import ( + "context" + "fmt" "testing" "github.com/gohugoio/hugo/hugolib" + "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/tpl/collections" ) // Issue 9585 @@ -555,3 +559,58 @@ tags_weight: 20 "intersect:[]string:[a b c d]", ) } + +func BenchmarkWhereAndSortPages(b *testing.B) { + pageTemplate := ` +-- content/page%04d.md -- +--- +title: Page%04d +--- +` + + files := ` +-- hugo.toml -- +-- layouts/all.html -- +All. +` + + for i := 0; i < 500; i++ { + files += fmt.Sprintf(pageTemplate, i, i) + } + + bb := hugolib.Test(b, files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) { + conf.BuildCfg = hugolib.BuildCfg{ + SkipRender: true, + } + })) + + s := bb.H.Sites[0] + seq := s.RegularPages() + ns := s.TemplateStore.GetTemplateFuncsNamespace("collections").(*collections.Namespace) + + b.Run("Where", func(b *testing.B) { + for b.Loop() { + v, err := ns.Where(context.Background(), seq, "Title", "ge", "Page0480") + if err != nil { + b.Fatal(err) + } + res := v.(page.Pages) + if len(res) != 20 { + b.Fatalf("Where didn't return an expected result, got %d", len(res)) + } + } + }) + + b.Run("Sort", func(b *testing.B) { + for b.Loop() { + v, err := ns.Sort(context.Background(), s.RegularPages(), "Title", "desc") + if err != nil { + b.Fatal(err) + } + res := v.(page.Pages) + if len(res) != 500 { + b.Fatalf("Sort didn't return an expected result, got %d", len(res)) + } + } + }) +} diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index 2caf1e830..1822e9435 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -906,18 +906,42 @@ func BenchmarkWhereOps(b *testing.B) { func BenchmarkWhereMap(b *testing.B) { ns := newNs() - seq := map[string]string{} + seqString := map[string]string{} + seqAny := map[string]any{} + seqInt := map[string]int{} for i := range 1000 { - seq[fmt.Sprintf("key%d", i)] = "value" + seqString[fmt.Sprintf("key%d", i)] = "value" + seqAny[fmt.Sprintf("key%d", i)] = "value" + seqInt[fmt.Sprintf("key%d", i)] = i } - for b.Loop() { - _, err := ns.Where(context.Background(), seq, "key", "eq", "value") - if err != nil { - b.Fatal(err) + b.Run("String", func(b *testing.B) { + for b.Loop() { + _, err := ns.Where(context.Background(), seqString, "key", "eq", "value") + if err != nil { + b.Fatal(err) + } } - } + }) + + b.Run("Int", func(b *testing.B) { + for b.Loop() { + _, err := ns.Where(context.Background(), seqAny, "key", "eq", 42) + if err != nil { + b.Fatal(err) + } + } + }) + + b.Run("Any", func(b *testing.B) { + for b.Loop() { + _, err := ns.Where(context.Background(), seqAny, "key", "eq", "value") + if err != nil { + b.Fatal(err) + } + } + }) } func BenchmarkWhereSliceOfStructPointersWithMethod(b *testing.B) { diff --git a/tpl/tplimpl/templatestore.go b/tpl/tplimpl/templatestore.go index 944a0953f..1d4e537fd 100644 --- a/tpl/tplimpl/templatestore.go +++ b/tpl/tplimpl/templatestore.go @@ -810,6 +810,16 @@ func (s TemplateStore) WithSiteOpts(opts SiteOptions) *TemplateStore { return &s } +// GetTemplateFuncsNamespace returns the given template funcs namespace. +// Used in tests only. +func (s *TemplateStore) GetTemplateFuncsNamespace(ns string) any { + v, err := s.storeSite.opts.TemplateFuncs[ns].(func(cctx context.Context, args ...any) (any, error))(context.Background()) + if err != nil { + panic(fmt.Sprintf("template func namespace %q not found: %s", ns, err)) + } + return v +} + func (s *TemplateStore) findBestMatchGet(key string, category Category, consider func(candidate *TemplInfo) bool, d1 TemplateDescriptor, dims1 sitesmatrix.VectorProvider, best *bestMatch, ) {