Skip to content

Commit

Permalink
chaindump: microoptimize memory management
Browse files Browse the repository at this point in the history
Reuse buffers.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Jul 16, 2024
1 parent d9d9d00 commit 1aefd7d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/core/chaindump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,38 @@ type DumperRestorer interface {
// Dump writes count blocks from start to the provided writer.
// Note: header needs to be written separately by a client.
func Dump(bc DumperRestorer, w *io.BinWriter, start, count uint32) error {
var buf = io.NewBufBinWriter()

for i := start; i < start+count; i++ {
bh := bc.GetHeaderHash(i)
b, err := bc.GetBlock(bh)
if err != nil {
return err
}
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
bytes := buf.Bytes()
w.WriteU32LE(uint32(len(bytes)))
w.WriteBytes(bytes)
if w.Err != nil {
return w.Err
}
buf.Reset()
}
return nil
}

// Restore restores blocks from the provided reader.
// f is called after addition of every block.
func Restore(bc DumperRestorer, r *io.BinReader, skip, count uint32, f func(b *block.Block) error) error {
var buf []byte

readBlock := func(r *io.BinReader) ([]byte, error) {
var size = r.ReadU32LE()
buf := make([]byte, size)
if uint32(cap(buf)) < size {
buf = make([]byte, size)
} else {
buf = buf[:size]
}
r.ReadBytes(buf)
return buf, r.Err
}
Expand Down

0 comments on commit 1aefd7d

Please sign in to comment.