mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
wal: reduce allocation when encoding int64
This commit is contained in:
parent
2990c29a71
commit
8bcaa2bfdf
@ -29,8 +29,9 @@ type encoder struct {
|
||||
mu sync.Mutex
|
||||
bw *bufio.Writer
|
||||
|
||||
crc hash.Hash32
|
||||
buf []byte
|
||||
crc hash.Hash32
|
||||
buf []byte
|
||||
uint64buf []byte
|
||||
}
|
||||
|
||||
func newEncoder(w io.Writer, prevCrc uint32) *encoder {
|
||||
@ -38,7 +39,8 @@ func newEncoder(w io.Writer, prevCrc uint32) *encoder {
|
||||
bw: bufio.NewWriter(w),
|
||||
crc: crc.New(prevCrc, crcTable),
|
||||
// 1MB buffer
|
||||
buf: make([]byte, 1024*1024),
|
||||
buf: make([]byte, 1024*1024),
|
||||
uint64buf: make([]byte, 8),
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,7 +68,7 @@ func (e *encoder) encode(rec *walpb.Record) error {
|
||||
}
|
||||
data = e.buf[:n]
|
||||
}
|
||||
if err := writeInt64(e.bw, int64(len(data))); err != nil {
|
||||
if err := writeInt64(e.bw, int64(len(data)), e.uint64buf); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = e.bw.Write(data)
|
||||
@ -79,7 +81,9 @@ func (e *encoder) flush() error {
|
||||
return e.bw.Flush()
|
||||
}
|
||||
|
||||
func writeInt64(w io.Writer, n int64) error {
|
||||
// TODO: use putuint64 to reduce two alloctions
|
||||
return binary.Write(w, binary.LittleEndian, n)
|
||||
func writeInt64(w io.Writer, n int64, buf []byte) error {
|
||||
// http://golang.org/src/encoding/binary/binary.go
|
||||
binary.LittleEndian.PutUint64(buf, uint64(n))
|
||||
_, err := w.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user