pkg/util/net: replace bytes.Buffer with strings.Builder

strings.Builder has better performance over bytes.Buffer for building
strings:

Benchmark results:
```
name                  old time/op    new time/op    delta
_ParseQuotedString-8     146ns ±20%     105ns ± 2%  -28.14%  (p=0.008 n=5+5)

name                  old alloc/op   new alloc/op   delta
_ParseQuotedString-8     80.0B ± 0%     24.0B ± 0%  -70.00%  (p=0.008 n=5+5)

name                  old allocs/op  new allocs/op  delta
_ParseQuotedString-8      2.00 ± 0%      2.00 ± 0%     ~     (all equal)
```

Signed-off-by: André Martins <aanm90@gmail.com>
This commit is contained in:
André Martins 2020-12-28 14:54:40 +01:00
parent 0a839c6c3b
commit b3561401fd
2 changed files with 19 additions and 1 deletions

View File

@ -693,7 +693,7 @@ func parseQuotedString(quotedString string) (string, string, error) {
var remainder string
escaping := false
closedQuote := false
result := &bytes.Buffer{}
result := &strings.Builder{}
loop:
for i := 0; i < len(quotedString); i++ {
b := quotedString[i]

View File

@ -1107,3 +1107,21 @@ func TestPingTimeoutSeconds(t *testing.T) {
}
reset()
}
func Benchmark_ParseQuotedString(b *testing.B) {
str := `"The quick brown" fox jumps over the lazy dog`
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
quoted, remainder, err := parseQuotedString(str)
if err != nil {
b.Errorf("Unexpected error %s", err)
}
if quoted != "The quick brown" {
b.Errorf("Unexpected quoted string %s", quoted)
}
if remainder != "fox jumps over the lazy dog" {
b.Errorf("Unexpected remainder string %s", quoted)
}
}
}