From f522504216120c67495a8924fec8a13b31ac59cf Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Mon, 26 Nov 2018 16:41:39 +1030 Subject: [PATCH] Eliminate roBs allocation from packDomainName This allocation only occurred when s was escaped, but will no longer occur. --- msg.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/msg.go b/msg.go index f713b735..6e1f5593 100644 --- a/msg.go +++ b/msg.go @@ -212,11 +212,9 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c // Emit sequence of counted strings, chopping at dots. var ( - begin int - bs []byte - roBs = s - bsDirty bool - wasDot bool + begin int + bs []byte + wasDot bool ) loop: for i := 0; i < ls; i++ { @@ -247,7 +245,6 @@ loop: ls-- } - bsDirty = true wasDot = false case '.': if wasDot { @@ -267,16 +264,21 @@ loop: return lenmsg, labels, ErrBuf } - if compression != nil && bsDirty { - roBs = string(bs[:ls]) - bsDirty = false - } - // Don't try to compress '.' // We should only compress when compress is true, but we should also still pick // up names that can be used for *future* compression(s). if compression != nil && !isRootLabel(s, bs, begin, ls) { - if p, ok := compression[roBs[begin:]]; ok { + var ( + p int + ok bool + ) + if bs == nil { + p, ok = compression[s[begin:]] + } else { + p, ok = compression[string(bs[begin:ls])] + } + + if ok { // The first hit is the longest matching dname // keep the pointer offset we get back and store // the offset of the current name, because that's @@ -289,7 +291,11 @@ loop: } } else if off < maxCompressionOffset { // Only offsets smaller than maxCompressionOffset can be used. - compression[roBs[begin:]] = off + if bs == nil { + compression[s[begin:]] = off + } else { + compression[string(bs[begin:ls])] = off + } } }