Suggestion for gas optimization
The implementation of the SP1Helios contract contains several areas where gas usage can be optimized.
// Set all the storage slots.
for (uint256 i = 0; i < po.storageSlots.length; i++) {
bytes32 key = computeStorageSlotKey(
po.executionBlockNumber, po.storageSlots[i].contractAddress, po.storageSlots[i].key
);
storageSlots[key] = po.storageSlots[i].value;
}
In the update
and updateStorageSlot
functions, the loop variable i
used in the for loops is incremented using the postfix increment operator. Replacing the postfix increment operator with the prefix increment operator in the for loops can help save gas.
Additionally, since the loop variable i
is of type uint256
and there is no risk of overflow, incrementing it within an unchecked
block can further save gas. Lastly, caching po.storageSlots.length
before the loop, instead of accessing it in every iteration, would also help improve efficiency.
The redundant check described in Finding ref↗ also involves unnecessary gas usage; therefore, introducing the fix for that finding can further optimize gas usage.