all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
// Copyright 2024 The Hugo Authors. All rights reserved.
|
2019-01-02 06:33:26 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package page
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-30 01:55:24 -04:00
|
|
|
"regexp"
|
2023-01-04 12:24:36 -05:00
|
|
|
"strings"
|
2019-01-02 06:33:26 -05:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2019-01-02 06:33:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// testdataPermalinks is used by a couple of tests; the expandsTo content is
|
|
|
|
// subject to the data in simplePageJSON.
|
|
|
|
var testdataPermalinks = []struct {
|
|
|
|
spec string
|
|
|
|
valid bool
|
|
|
|
expandsTo string
|
|
|
|
}{
|
|
|
|
{":title", true, "spf13-vim-3.0-release-and-new-website"},
|
|
|
|
{"/:year-:month-:title", true, "/2012-04-spf13-vim-3.0-release-and-new-website"},
|
|
|
|
{"/:year/:yearday/:month/:monthname/:day/:weekday/:weekdayname/", true, "/2012/97/04/April/06/5/Friday/"}, // Dates
|
2019-11-10 00:58:38 -05:00
|
|
|
{"/:section/", true, "/blue/"}, // Section
|
|
|
|
{"/:title/", true, "/spf13-vim-3.0-release-and-new-website/"}, // Title
|
|
|
|
{"/:slug/", true, "/the-slug/"}, // Slug
|
2022-06-13 08:22:58 -04:00
|
|
|
{"/:slugorfilename/", true, "/the-slug/"}, // Slug or filename
|
2019-11-10 00:58:38 -05:00
|
|
|
{"/:filename/", true, "/test-page/"}, // Filename
|
|
|
|
{"/:06-:1-:2-:Monday", true, "/12-4-6-Friday"}, // Dates with Go formatting
|
|
|
|
{"/:2006_01_02_15_04_05.000", true, "/2012_04_06_03_01_59.000"}, // Complicated custom date format
|
2021-03-30 01:55:24 -04:00
|
|
|
{"/:sections/", true, "/a/b/c/"}, // Sections
|
|
|
|
{"/:sections[last]/", true, "/c/"}, // Sections
|
2023-01-17 05:00:38 -05:00
|
|
|
{"/:sections[0]/:sections[last]/", true, "/a/c/"}, // Sections
|
2024-10-14 06:08:31 -04:00
|
|
|
{"/\\:filename", true, "/:filename"}, // Escape sequence
|
|
|
|
{"/special\\::slug/", true, "/special:the-slug/"}, // Escape sequence
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
// Failures
|
|
|
|
{"/blog/:fred", false, ""},
|
|
|
|
{"/:year//:title", false, ""},
|
2019-11-10 00:58:38 -05:00
|
|
|
{"/:TITLE", false, ""}, // case is not normalized
|
|
|
|
{"/:2017", false, ""}, // invalid date format
|
|
|
|
{"/:2006-01-02", false, ""}, // valid date format but invalid attribute name
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func urlize(uri string) string {
|
|
|
|
// This is just an approximation of the real urlize function.
|
|
|
|
return strings.ToLower(strings.ReplaceAll(uri, " ", "-"))
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func TestPermalinkExpansion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
page := newTestPageWithFile("/test-page/index.md")
|
|
|
|
page.title = "Spf13 Vim 3.0 Release and new website"
|
2019-11-10 00:58:38 -05:00
|
|
|
d, _ := time.Parse("2006-01-02 15:04:05", "2012-04-06 03:01:59")
|
2019-01-02 06:33:26 -05:00
|
|
|
page.date = d
|
|
|
|
page.section = "blue"
|
|
|
|
page.slug = "The Slug"
|
2023-06-26 09:31:01 -04:00
|
|
|
page.kind = "page"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
for _, item := range testdataPermalinks {
|
2019-01-02 06:33:26 -05:00
|
|
|
if !item.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-30 01:55:24 -04:00
|
|
|
specNameCleaner := regexp.MustCompile(`[\:\/\[\]]`)
|
|
|
|
name := specNameCleaner.ReplaceAllString(item.spec, "")
|
|
|
|
|
|
|
|
c.Run(name, func(c *qt.C) {
|
2023-06-26 09:31:01 -04:00
|
|
|
patterns := map[string]map[string]string{
|
|
|
|
"page": {
|
|
|
|
"posts": item.spec,
|
|
|
|
},
|
2021-03-30 01:55:24 -04:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
expander, err := NewPermalinkExpander(urlize, patterns)
|
2021-03-30 01:55:24 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
expanded, err := expander.Expand("posts", page)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, item.expandsTo)
|
2024-10-15 03:13:26 -04:00
|
|
|
|
|
|
|
expanded, err = expander.ExpandPattern(item.spec, page)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, item.expandsTo)
|
2021-03-30 01:55:24 -04:00
|
|
|
})
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermalinkExpansionMultiSection(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
page := newTestPage()
|
|
|
|
page.title = "Page Title"
|
|
|
|
d, _ := time.Parse("2006-01-02", "2012-04-06")
|
|
|
|
page.date = d
|
|
|
|
page.section = "blue"
|
|
|
|
page.slug = "The Slug"
|
2023-06-26 09:31:01 -04:00
|
|
|
page.kind = "page"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2022-06-13 08:22:58 -04:00
|
|
|
page_slug_fallback := newTestPageWithFile("/page-filename/index.md")
|
|
|
|
page_slug_fallback.title = "Page Title"
|
2023-06-26 09:31:01 -04:00
|
|
|
page_slug_fallback.kind = "page"
|
|
|
|
|
|
|
|
permalinksConfig := map[string]map[string]string{
|
|
|
|
"page": {
|
|
|
|
"posts": "/:slug",
|
|
|
|
"blog": "/:section/:year",
|
|
|
|
"recipes": "/:slugorfilename",
|
2024-10-14 06:08:31 -04:00
|
|
|
"special": "/special\\::slug",
|
2023-06-26 09:31:01 -04:00
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
expanded, err := expander.Expand("posts", page)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, "/the-slug")
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
expanded, err = expander.Expand("blog", page)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, "/blue/2012")
|
2022-06-13 08:22:58 -04:00
|
|
|
|
|
|
|
expanded, err = expander.Expand("posts", page_slug_fallback)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, "/page-title")
|
|
|
|
|
|
|
|
expanded, err = expander.Expand("recipes", page_slug_fallback)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, "/page-filename")
|
2024-10-14 06:08:31 -04:00
|
|
|
|
|
|
|
expanded, err = expander.Expand("special", page)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, "/special:the-slug")
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermalinkExpansionConcurrent(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-06-26 09:31:01 -04:00
|
|
|
permalinksConfig := map[string]map[string]string{
|
|
|
|
"page": {
|
|
|
|
"posts": "/:slug/",
|
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 1; i < 20; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
page := newTestPage()
|
2023-06-26 09:31:01 -04:00
|
|
|
page.kind = "page"
|
2019-01-02 06:33:26 -05:00
|
|
|
for j := 1; j < 20; j++ {
|
|
|
|
page.slug = fmt.Sprintf("slug%d", i+j)
|
|
|
|
expanded, err := expander.Expand("posts", page)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(expanded, qt.Equals, fmt.Sprintf("/%s/", page.slug))
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-03-30 01:55:24 -04:00
|
|
|
func TestPermalinkExpansionSliceSyntax(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
exp, err := NewPermalinkExpander(urlize, nil)
|
|
|
|
c.Assert(err, qt.IsNil)
|
2023-06-28 04:27:39 -04:00
|
|
|
slice4 := []string{"a", "b", "c", "d"}
|
|
|
|
fn4 := func(s string) []string {
|
|
|
|
return exp.toSliceFunc(s)(slice4)
|
|
|
|
}
|
|
|
|
|
|
|
|
slice1 := []string{"a"}
|
|
|
|
fn1 := func(s string) []string {
|
|
|
|
return exp.toSliceFunc(s)(slice1)
|
2021-03-30 01:55:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Run("Basic", func(c *qt.C) {
|
2023-06-28 04:27:39 -04:00
|
|
|
c.Assert(fn4("[1:3]"), qt.DeepEquals, []string{"b", "c"})
|
|
|
|
c.Assert(fn4("[1:]"), qt.DeepEquals, []string{"b", "c", "d"})
|
|
|
|
c.Assert(fn4("[:2]"), qt.DeepEquals, []string{"a", "b"})
|
|
|
|
c.Assert(fn4("[0:2]"), qt.DeepEquals, []string{"a", "b"})
|
|
|
|
c.Assert(fn4("[:]"), qt.DeepEquals, []string{"a", "b", "c", "d"})
|
|
|
|
c.Assert(fn4(""), qt.DeepEquals, []string{"a", "b", "c", "d"})
|
|
|
|
c.Assert(fn4("[last]"), qt.DeepEquals, []string{"d"})
|
|
|
|
c.Assert(fn4("[:last]"), qt.DeepEquals, []string{"a", "b", "c"})
|
|
|
|
c.Assert(fn1("[last]"), qt.DeepEquals, []string{"a"})
|
|
|
|
c.Assert(fn1("[:last]"), qt.DeepEquals, []string{})
|
|
|
|
c.Assert(fn1("[1:last]"), qt.DeepEquals, []string{})
|
|
|
|
c.Assert(fn1("[1]"), qt.DeepEquals, []string{})
|
2021-03-30 01:55:24 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Out of bounds", func(c *qt.C) {
|
2023-06-28 04:27:39 -04:00
|
|
|
c.Assert(fn4("[1:5]"), qt.DeepEquals, []string{"b", "c", "d"})
|
|
|
|
c.Assert(fn4("[-1:5]"), qt.DeepEquals, []string{"a", "b", "c", "d"})
|
|
|
|
c.Assert(fn4("[5:]"), qt.DeepEquals, []string{})
|
|
|
|
c.Assert(fn4("[5:]"), qt.DeepEquals, []string{})
|
|
|
|
c.Assert(fn4("[5:32]"), qt.DeepEquals, []string{})
|
2021-03-30 01:55:24 -04:00
|
|
|
c.Assert(exp.toSliceFunc("[:1]")(nil), qt.DeepEquals, []string(nil))
|
|
|
|
c.Assert(exp.toSliceFunc("[:1]")([]string{}), qt.DeepEquals, []string(nil))
|
|
|
|
|
|
|
|
// These all return nil
|
2023-06-28 04:27:39 -04:00
|
|
|
c.Assert(fn4("[]"), qt.IsNil)
|
|
|
|
c.Assert(fn4("[1:}"), qt.IsNil)
|
|
|
|
c.Assert(fn4("foo"), qt.IsNil)
|
2021-03-30 01:55:24 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func BenchmarkPermalinkExpand(b *testing.B) {
|
|
|
|
page := newTestPage()
|
|
|
|
page.title = "Hugo Rocks"
|
|
|
|
d, _ := time.Parse("2006-01-02", "2019-02-28")
|
|
|
|
page.date = d
|
2023-06-26 09:31:01 -04:00
|
|
|
page.kind = "page"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2023-06-26 09:31:01 -04:00
|
|
|
permalinksConfig := map[string]map[string]string{
|
|
|
|
"page": {
|
|
|
|
"posts": "/:year-:month-:title",
|
|
|
|
},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
|
2019-01-02 06:33:26 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s, err := expander.Expand("posts", page)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if s != "/2019-02-hugo-rocks" {
|
|
|
|
b.Fatal(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|