diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-23 01:20:31 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-23 15:18:26 +0300 |
commit | fbb78b89df8ccef8f0ab26af00aa45d35c1ee2cf (patch) | |
tree | 67fb115f9c9c1f6b528552cf45d5c9494e4826f3 /hugolib | |
parent | 6c560288a6329305ae3327a589e072e56bc9970d (diff) |
hugolib: Speed up GetPage
When we know to look into the index pages collection, do that:
```
benchmark old ns/op new ns/op delta
BenchmarkGetPage-4 51483 7072 -86.26%
benchmark old allocs new allocs delta
BenchmarkGetPage-4 71 71 +0.00%
benchmark old bytes new bytes delta
BenchmarkGetPage-4 2648 2648 +0.00%
```
This commit also returns an error if .Site.GetPage is called with the regular Page Kind, as that is currently not supported.
Fixes #3503
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/page_collections.go | 18 | ||||
-rw-r--r-- | hugolib/site.go | 7 |
2 files changed, 21 insertions, 4 deletions
diff --git a/hugolib/page_collections.go b/hugolib/page_collections.go index 3bf313282..0cd432f48 100644 --- a/hugolib/page_collections.go +++ b/hugolib/page_collections.go @@ -51,8 +51,8 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections { return &PageCollections{rawAllPages: pages} } -func (c *PageCollections) getPage(typ string, path ...string) *Page { - pages := c.findPagesByKindIn(typ, c.Pages) +func (c *PageCollections) getFirstPageMatchIn(ps Pages, typ string, path ...string) *Page { + pages := c.findPagesByKindIn(typ, ps) if len(pages) == 0 { return nil @@ -78,6 +78,20 @@ func (c *PageCollections) getPage(typ string, path ...string) *Page { } return nil + +} + +func (c *PageCollections) getPage(typ string, path ...string) *Page { + var pages Pages + + if typ == KindPage { + pages = c.RegularPages + } else { + pages = c.indexPages + } + + return c.getFirstPageMatchIn(pages, typ, path...) + } func (*PageCollections) findPagesByKindIn(kind string, inPages Pages) Pages { diff --git a/hugolib/site.go b/hugolib/site.go index 45acd54cf..169c6cea4 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1906,8 +1906,11 @@ func (s *Site) Stats() { // This will return nil when no page could be found. // // The valid page types are: home, section, taxonomy and taxonomyTerm -func (s *SiteInfo) GetPage(typ string, path ...string) *Page { - return s.getPage(typ, path...) +func (s *SiteInfo) GetPage(typ string, path ...string) (*Page, error) { + if typ == KindPage { + return nil, errors.New("GetPage not supported for regular pages") + } + return s.getPage(typ, path...), nil } func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) { |