no copy if cap is enough

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-07-22 11:47:59 +08:00
parent a35b45d1d7
commit dc12b75db6

View file

@ -381,8 +381,17 @@ func (b *blockWriter) full() bool {
// This function is safe to be called multiple times. // This function is safe to be called multiple times.
func (b *blockWriter) finish() []byte { func (b *blockWriter) finish() []byte {
restartsLen := len(b.restarts) restartsLen := len(b.restarts)
buf := make([]byte, len(b.data)+restartsLen*2+1) extra := restartsLen*2 + 1
var buf []byte
if cap(b.data)-len(b.data) >= extra {
// Enough capacity, just reslice; data is already in place.
buf = b.data[:len(b.data)+extra]
} else {
// Not enough capacity, allocate and copy.
buf = make([]byte, len(b.data)+extra)
copy(buf, b.data) copy(buf, b.data)
}
restartsOffset := len(b.data) restartsOffset := len(b.data)
for i, restart := range b.restarts { for i, restart := range b.restarts {