Created
May 3, 2023 11:44
-
-
Save johnashu/0305231c17ab0d48f7678190c872d514 to your computer and use it in GitHub Desktop.
Create Yul logic to 'Unroll' arrays to save gas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
asm = "\t\tsum := add(sum, calldataload(add(weights, {})))\n" | |
arrayLen = 5 | |
hexAdd = 0x20 | |
name = 'weights' | |
unrolled = "" | |
for x in range(1, arrayLen): | |
added = hexAdd * x | |
unrolled += asm.format(hex(added)) | |
functionUnroll = f""" | |
function _sumWeights(uint256[{arrayLen}] calldata {name}) internal pure returns(uint sum){{ | |
\tassembly("memory-safe"){{ | |
\t\tsum := calldataload(weights) | |
{unrolled} | |
}} | |
}} | |
""" | |
print(functionUnroll) | |
# function _sumWeights(uint256[5] calldata weights) internal pure returns(uint sum){ | |
# assembly("memory-safe"){ | |
# sum := calldataload(weights) | |
# sum := add(sum, calldataload(add(weights, 0x20))) | |
# sum := add(sum, calldataload(add(weights, 0x40))) | |
# sum := add(sum, calldataload(add(weights, 0x60))) | |
# sum := add(sum, calldataload(add(weights, 0x80))) | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment