1
0
mirror of https://github.com/taigrr/bitcask synced 2025-01-18 04:03:17 -08:00

Improve write performance by ~33% to 80,000 writes/sec buf reducing syscalls and using a bufio.Writer

This commit is contained in:
James Mills
2019-03-16 07:41:37 +10:00
parent 3f1d6635c4
commit 2585222830
2 changed files with 33 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package streampb
import (
"bufio"
"encoding/binary"
"io"
@@ -16,13 +17,13 @@ const (
// NewEncoder creates a streaming protobuf encoder.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
return &Encoder{w: bufio.NewWriter(w)}
}
// Encoder wraps an underlying io.Writer and allows you to stream
// proto encodings on it.
type Encoder struct {
w io.Writer
w *bufio.Writer
}
// Encode takes any proto.Message and streams it to the underlying writer.
@@ -40,8 +41,15 @@ func (e *Encoder) Encode(msg proto.Message) error {
return errors.Wrap(err, "failed writing length prefix")
}
_, err = e.w.Write(buf)
return errors.Wrap(err, "failed writing marshaled data")
if _, err = e.w.Write(buf); err != nil {
return errors.Wrap(err, "failed writing marshaled data")
}
if err = e.w.Flush(); err != nil {
return errors.Wrap(err, "failed flushing data")
}
return nil
}
// NewDecoder creates a streaming protobuf decoder.