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!
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 astorage
variable. I created amemory
variabletempFrontier
to write to instead (much cheaper). We then only write tofrontier
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)