From c60ee1aab0808d19e60ad53500ff9a7ac3dfd3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Sat, 8 Oct 2016 18:17:00 +0300 Subject: [PATCH] Avoid allocating []byte for every written newline This shaves off another ~25% of allocs. --- html.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/html.go b/html.go index 8f37187..2785bba 100644 --- a/html.go +++ b/html.go @@ -314,11 +314,6 @@ func appendLanguageAttr(attrs []string, info []byte) []string { return attrs } -var ( - gtBytes = []byte{'>'} - spaceBytes = []byte{' '} -) - func (r *HTMLRenderer) tag(w io.Writer, name []byte, attrs []string) { w.Write(name) if len(attrs) > 0 { @@ -385,10 +380,16 @@ func (r *HTMLRenderer) out(w io.Writer, text []byte) { func (r *HTMLRenderer) cr(w io.Writer) { if r.lastOutputLen > 0 { - r.out(w, []byte{'\n'}) + r.out(w, nlBytes) } } +var ( + nlBytes = []byte{'\n'} + gtBytes = []byte{'>'} + spaceBytes = []byte{' '} +) + var ( brTag = []byte("
") brXHTMLTag = []byte("
") @@ -504,7 +505,7 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt } } case Softbreak: - r.out(w, []byte{'\n'}) + r.cr(w) // TODO: make it configurable via out(renderer.softbreak) case Hardbreak: if r.Flags&UseXHTML == 0 {