Last active
February 28, 2023 12:22
-
-
Save ecmendenhall/5337006f4e02d1660c339c087c2787d9 to your computer and use it in GitHub Desktop.
when you're gas golfing
This file contains 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
$ dapp --version | |
dapp 0.35.0 | |
solc, the solidity compiler commandline interface | |
Version: 0.8.10+commit.fc410830.Darwin.appleclang | |
hevm 0.49.0 | |
$ dapp test | |
+ dapp clean | |
+ rm -rf out | |
Running 1 tests for src/test/Loops.t.sol:Loop1 | |
[PASS] test_loop() (gas: 45916) | |
Running 1 tests for src/test/Loops.t.sol:Loop2 | |
[PASS] test_loop() (gas: 45621) | |
Running 1 tests for src/test/Loops.t.sol:Loop3 | |
[PASS] test_loop() (gas: 45121) | |
Running 1 tests for src/test/Loops.t.sol:Loop4 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 tests for src/test/Loops.t.sol:Loop5 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 tests for src/test/Loops.t.sol:Loop6 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 tests for src/test/Loops.t.sol:Loop7 | |
[PASS] test_loop() (gas: 37948) |
This file contains 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
$ forge --version | |
forge 0.1.0 (1d31ecb 2022-03-12T00:16:00.034811+00:00) | |
$ forge test | |
[⠊] Compiling... | |
No files changed, compilation skipped | |
Running 1 test for Loop1.json:Loop1 | |
[PASS] test_loop() (gas: 45916) | |
Running 1 test for Loop2.json:Loop2 | |
[PASS] test_loop() (gas: 45621) | |
Running 1 test for Loop3.json:Loop3 | |
[PASS] test_loop() (gas: 45121) | |
Running 1 test for Loop4.json:Loop4 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 test for Loop5.json:Loop5 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 test for Loop6.json:Loop6 | |
[PASS] test_loop() (gas: 38221) | |
Running 1 test for Loop7.json:Loop7 | |
[PASS] test_loop() (gas: 37948) |
This file contains 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
Running 1 test for Loop1.json:Loop1 | |
[PASS] test_loop() (gas: 106969) | |
Running 1 test for Loop3.json:Loop3 | |
[PASS] test_loop() (gas: 106182) | |
Running 1 test for Loop2.json:Loop2 | |
[PASS] test_loop() (gas: 106682) | |
Running 1 test for Loop4.json:Loop4 | |
[PASS] test_loop() (gas: 94382) | |
Running 1 test for Loop5.json:Loop5 | |
[PASS] test_loop() (gas: 93882) | |
Running 1 test for Loop6.json:Loop6 | |
[PASS] test_loop() (gas: 93882) | |
Running 1 test for Loop7.json:Loop7 | |
[PASS] test_loop() (gas: 94506) |
This file contains 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
// SPDX-License-Identifier: Unlicense | |
pragma solidity 0.8.10; | |
interface ILooper { | |
function doStuff(uint256 i) external pure returns (uint256); | |
function loop(uint256[] memory array) external pure; | |
} | |
abstract contract Looper is ILooper { | |
function doStuff(uint256 i) public pure returns (uint256) { | |
return i; | |
} | |
function loop(uint256[] memory array) external virtual pure; | |
} | |
contract Looper1 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
for (uint256 i=0; i<array.length; i++) { | |
doStuff(array[i]); | |
} | |
} | |
} | |
contract Looper2 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length; | |
for (uint256 i=0; i<length; i++) { | |
doStuff(array[i]); | |
} | |
} | |
} | |
contract Looper3 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length; | |
for (uint256 i=0; i<length; ++i) { | |
doStuff(array[i]); | |
} | |
} | |
} | |
contract Looper4 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length; | |
for (uint256 i=0; i<length;) { | |
doStuff(array[i]); | |
unchecked { i++; } | |
} | |
} | |
} | |
contract Looper5 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length; | |
for (uint256 i=0; i<length;) { | |
doStuff(array[i]); | |
unchecked { ++i; } | |
} | |
} | |
} | |
contract Looper6 is Looper { | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length; | |
for (uint256 i; i<length;) { | |
doStuff(array[i]); | |
unchecked { ++i; } | |
} | |
} | |
} | |
contract Looper7 is Looper { | |
// https://twitter.com/WilsonCusack/status/1502468024682561537 | |
function loop(uint256[] memory array) external override pure { | |
uint256 length = array.length - 1; | |
while (length != 0) { | |
doStuff(array[length]); | |
unchecked { length--; } | |
} | |
doStuff(array[0]); | |
} | |
} |
This file contains 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
// SPDX-License-Identifier: Unlicense | |
pragma solidity 0.8.10; | |
import "../../lib/ds-test/src/test.sol"; | |
import "../Loops.sol"; | |
contract LoopTest is DSTest { | |
uint256[] internal array; | |
ILooper[] internal loopers; | |
ILooper internal looper; | |
constructor(uint256 id) { | |
loopers.push(new Looper1()); | |
loopers.push(new Looper2()); | |
loopers.push(new Looper3()); | |
loopers.push(new Looper4()); | |
loopers.push(new Looper5()); | |
loopers.push(new Looper1()); | |
loopers.push(new Looper7()); | |
looper = loopers[id-1]; | |
} | |
function setUp() public { | |
for (uint256 i; i < 100;) { | |
array.push(i); | |
unchecked {++i;} | |
} | |
} | |
function test_loop() public view { | |
looper.loop(array); | |
} | |
} | |
contract Loop1 is LoopTest(1) {} | |
contract Loop2 is LoopTest(2) {} | |
contract Loop3 is LoopTest(3) {} | |
contract Loop4 is LoopTest(4) {} | |
contract Loop5 is LoopTest(5) {} | |
contract Loop6 is LoopTest(6) {} | |
contract Loop7 is LoopTest(7) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment