mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
no copy if cap is enough
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
a35b45d1d7
commit
dc12b75db6
1 changed files with 11 additions and 2 deletions
|
|
@ -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
|
||||||
copy(buf, b.data)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
restartsOffset := len(b.data)
|
restartsOffset := len(b.data)
|
||||||
for i, restart := range b.restarts {
|
for i, restart := range b.restarts {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue