summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-09-10 11:26:34 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-18 09:49:42 +0100
commiteada236f87d9669885da1ff647672bb3dc6b4954 (patch)
treea0303f067b2cbe06c55637013dbd7702a551c64f
parente5329f13c02b87f0c30f8837759c810cd90ff8da (diff)
Introduce a tree map for all content
This commit introduces a new data structure to store pages and their resources. This data structure is backed by radix trees. This simplies tree operations, makes all pages a bundle, and paves the way for #6310. It also solves a set of annoying issues (see list below). Not a motivation behind this, but this commit also makes Hugo in general a little bit faster and more memory effective (see benchmarks). Especially for partial rebuilds on content edits, but also when taxonomies is in use. ``` name old time/op new time/op delta SiteNew/Bundle_with_image/Edit-16 1.32ms ± 8% 1.00ms ± 9% -24.42% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 1.28ms ± 0% 0.94ms ± 0% -26.26% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 33.9ms ± 2% 21.8ms ± 1% -35.67% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 40.6ms ± 1% 37.7ms ± 3% -7.20% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 56.7ms ± 0% 51.7ms ± 1% -8.82% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 19.9ms ± 2% 18.3ms ± 3% -7.64% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 37.9ms ± 4% 34.0ms ± 2% -10.28% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 10.7ms ± 0% 10.6ms ± 0% -1.15% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 10.8ms ± 0% 10.7ms ± 0% -1.05% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 43.2ms ± 1% 39.6ms ± 1% -8.35% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 47.6ms ± 1% 47.3ms ± 0% ~ (p=0.057 n=4+4) SiteNew/Deep_content_tree-16 73.0ms ± 1% 74.2ms ± 1% ~ (p=0.114 n=4+4) SiteNew/Many_HTML_templates-16 37.9ms ± 0% 38.1ms ± 1% ~ (p=0.114 n=4+4) SiteNew/Page_collections-16 53.6ms ± 1% 54.7ms ± 1% +2.09% (p=0.029 n=4+4) name old alloc/op new alloc/op delta SiteNew/Bundle_with_image/Edit-16 486kB ± 0% 430kB ± 0% -11.47% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 265kB ± 0% 209kB ± 0% -21.06% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 13.6MB ± 0% 8.8MB ± 0% -34.93% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 66.5MB ± 0% 63.9MB ± 0% -3.95% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 28.8MB ± 0% 25.8MB ± 0% -10.55% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 6.16MB ± 0% 5.56MB ± 0% -9.86% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 16.9MB ± 0% 16.0MB ± 0% -5.19% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 2.28MB ± 0% 2.29MB ± 0% +0.35% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 2.07MB ± 0% 2.07MB ± 0% ~ (p=0.114 n=4+4) SiteNew/Tags_and_categories-16 14.3MB ± 0% 13.2MB ± 0% -7.30% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 69.1MB ± 0% 69.0MB ± 0% ~ (p=0.343 n=4+4) SiteNew/Deep_content_tree-16 31.3MB ± 0% 31.8MB ± 0% +1.49% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 10.8MB ± 0% 10.9MB ± 0% +1.11% (p=0.029 n=4+4) SiteNew/Page_collections-16 21.4MB ± 0% 21.6MB ± 0% +1.15% (p=0.029 n=4+4) name old allocs/op new allocs/op delta SiteNew/Bundle_with_image/Edit-16 4.74k ± 0% 3.86k ± 0% -18.57% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 4.73k ± 0% 3.85k ± 0% -18.58% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 301k ± 0% 198k ± 0% -34.14% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 389k ± 0% 373k ± 0% -4.07% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 338k ± 0% 262k ± 0% -22.63% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 102k ± 0% 88k ± 0% -13.81% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 176k ± 0% 152k ± 0% -13.32% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 26.8k ± 0% 26.8k ± 0% +0.05% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 26.8k ± 0% 26.8k ± 0% +0.05% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 273k ± 0% 245k ± 0% -10.36% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 396k ± 0% 398k ± 0% +0.39% (p=0.029 n=4+4) SiteNew/Deep_content_tree-16 317k ± 0% 325k ± 0% +2.53% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 146k ± 0% 147k ± 0% +0.98% (p=0.029 n=4+4) SiteNew/Page_collections-16 210k ± 0% 215k ± 0% +2.44% (p=0.029 n=4+4) ``` Fixes #6312 Fixes #6087 Fixes #6738 Fixes #6412 Fixes #6743 Fixes #6875 Fixes #6034 Fixes #6902 Fixes #6173 Fixes #6590
-rw-r--r--commands/convert.go52
-rw-r--r--commands/import_jekyll.go8
-rw-r--r--common/herrors/errors.go5
-rw-r--r--common/para/para_test.go1
-rw-r--r--common/types/convert.go28
-rw-r--r--common/types/convert_test.go29
-rw-r--r--config/configProvider.go11
-rw-r--r--create/content_template_handler.go2
-rw-r--r--go.sum1
-rw-r--r--helpers/general.go30
-rw-r--r--helpers/path.go9
-rw-r--r--htesting/hqt/checkers.go42
-rw-r--r--htesting/test_helpers.go30
-rw-r--r--hugofs/decorators.go12
-rw-r--r--hugofs/fileinfo.go13
-rw-r--r--hugofs/files/classifier.go16
-rw-r--r--hugofs/filter_fs.go2
-rw-r--r--hugofs/rootmapping_fs.go2
-rw-r--r--hugolib/alias.go3
-rw-r--r--hugolib/cascade_test.go193
-rw-r--r--hugolib/content_map.go971
-rw-r--r--hugolib/content_map_page.go998
-rw-r--r--hugolib/content_map_test.go455
-rw-r--r--hugolib/disableKinds_test.go384
-rw-r--r--hugolib/filesystems/basefs.go1
-rw-r--r--hugolib/hugo_modules_test.go5
-rw-r--r--hugolib/hugo_sites.go198
-rw-r--r--hugolib/hugo_sites_build.go45
-rw-r--r--hugolib/hugo_sites_build_test.go11
-rw-r--r--hugolib/page.go162
-rw-r--r--hugolib/page__common.go30
-rw-r--r--hugolib/page__data.go31
-rw-r--r--hugolib/page__meta.go122
-rw-r--r--hugolib/page__new.go174
-rw-r--r--hugolib/page__output.go2
-rw-r--r--hugolib/page__paths.go23
-rw-r--r--hugolib/page__tree.go85
-rw-r--r--hugolib/page_kinds.go1
-rw-r--r--hugolib/page_test.go4
-rw-r--r--hugolib/pagebundler_test.go20
-rw-r--r--hugolib/pagecollections.go489
-rw-r--r--hugolib/pagecollections_test.go268
-rw-r--r--hugolib/pages_capture.go646
-rw-r--r--hugolib/pages_capture_test.go10
-rw-r--r--hugolib/pages_map.go474
-rw-r--r--hugolib/pages_process.go198
-rw-r--r--hugolib/site.go275
-rw-r--r--hugolib/site_benchmark_new_test.go23
-rw-r--r--hugolib/site_output.go25
-rw-r--r--hugolib/site_render.go85
-rw-r--r--hugolib/site_sections_test.go2
-rw-r--r--hugolib/site_test.go21
-rw-r--r--hugolib/taxonomy_test.go179
-rw-r--r--hugolib/template_test.go22
-rw-r--r--hugolib/testhelpers_test.go10
-rw-r--r--hugolib/translations.go16
-rw-r--r--parser/metadecoders/format.go18
-rw-r--r--parser/metadecoders/format_test.go18
-rw-r--r--parser/pageparser/pageparser.go56
-rw-r--r--parser/pageparser/pageparser_test.go19
-rw-r--r--resources/image_cache.go26
-rw-r--r--resources/image_test.go1
-rw-r--r--resources/page/page.go4
-rw-r--r--resources/page/page_marshaljson.autogen.go3
-rw-r--r--resources/page/page_nop.go4
-rw-r--r--resources/page/pagemeta/pagemeta.go50
-rw-r--r--resources/page/testhelpers_test.go4
-rw-r--r--resources/resource_spec.go11
-rw-r--r--tpl/compare/compare.go2
-rw-r--r--tpl/compare/compare_test.go11
-rw-r--r--tpl/transform/remarshal_test.go7
71 files changed, 4758 insertions, 2430 deletions
diff --git a/commands/convert.go b/commands/convert.go
index b9129e594..fe64405e9 100644
--- a/commands/convert.go
+++ b/commands/convert.go
@@ -16,10 +16,11 @@ package commands
import (
"bytes"
"fmt"
- "io"
"strings"
"time"
+ "github.com/gohugoio/hugo/parser/pageparser"
+
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/hugofs"
@@ -28,7 +29,6 @@ import (
"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders"
- "github.com/gohugoio/hugo/parser/pageparser"
"github.com/pkg/errors"
@@ -157,7 +157,7 @@ func (cc *convertCmd) convertAndSavePage(p page.Page, site *hugolib.Site, target
return nil
}
- pf, err := parseContentFile(file)
+ pf, err := pageparser.ParseFrontMatterAndContent(file)
if err != nil {
site.Log.ERROR.Println(errMsg)
file.Close()
@@ -167,23 +167,23 @@ func (cc *convertCmd) convertAndSavePage(p page.Page, site *hugolib.Site, target
file.Close()
// better handling of dates in formats that don't have support for them
- if pf.frontMatterFormat == metadecoders.JSON || pf.frontMatterFormat == metadecoders.YAML || pf.frontMatterFormat == metadecoders.TOML {
- for k, v := range pf.frontMatter {
+ if pf.FrontMatterFormat == metadecoders.JSON || pf.FrontMatterFormat == metadecoders.YAML || pf.FrontMatterFormat == metadecoders.TOML {
+ for k, v := range pf.FrontMatter {
switch vv := v.(type) {
case time.Time:
- pf.frontMatter[k] = vv.Format(time.RFC3339)
+ pf.FrontMatter[k] = vv.Format(time.RFC3339)
}
}
}
var newContent bytes.Buffer
- err = parser.InterfaceToFrontMatter(pf.frontMatter, targetFormat, &newContent)
+ err = parser.InterfaceToFrontMatter(pf.FrontMatter, targetFormat, &newContent)
if err != nil {
site.Log.ERROR.Println(errMsg)
return err
}
- newContent.Write(pf.content)
+ newContent.Write(pf.Content)
newFilename := p.File().Filename()
@@ -210,39 +210,3 @@ type parsedFile struct {
// Everything after Front Matter
content []byte
}
-
-func parseContentFile(r io.Reader) (parsedFile, error) {
- var pf parsedFile
-
- psr, err := pageparser.Parse(r, pageparser.Config{})
- if err != nil {
- return pf, err
- }
-
- iter := psr.Iterator()
-
- walkFn := func(item pageparser.Item) bool {
- if pf.frontMatterSource != nil {
- // The rest is content.
- pf.content = psr.Input()[item.Pos:]
- // Done
- return false
- } else if item.IsFrontMatter() {
- pf.frontMatterFormat = metadecoders.FormatFromFrontMatterType(item.Type)
- pf.frontMatterSource = item.Val
- }
- return true
-
- }
-
- iter.PeekWalk(walkFn)
-
- metadata, err := metadecoders.Default.UnmarshalToMap(pf.frontMatterSource, pf.frontMatterFormat)
- if err != nil {
- return pf, err
- }
- pf.frontMatter = metadata
-
- return pf, nil
-
-}
diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go
index d2e7b27da..2dd0fc051 100644
--- a/commands/import_jekyll.go
+++ b/commands/import_jekyll.go
@@ -26,6 +26,8 @@ import (
"time"
"unicode"
+ "github.com/gohugoio/hugo/parser/pageparser"
+
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/parser/metadecoders"
@@ -397,19 +399,19 @@ func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
return err
}
- pf, err := parseContentFile(bytes.NewReader(contentBytes))
+ pf, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(contentBytes))
if err != nil {
jww.ERROR.Println("Parse file error:", path)
return err
}
- newmetadata, err := convertJekyllMetaData(pf.frontMatter, postName, postDate, draft)
+ newmetadata, err := convertJekyllMetaData(pf.FrontMatter, postName, postDate, draft)
if err != nil {
jww.ERROR.Println("Convert metadata error:", path)
return err
}
- content, err := convertJekyllContent(newmetadata, string(pf.content))
+ content, err := convertJekyllContent(newmetadata, string(pf.Content))
if err != nil {
jww.ERROR.Println("Converting Jekyll error:", path)
return err
diff --git a/common/herrors/errors.go b/common/herrors/errors.go
index 5fae6fcae..fded30b1a 100644
--- a/common/herrors/errors.go
+++ b/common/herrors/errors.go
@@ -57,6 +57,11 @@ func PrintStackTrace(w io.Writer) {
fmt.Fprintf(w, "%s", buf)
}
+// ErrorSender is a, typically, non-blocking error handler.
+type ErrorSender interface {
+ SendError(err error)
+}
+
// Recover is a helper function that can be used to capture panics.
// Put this at the top of a method/function that crashes in a template:
// defer herrors.Recover()
diff --git a/common/para/para_test.go b/common/para/para_test.go
index bda7f5d27..9b268b0c0 100644
--- a/common/para/para_test.go
+++ b/common/para/para_test.go
@@ -16,6 +16,7 @@ package para
import (
"context"
"runtime"
+
"sort"
"sync"
"sync/atomic"
diff --git a/common/types/convert.go b/common/types/convert.go
new file mode 100644
index 000000000..b55330757
--- /dev/null
+++ b/common/types/convert.go
@@ -0,0 +1,28 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// 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 types
+
+import "github.com/spf13/cast"
+
+// ToStringSlicePreserveString converts v to a string slice.
+// If v is a string, it will be wrapped in a string slice.
+func ToStringSlicePreserveString(v interface{}) []string {
+ if v == nil {
+ return nil
+ }
+ if sds, ok := v.(string); ok {
+ return []string{sds}
+ }
+ return cast.ToStringSlice(v)
+}
diff --git a/common/types/convert_test.go b/common/types/convert_test.go
new file mode 100644
index 000000000..7f86f4c8a
--- /dev/null
+++ b/common/types/convert_test.go
@@ -0,0 +1,29 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// 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 types
+
+import (
+ "testing"
+
+ qt "github.com/frankban/quicktest"
+)
+
+func TestToStringSlicePreserveString(t *testing.T) {
+ c := qt.New(t)
+
+ c.Assert(ToStringSlicePreserveString("Hugo"), qt.DeepEquals, []string{"Hugo"})
+ c.Assert(ToStringSlicePreserveString([]interface{}{"A", "B"}), qt.DeepEquals, []string{"A", "B"})
+ c.Assert(ToStringSlicePreserveString(nil), qt.IsNil)
+
+}
diff --git a/config/configProvider.go b/config/configProvider.go
index 187fb7b10..928bf948a 100644
--- a/config/configProvider.go
+++ b/config/configProvider.go
@@ -14,7 +14,7 @@
package config
import (
- "github.com/spf13/cast"
+ "github.com/gohugoio/hugo/common/types"
)
// Provider provides the configuration settings for Hugo.
@@ -35,14 +35,7 @@ type Provider interface {
// we do not attempt to split it into fields.
func GetStringSlicePreserveString(cfg Provider, key string) []string {
sd := cfg.Get(key)
- return toStringSlicePreserveString(sd)
-}
-
-func toStringSlicePreserveString(v interface{}) []string {
- if sds, ok := v.(string); ok {
- return []string{sds}
- }
- return cast.ToStringSlice(v)
+ return types.ToStringSlicePreserveString(sd)
}
// SetBaseTestDefaults provides some common config defaults used in tests.
diff --git a/create/content_template_handler.go b/create/content_template_handler.go
index e4cddedf5..3a7007f1a 100644
--- a/create/content_template_handler.go
+++ b/create/content_template_handler.go
@@ -110,7 +110,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archety
Date: time.Now().Format(time.RFC3339),
Name: name,
File: f,
- Site: &s.Info,