Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up GetTerms #12611

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type pageMap struct {
cachePages1 *dynacache.Partition[string, page.Pages]
cachePages2 *dynacache.Partition[string, page.Pages]
cacheResources *dynacache.Partition[string, resource.Resources]
cacheGetTerms *dynacache.Partition[string, map[string]page.Pages]
cacheContentRendered *dynacache.Partition[string, *resources.StaleValue[contentSummary]]
cacheContentPlain *dynacache.Partition[string, *resources.StaleValue[contentPlainPlainWords]]
contentTableOfContents *dynacache.Partition[string, *resources.StaleValue[contentTableOfContents]]
Expand Down Expand Up @@ -448,32 +449,32 @@ func (m *pageMap) getPagesWithTerm(q pageMapQueryPagesBelowPath) page.Pages {
func (m *pageMap) getTermsForPageInTaxonomy(path, taxonomy string) page.Pages {
prefix := paths.AddLeadingSlash(taxonomy)

v, err := m.cachePages1.GetOrCreate(prefix+path, func(string) (page.Pages, error) {
var pas page.Pages

termPages, err := m.cacheGetTerms.GetOrCreate(prefix, func(string) (map[string]page.Pages, error) {
mm := make(map[string]page.Pages)
err := m.treeTaxonomyEntries.WalkPrefix(
doctree.LockTypeNone,
paths.AddTrailingSlash(prefix),
func(s string, n *weightedContentNode) (bool, error) {
if strings.HasSuffix(s, path) {
pas = append(pas, n.term)
}
mm[n.n.Path()] = append(mm[n.n.Path()], n.term)
return false, nil
},
)
if err != nil {
return nil, err
}

page.SortByDefault(pas)
// Sort the terms.
for _, v := range mm {
page.SortByDefault(v)
}

return pas, nil
return mm, nil
})
if err != nil {
panic(err)
}

return v
return termPages[path]
}

func (m *pageMap) forEachResourceInPage(
Expand Down Expand Up @@ -898,6 +899,7 @@ func newPageMap(i int, s *Site, mcache *dynacache.Cache, pageTrees *pageTrees) *
pageTrees: pageTrees.Shape(0, i),
cachePages1: dynacache.GetOrCreatePartition[string, page.Pages](mcache, fmt.Sprintf("/pag1/%d", i), dynacache.OptionsPartition{Weight: 10, ClearWhen: dynacache.ClearOnRebuild}),
cachePages2: dynacache.GetOrCreatePartition[string, page.Pages](mcache, fmt.Sprintf("/pag2/%d", i), dynacache.OptionsPartition{Weight: 10, ClearWhen: dynacache.ClearOnRebuild}),
cacheGetTerms: dynacache.GetOrCreatePartition[string, map[string]page.Pages](mcache, fmt.Sprintf("/gett/%d", i), dynacache.OptionsPartition{Weight: 5, ClearWhen: dynacache.ClearOnRebuild}),
cacheResources: dynacache.GetOrCreatePartition[string, resource.Resources](mcache, fmt.Sprintf("/ress/%d", i), dynacache.OptionsPartition{Weight: 60, ClearWhen: dynacache.ClearOnRebuild}),
cacheContentRendered: dynacache.GetOrCreatePartition[string, *resources.StaleValue[contentSummary]](mcache, fmt.Sprintf("/cont/ren/%d", i), dynacache.OptionsPartition{Weight: 70, ClearWhen: dynacache.ClearOnChange}),
cacheContentPlain: dynacache.GetOrCreatePartition[string, *resources.StaleValue[contentPlainPlainWords]](mcache, fmt.Sprintf("/cont/pla/%d", i), dynacache.OptionsPartition{Weight: 70, ClearWhen: dynacache.ClearOnChange}),
Expand Down
55 changes: 55 additions & 0 deletions hugolib/taxonomy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,58 @@ title: p1
b.AssertFileExists("public/ja/s1/index.html", false) // failing test
b.AssertFileExists("public/ja/s1/category/index.html", true)
}

func BenchmarkTaxonomiesGetTerms(b *testing.B) {
createBuilders := func(b *testing.B, numPages int) []*IntegrationTestBuilder {
files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ["RSS", "sitemap", "section"]
[taxononomies]
tag = "tags"
-- layouts/_default/list.html --
List.
-- layouts/_default/single.html --
GetTerms.tags: {{ range .GetTerms "tags" }}{{ .Title }}|{{ end }}
-- content/_index.md --
`

tagsVariants := []string{
"tags: ['a']",
"tags: ['a', 'b']",
"tags: ['a', 'b', 'c']",
"tags: ['a', 'b', 'c', 'd']",
"tags: ['a', 'b', 'd', 'e']",
"tags: ['a', 'b', 'c', 'd', 'e']",
"tags: ['a', 'd']",
"tags: ['a', 'f']",
}

for i := 1; i < numPages; i++ {
tags := tagsVariants[i%len(tagsVariants)]
files += fmt.Sprintf("\n-- content/posts/p%d.md --\n---\n%s\n---", i+1, tags)
}
cfg := IntegrationTestConfig{
T: b,
TxtarString: files,
}
builders := make([]*IntegrationTestBuilder, b.N)

for i := range builders {
builders[i] = NewIntegrationTestBuilder(cfg)
}

b.ResetTimer()

return builders
}

for _, numPages := range []int{100, 1000, 10000, 20000} {
b.Run(fmt.Sprintf("pages_%d", numPages), func(b *testing.B) {
builders := createBuilders(b, numPages)
for i := 0; i < b.N; i++ {
builders[i].Build()
}
})
}
}
Loading