triedb/pathdb: simplify finish function in block writer

This commit is contained in:
Gary Rong 2025-07-22 14:53:32 +08:00
parent fe7ae65985
commit 2f98e38959

View file

@ -380,25 +380,10 @@ func (b *blockWriter) full() bool {
//
// This function is safe to be called multiple times.
func (b *blockWriter) finish() []byte {
var (
restartsLen = len(b.restarts)
restartsOff = len(b.data)
extra = restartsLen*2 + 1
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)
}
buf := make([]byte, len(b.restarts)*2+1)
for i, restart := range b.restarts {
binary.BigEndian.PutUint16(buf[restartsOff+2*i:], restart)
binary.BigEndian.PutUint16(buf[2*i:], restart)
}
buf[len(buf)-1] = byte(restartsLen)
return buf
buf[len(buf)-1] = byte(len(b.restarts))
return append(b.data, buf...)
}