EYBlockchain / timber

Construct a Merkle Tree database from Ethereum logs.
Other
67 stars 19 forks source link

perf: cheaper batch inserts: fewer storage overwrites #10

Closed iAmMichaelConnor closed 4 years ago

iAmMichaelConnor commented 4 years ago

I was measuring gas costs of different tree heights and batch numbers when I spotted an inefficiency or two in the merkletree.sol contracts.

The main gas-saving update is in batch inserts. Previously, at each for-loop iteration, the frontier was being written to, which is expensive (5,000 gas per update) because it is a storage variable. I created a memory variable tempFrontier to write to instead (much cheaper). We then only write to frontier at the end of all the loops.

Rough tests suggest this saves around 1,200,000 gas for a batch insert of 512 leaves!

Updated documentation (especially tests)

iAmMichaelConnor commented 4 years ago

(DNM label added until further testing is completed)

github-actions[bot] commented 4 years ago

:tada: This PR is included in version 2.1.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: