Skip to content

Conversation

@Philogy
Copy link

@Philogy Philogy commented Nov 2, 2023

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 from call is then used to index into memory:

let suc := call(...)
mstore8(suc, 0) // 0 or 1 depending if flag is set to false or true on failure

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 (1x MSTORE8 + 1x PUSH), meanwhile a branch costs 14 gas (1x PUSH + 1x JUMPI + 1x JUMPDEST)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants