Fix regression in kube-proxy (#106214)

* Fix regression in kube-proxy

Don't use a prepend() - that allocates.  Instead, make Write() take
either strings or slices (I wish we could express that better).

* WIP: switch to intf

* WIP: less appends

* tests and ipvs
This commit is contained in:
Tim Hockin
2021-11-08 15:14:49 -08:00
committed by GitHub
parent cda360c59f
commit 731dc8cf74
4 changed files with 126 additions and 88 deletions

View File

@@ -472,17 +472,29 @@ type LineBuffer struct {
b bytes.Buffer
}
// Write joins all words with spaces, terminates with newline and writes to buf.
func (buf *LineBuffer) Write(words ...string) {
// We avoid strings.Join for performance reasons.
for i := range words {
buf.b.WriteString(words[i])
if i < len(words)-1 {
// Write takes a list of arguments, each a string or []string, joins all the
// individual strings with spaces, terminates with newline, and writes to buf.
// Any other argument type will panic.
func (buf *LineBuffer) Write(args ...interface{}) {
for i, arg := range args {
if i > 0 {
buf.b.WriteByte(' ')
} else {
buf.b.WriteByte('\n')
}
switch x := arg.(type) {
case string:
buf.b.WriteString(x)
case []string:
for j, s := range x {
if j > 0 {
buf.b.WriteByte(' ')
}
buf.b.WriteString(s)
}
default:
panic(fmt.Sprintf("unknown argument type: %T", x))
}
}
buf.b.WriteByte('\n')
}
// WriteBytes writes bytes to buffer, and terminates with newline.