Skip to content

Instantly share code, notes, and snippets.

@ecmendenhall
Last active March 12, 2022 02:12
Show Gist options
  • Save ecmendenhall/3d6736fbbfa874b6c6534ac4af7919f5 to your computer and use it in GitHub Desktop.
Save ecmendenhall/3d6736fbbfa874b6c6534ac4af7919f5 to your computer and use it in GitHub Desktop.
when you're gas golfing
$ 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)
$ 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 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)
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.10;
contract Looper {
function doStuff(uint256 i) public pure returns (uint256) {
return i;
}
}
contract Looper1 is Looper {
function loop(uint256[] memory array) external pure {
for (uint256 i=0; i<array.length; i++) {
doStuff(array[i]);
}
}
}
contract Looper2 is Looper {
function loop(uint256[] memory array) external 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 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 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 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 pure {
uint256 length = array.length;
for (uint256 i; i<length;) {
doStuff(array[i]);
unchecked { ++i; }
}
}
}
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.10;
import "../../lib/ds-test/src/test.sol";
import "../Loops.sol";
contract Loop1 is DSTest {
uint256[] internal array;
Looper1 internal looper = new Looper1();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
contract Loop2 is DSTest {
uint256[] internal array;
Looper2 internal looper = new Looper2();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
contract Loop3 is DSTest {
uint256[] internal array;
Looper3 internal looper = new Looper3();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
contract Loop4 is DSTest {
uint256[] internal array;
Looper4 internal looper = new Looper4();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
contract Loop5 is DSTest {
uint256[] internal array;
Looper5 internal looper = new Looper5();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
contract Loop6 is DSTest {
uint256[] internal array;
Looper6 internal looper = new Looper6();
function setUp() public {
for (uint256 j; j < 100;) {
array.push(j);
unchecked {++j;}
}
}
function test_loop() public view {
looper.loop(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment