Reduce string allocations

This commit is contained in:
Alex Ciuba 2014-01-26 15:08:05 -05:00
parent c97bb6d79d
commit f73d400eb2

12
msg.go
View File

@ -255,6 +255,7 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
// Emit sequence of counted strings, chopping at dots. // Emit sequence of counted strings, chopping at dots.
begin := 0 begin := 0
bs := []byte(s) bs := []byte(s)
ro_bs, bs_fresh := s, true
for i := 0; i < ls; i++ { for i := 0; i < ls; i++ {
if bs[i] == '\\' { if bs[i] == '\\' {
for j := i; j < ls-1; j++ { for j := i; j < ls-1; j++ {
@ -274,6 +275,7 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
} }
ls -= 2 ls -= 2
} }
bs_fresh = false
continue continue
} }
@ -304,12 +306,16 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
} }
off++ off++
} }
if compress && !bs_fresh {
ro_bs = string(bs)
bs_fresh = true
}
// Dont try to compress '.' // Dont try to compress '.'
if compression != nil && string(bs[begin:]) != "." { if compress && ro_bs[begin:] != "." {
if p, ok := compression[string(bs[begin:])]; !ok { if p, ok := compression[ro_bs[begin:]]; !ok {
// Only offsets smaller than this can be used. // Only offsets smaller than this can be used.
if offset < maxCompressionOffset { if offset < maxCompressionOffset {
compression[string(bs[begin:])] = offset compression[ro_bs[begin:]] = offset
} }
} else { } else {
// The first hit is the longest matching dname // The first hit is the longest matching dname