Skip to content

Commit

Permalink
Use strings.Builder in endingToString (#1506)
Browse files Browse the repository at this point in the history
This will cause one less allocation as String allocates on a
bytes.Buffer but not on a strings.Builder.
  • Loading branch information
tmthrgd committed Nov 6, 2023
1 parent 3996b17 commit a16092f
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions scan_rr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dns

import (
"bytes"
"encoding/base64"
"errors"
"net"
Expand All @@ -12,23 +11,23 @@ import (
// A remainder of the rdata with embedded spaces, return the parsed string (sans the spaces)
// or an error
func endingToString(c *zlexer, errstr string) (string, *ParseError) {
var buffer bytes.Buffer
var s strings.Builder
l, _ := c.Next() // zString
for l.value != zNewline && l.value != zEOF {
if l.err {
return buffer.String(), &ParseError{"", errstr, l}
return s.String(), &ParseError{"", errstr, l}
}
switch l.value {
case zString:
buffer.WriteString(l.token)
s.WriteString(l.token)
case zBlank: // Ok
default:
return "", &ParseError{"", errstr, l}
}
l, _ = c.Next()
}

return buffer.String(), nil
return s.String(), nil
}

// A remainder of the rdata with embedded spaces, split on unquoted whitespace
Expand Down

0 comments on commit a16092f

Please sign in to comment.