Skip to content

Commit fbb78b8

Browse files
committed
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
1 parent 6c56028 commit fbb78b8

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

‎hugolib/page_collections.go‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections {
5151
return &PageCollections{rawAllPages: pages}
5252
}
5353

54-
func (c *PageCollections) getPage(typ string, path ...string) *Page {
55-
pages := c.findPagesByKindIn(typ, c.Pages)
54+
func (c *PageCollections) getFirstPageMatchIn(ps Pages, typ string, path ...string) *Page {
55+
pages := c.findPagesByKindIn(typ, ps)
5656

5757
if len(pages) == 0 {
5858
return nil
@@ -78,6 +78,20 @@ func (c *PageCollections) getPage(typ string, path ...string) *Page {
7878
}
7979

8080
return nil
81+
82+
}
83+
84+
func (c *PageCollections) getPage(typ string, path ...string) *Page {
85+
var pages Pages
86+
87+
if typ == KindPage {
88+
pages = c.RegularPages
89+
} else {
90+
pages = c.indexPages
91+
}
92+
93+
return c.getFirstPageMatchIn(pages, typ, path...)
94+
8195
}
8296

8397
func (*PageCollections) findPagesByKindIn(kind string, inPages Pages) Pages {

‎hugolib/site.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,8 +1906,11 @@ func (s *Site) Stats() {
19061906
// This will return nil when no page could be found.
19071907
//
19081908
// The valid page types are: home, section, taxonomy and taxonomyTerm
1909-
func (s *SiteInfo) GetPage(typ string, path ...string) *Page {
1910-
return s.getPage(typ, path...)
1909+
func (s *SiteInfo) GetPage(typ string, path ...string) (*Page, error) {
1910+
if typ == KindPage {
1911+
return nil, errors.New("GetPage not supported for regular pages")
1912+
}
1913+
return s.getPage(typ, path...), nil
19111914
}
19121915

19131916
func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) {

0 commit comments

Comments
 (0)