diff --git a/encode.go b/encode.go index 37cfdf2..4df7521 100644 --- a/encode.go +++ b/encode.go @@ -63,7 +63,7 @@ var ( // single space is written before the second and subsequent keys in a record. // Nothing is written if a non-nil error is returned. func (enc *Encoder) EncodeKeyval(key, value interface{}) error { - defer enc.scratch.Reset() + enc.scratch.Reset() if enc.needSep { if _, err := enc.scratch.Write(space); err != nil { return err diff --git a/encode_test.go b/encode_test.go index 39482a5..c7aa1d5 100644 --- a/encode_test.go +++ b/encode_test.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "io/ioutil" "reflect" "testing" "time" @@ -187,3 +188,12 @@ type errorMarshaler struct{} func (errorMarshaler) MarshalText() ([]byte, error) { return nil, marshalError } + +func BenchmarkEncodeKeyval(b *testing.B) { + b.ReportAllocs() + enc := logfmt.NewEncoder(ioutil.Discard) + for i := 0; i < b.N; i++ { + enc.EncodeKeyval("sk", "10") + enc.EncodeKeyval("some-key", "a rather long string with spaces") + } +}