mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
triedb/pathdb: simplify finish function in block writer
This commit is contained in:
parent
fe7ae65985
commit
2f98e38959
1 changed files with 4 additions and 19 deletions
|
|
@ -380,25 +380,10 @@ 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 {
|
||||||
var (
|
buf := make([]byte, len(b.restarts)*2+1)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, restart := range b.restarts {
|
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)
|
buf[len(buf)-1] = byte(len(b.restarts))
|
||||||
return buf
|
return append(b.data, buf...)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue