internal/era/e2store: avoid double allocation during write

This commit is contained in:
lightclient@protonmail.com 2023-06-05 09:48:55 +02:00 committed by lightclient
parent b98b6d6524
commit af6837f249
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -50,11 +50,17 @@ func NewWriter(w io.Writer) *Writer {
// record store the type (2 bytes), the length (4 bytes), and some reserved // record store the type (2 bytes), the length (4 bytes), and some reserved
// data (2 bytes). The remaining bytes store b. // data (2 bytes). The remaining bytes store b.
func (w *Writer) Write(typ uint16, b []byte) (int, error) { func (w *Writer) Write(typ uint16, b []byte) (int, error) {
buf := make([]byte, headerSize+len(b)) buf := make([]byte, headerSize)
binary.LittleEndian.PutUint16(buf, typ) binary.LittleEndian.PutUint16(buf, typ)
binary.LittleEndian.PutUint32(buf[2:], uint32(len(b))) binary.LittleEndian.PutUint32(buf[2:], uint32(len(b)))
copy(buf[8:], b)
return w.w.Write(buf) // Write header.
if n, err := w.w.Write(buf); err != nil {
return n, err
}
// Write value, return combined write size.
n, err := w.w.Write(b)
return n + headerSize, err
} }
// A Reader reads entries from an e2store-encoded file. // A Reader reads entries from an e2store-encoded file.