diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-08-16 23:50:15 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-09-14 11:50:55 +0300 |
commit | bcd434794a28ff75a6e6504c6c3bada554ba88ce (patch) | |
tree | 7ebb624045cdd6236cfdfd0f3620bb5bb8bf88d2 /hugolib | |
parent | 74ffb45fbe2c121881b2386fc3210f8b1c6bd952 (diff) |
Avoid splitting words for summary
For people using autogenerated summaries, this is one of the hot spots in the memory department.
We don't need to split al the content into words to do proper summary truncation.
This is obviously more effective:
```
BenchmarkTestTruncateWordsToWholeSentence-4 300000 4720 ns/op 0 B/op 0 allocs/op
BenchmarkTestTruncateWordsToWholeSentenceOld-4 100000 17699 ns/op 3072 B/op 3 allocs/op
```
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/page.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/hugolib/page.go b/hugolib/page.go index bc54aac91..0784f5bf8 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -89,6 +89,7 @@ type Page struct { plain string // TODO should be []byte plainWords []string plainInit sync.Once + plainWordsInit sync.Once renderingConfig *helpers.Blackfriday renderingConfigInit sync.Once pageMenus PageMenus @@ -147,14 +148,20 @@ func (p *Page) Plain() string { } func (p *Page) PlainWords() []string { - p.initPlain() + p.initPlainWords() return p.plainWords } 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) initPlainWords() { + p.plainWordsInit.Do(func() { + p.plainWords = strings.Fields(p.Plain()) return }) } @@ -335,7 +342,7 @@ func (p *Page) setAutoSummary() error { if p.isCJKLanguage { summary, truncated = helpers.TruncateWordsByRune(p.PlainWords(), helpers.SummaryLength) } else { - summary, truncated = helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength) + summary, truncated = helpers.TruncateWordsToWholeSentence(p.Plain(), helpers.SummaryLength) } p.Summary = template.HTML(summary) p.Truncated = truncated @@ -479,6 +486,10 @@ func (p *Page) ReadFrom(buf io.Reader) (int64, error) { } func (p *Page) analyzePage() { + // TODO(bep) + if true { + return + } if p.isCJKLanguage { p.WordCount = 0 for _, word := range p.PlainWords() { |