mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-16 20:53:59 +02:00
Lazy calculate WordCount, ReadingTime and FuzzyWordCount
This avoids having to execute these expensive operations for sites not using these values. This commit sums up a set of wordcounting and autosummary related performance improvements. The effect of these kind of depends on what features your site use, but a benchmark from 4 Hugo sites in the wild shows promise: ``` benchmark old ns/op new ns/op delta BenchmarkHugo-4 21293005843 20032857342 -5.92% benchmark old allocs new allocs delta BenchmarkHugo-4 65290922 65186032 -0.16% benchmark old bytes new bytes delta BenchmarkHugo-4 9771213416 9681866464 -0.91% ``` Closes #2378
This commit is contained in:
@@ -138,19 +138,28 @@ func StripHTML(s string) string {
|
||||
// Walk through the string removing all tags
|
||||
b := bp.GetBuffer()
|
||||
defer bp.PutBuffer(b)
|
||||
|
||||
inTag := false
|
||||
var inTag, isSpace, wasSpace bool
|
||||
for _, r := range s {
|
||||
switch r {
|
||||
case '<':
|
||||
if !inTag {
|
||||
isSpace = false
|
||||
}
|
||||
|
||||
switch {
|
||||
case r == '<':
|
||||
inTag = true
|
||||
case '>':
|
||||
case r == '>':
|
||||
inTag = false
|
||||
case unicode.IsSpace(r):
|
||||
isSpace = true
|
||||
fallthrough
|
||||
default:
|
||||
if !inTag {
|
||||
if !inTag && (!isSpace || (isSpace && !wasSpace)) {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
wasSpace = isSpace
|
||||
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
Reference in New Issue
Block a user