From 3663828f5eefc5cda8ebc0d3077d3c3fb90066c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 12 Jul 2015 11:28:19 +0200 Subject: [PATCH] Optimize RuneCount Do not create it unless used. See #1266 --- hugolib/page.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hugolib/page.go b/hugolib/page.go index 1fa010abe..96de47154 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -68,6 +68,7 @@ type Page struct { plainWords []string plainRuneCount int plainInit sync.Once + plainSecondaryInit sync.Once renderingConfig *helpers.Blackfriday renderingConfigInit sync.Once PageMeta @@ -111,7 +112,7 @@ func (p *Page) PlainWords() []string { // RuneCount returns the rune count, excluding any whitespace, of the plain content. func (p *Page) RuneCount() int { - p.initPlain() + p.initPlainSecondary() return p.plainRuneCount } @@ -119,6 +120,13 @@ func (p *Page) initPlain() { p.plainInit.Do(func() { p.plain = helpers.StripHTML(string(p.Content)) p.plainWords = strings.Fields(p.plain) + return + }) +} + +func (p *Page) initPlainSecondary() { + p.plainSecondaryInit.Do(func() { + p.initPlain() runeCount := 0 for _, r := range p.plain { if !helpers.IsWhitespace(r) {