Skip to content

Commit

Permalink
publisher: Fix memory usage in writeStats
Browse files Browse the repository at this point in the history
```
name                     old time/op    new time/op    delta
ClassCollectorWriter-16    72.1µs ± 0%    32.3µs ± 0%  -55.17%  (p=0.029 n=4+4)

name                     old alloc/op   new alloc/op   delta
ClassCollectorWriter-16    85.9kB ± 0%    35.1kB ± 0%  -59.16%  (p=0.029 n=4+4)

name                     old allocs/op  new allocs/op  delta
ClassCollectorWriter-16       329 ± 0%       149 ± 0%  -54.71%  (p=0.029 n=4+4)
```

Fixes #7945
  • Loading branch information
bep committed Nov 27, 2020
1 parent 17e0bbe commit d162bbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion publisher/htmlElementsCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ func (w *cssClassCollectorWriter) Write(p []byte) (n int, err error) {
continue
}

key := s

s, tagName := w.insertStandinHTMLElement(s)
el := parseHTMLElement(s)
el.Tag = tagName

w.collector.mu.Lock()
w.collector.elementSet[s] = true
w.collector.elementSet[key] = true
if el.Tag != "" {
w.collector.elements = append(w.collector.elements, el)
}
Expand Down
27 changes: 27 additions & 0 deletions publisher/htmlElementsCollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,30 @@ func TestClassCollector(t *testing.T) {
}

}

func BenchmarkClassCollectorWriter(b *testing.B) {
const benchHTML = `
<html>
<body id="i1" class="a b c d">
<a class="c d e"></a>
<br>
<a class="c d e"></a>
<a class="c d e"></a>
<br>
<a id="i2" class="c d e f"></a>
<a id="i3" class="c d e"></a>
<a class="c d e"></a>
<br>
<a class="c d e"></a>
<a class="c d e"></a>
<a class="c d e"></a>
<a class="c d e"></a>
</body>
</html>
`
for i := 0; i < b.N; i++ {
w := newHTMLElementsCollectorWriter(newHTMLElementsCollector())
fmt.Fprint(w, benchHTML)

}
}

0 comments on commit d162bbd

Please sign in to comment.