Skip to content

Commit

Permalink
Avoid calling strings.Fields multiple times with same content
Browse files Browse the repository at this point in the history
This should be a relief for big sites.
  • Loading branch information
bep committed Feb 4, 2015
1 parent 11a19e0 commit f8704c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 1 addition & 2 deletions helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,7 @@ func TruncateWords(s string, max int) string {

// TruncateWordsToWholeSentence takes content and an int
// and returns entire sentences from content, delimited by the int.
func TruncateWordsToWholeSentence(s string, max int) string {
words := strings.Fields(s)
func TruncateWordsToWholeSentence(words []string, max int) string {
if max > len(words) {
return strings.Join(words, " ")
}
Expand Down
28 changes: 21 additions & 7 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type Page struct {
rawContent []byte
contentShortCodes map[string]string
plain string // TODO should be []byte
plainWords []string
plainInit sync.Once
renderingConfig *helpers.Blackfriday
renderingConfigInit sync.Once
PageMeta
Expand Down Expand Up @@ -97,12 +99,22 @@ type Position struct {
type Pages []*Page

func (p *Page) Plain() string {
if len(p.plain) == 0 {
p.plain = helpers.StripHTML(string(p.Content))
}
p.initPlain()
return p.plain
}

func (p *Page) PlainWords() []string {
p.initPlain()
return p.plainWords
}

func (p *Page) initPlain() {
p.plainInit.Do(func() {
p.plain = helpers.StripHTML(string(p.Content))
p.plainWords = strings.Fields(p.plain)
})
}

func (p *Page) IsNode() bool {
return false
}
Expand Down Expand Up @@ -178,9 +190,11 @@ func (p *Page) setSummary() {
} else {
// If hugo defines split:
// render, strip html, then split
plain := strings.Join(strings.Fields(p.Plain()), " ")

This comment has been minimized.

Copy link
@bep

bep Feb 5, 2015

Author Member

@anthonyfok I removed this; it adds about 10% to the processing time -- and, if really needed, should be solved differently. A test case would be nice.

This comment has been minimized.

Copy link
@anthonyfok

anthonyfok Feb 5, 2015

Member

@bep Thank you for your wonderful detective work! I agree with you: there must be a less expensive way to do this. I have opened Issue #880 as a follow-up.

p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(plain, helpers.SummaryLength)))
p.Truncated = len(p.Summary) != len(plain)
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)))

// todo bep - check if the Plain() can be trimmed earlier
p.Truncated = len(p.Summary) != len(strings.Trim(p.Plain(), "\n\r "))

}
}

Expand Down Expand Up @@ -322,7 +336,7 @@ func (p *Page) ReadFrom(buf io.Reader) (err error) {
}

func (p *Page) analyzePage() {
p.WordCount = helpers.TotalWords(p.Plain())
p.WordCount = len(p.PlainWords())
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
p.ReadingTime = int((p.WordCount + 212) / 213)
}
Expand Down

0 comments on commit f8704c1

Please sign in to comment.