From d162bbd7990b6a523bdadcd10bf60fcb43ecf270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 27 Nov 2020 08:46:58 +0100 Subject: [PATCH] publisher: Fix memory usage in writeStats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` 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 --- publisher/htmlElementsCollector.go | 4 +++- publisher/htmlElementsCollector_test.go | 27 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/publisher/htmlElementsCollector.go b/publisher/htmlElementsCollector.go index e2f8fd2ca..b9b0f4e11 100644 --- a/publisher/htmlElementsCollector.go +++ b/publisher/htmlElementsCollector.go @@ -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) } diff --git a/publisher/htmlElementsCollector_test.go b/publisher/htmlElementsCollector_test.go index 6b5ef9863..ab1529288 100644 --- a/publisher/htmlElementsCollector_test.go +++ b/publisher/htmlElementsCollector_test.go @@ -99,3 +99,30 @@ func TestClassCollector(t *testing.T) { } } + +func BenchmarkClassCollectorWriter(b *testing.B) { + const benchHTML = ` + + + +
+ + +
+ + + +
+ + + + + + +` + for i := 0; i < b.N; i++ { + w := newHTMLElementsCollectorWriter(newHTMLElementsCollector()) + fmt.Fprint(w, benchHTML) + + } +}