Merge pull request #97552 from aanm/pr/replace/buffer/with/strings

replace bytes.Buffer with strings.Builder
This commit is contained in:
Kubernetes Prow Robot 2021-01-15 14:49:56 -08:00 committed by GitHub
commit a99ebca030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 18 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package labels package labels
import ( import (
"bytes"
"fmt" "fmt"
"sort" "sort"
"strconv" "strconv"
@ -289,48 +288,55 @@ func (s internalSelector) Empty() bool {
// Requirement. If called on an invalid Requirement, an error is // Requirement. If called on an invalid Requirement, an error is
// returned. See NewRequirement for creating a valid Requirement. // returned. See NewRequirement for creating a valid Requirement.
func (r *Requirement) String() string { func (r *Requirement) String() string {
var buffer bytes.Buffer var sb strings.Builder
sb.Grow(
// length of r.key
len(r.key) +
// length of 'r.operator' + 2 spaces for the worst case ('in' and 'notin')
len(r.operator) + 2 +
// length of 'r.strValues' slice times. Heuristically 5 chars per word
+5*len(r.strValues))
if r.operator == selection.DoesNotExist { if r.operator == selection.DoesNotExist {
buffer.WriteString("!") sb.WriteString("!")
} }
buffer.WriteString(r.key) sb.WriteString(r.key)
switch r.operator { switch r.operator {
case selection.Equals: case selection.Equals:
buffer.WriteString("=") sb.WriteString("=")
case selection.DoubleEquals: case selection.DoubleEquals:
buffer.WriteString("==") sb.WriteString("==")
case selection.NotEquals: case selection.NotEquals:
buffer.WriteString("!=") sb.WriteString("!=")
case selection.In: case selection.In:
buffer.WriteString(" in ") sb.WriteString(" in ")
case selection.NotIn: case selection.NotIn:
buffer.WriteString(" notin ") sb.WriteString(" notin ")
case selection.GreaterThan: case selection.GreaterThan:
buffer.WriteString(">") sb.WriteString(">")
case selection.LessThan: case selection.LessThan:
buffer.WriteString("<") sb.WriteString("<")
case selection.Exists, selection.DoesNotExist: case selection.Exists, selection.DoesNotExist:
return buffer.String() return sb.String()
} }
switch r.operator { switch r.operator {
case selection.In, selection.NotIn: case selection.In, selection.NotIn:
buffer.WriteString("(") sb.WriteString("(")
} }
if len(r.strValues) == 1 { if len(r.strValues) == 1 {
buffer.WriteString(r.strValues[0]) sb.WriteString(r.strValues[0])
} else { // only > 1 since == 0 prohibited by NewRequirement } else { // only > 1 since == 0 prohibited by NewRequirement
// normalizes value order on output, without mutating the in-memory selector representation // normalizes value order on output, without mutating the in-memory selector representation
// also avoids normalization when it is not required, and ensures we do not mutate shared data // also avoids normalization when it is not required, and ensures we do not mutate shared data
buffer.WriteString(strings.Join(safeSort(r.strValues), ",")) sb.WriteString(strings.Join(safeSort(r.strValues), ","))
} }
switch r.operator { switch r.operator {
case selection.In, selection.NotIn: case selection.In, selection.NotIn:
buffer.WriteString(")") sb.WriteString(")")
} }
return buffer.String() return sb.String()
} }
// safeSort sorts input strings without modification // safeSort sorts input strings without modification

View File

@ -910,3 +910,21 @@ func TestValidatedSelectorFromSet(t *testing.T) {
} }
} }
} }
func BenchmarkRequirementString(b *testing.B) {
r := Requirement{
key: "environment",
operator: selection.NotIn,
strValues: []string{
"dev",
},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if r.String() != "environment notin (dev)" {
b.Errorf("Unexpected Requirement string")
}
}
}

View File

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

View File

@ -1107,3 +1107,21 @@ func TestPingTimeoutSeconds(t *testing.T) {
} }
reset() 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)
}
}
}