orbs-network / orbs-network-go

Orbs node virtual chain core reference implementation in Go
MIT License
48 stars 12 forks source link

decrease file seek operations by decoding first #1624

Closed gadcl closed 4 years ago

ronnno commented 4 years ago

@gadcl your solution involves an extra read of a full block whenever the blocks don't appear in order in the file.

This can be easily avoided - consider fetchBlockFromFile(height primitives.BlockHeight, file *os.File, currentOffset int) (*protocol.BlockPairContainer, int, error)

notice in inbound and outbound currentOffset arguments. fetchBlockFromFile() can check the index of the requested block in memory, then compare it to the inbound currentOffset, if they are different you seek. otherwise just read. Before returning fetchBlockFromFile() will add the bytes read (returned from decode) to the effective offset (the real offset where the requested block began) and return the sum which should always be the current cursor offset.

What do you think?