Introduce calldata packing and other minor optimizations to ETH & ERC20 Aidropping #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Calldata Packing
Main savings ~128 gas / recipient comes from packing the recipient and amount into 32-byte values. The rest are pretty minor (balancing out the cost of unpacking).
Aggregated failure check
The main other optimization is to store failures in a branchless way and then simply check the failure flag once at the very end. To do this the 1st byte (byte at index 0) in memory is used as the error flag ("no error flag" in erc20, "error flag" in eth). The
success
flag returned fromcall
is then used to index into memory:If the call was successful (
suc := 1
) then byte-1 will be written to, leaving the error flag alone. If it was a failure (suc := 0
) then the error flag is change. This is cheaper in the bytecode as it's at most 6 gas (1xMSTORE8
+ 1xPUSH
), meanwhile a branch costs 14 gas (1xPUSH
+ 1xJUMPI
+ 1xJUMPDEST
)