-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
import {ModifyInputUnop} from "./ModifyInputUnop.sol"; | ||
|
||
contract ModifyInputFixed is ModifyInputUnop { | ||
|
||
constructor(uint256[] memory _comps) ModifyInputUnop(_comps) {} | ||
|
||
function returnLowest(uint256 input) external view override returns(uint256 lowest) { | ||
lowest = input; | ||
|
||
uint256 compsLen = comps.length; | ||
for(uint256 i; i<compsLen; i++) { | ||
// overwrite input for comparison, no need for additional | ||
// temporary variable if original input val doesn't need preservation | ||
input = comps[i]; | ||
|
||
if(input < lowest) { | ||
lowest = input; | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
interface IModifyInput { | ||
function returnLowest(uint256 input) external view returns(uint256 lowest); | ||
} | ||
|
||
contract ModifyInputUnop is IModifyInput { | ||
uint256[] internal comps; | ||
|
||
constructor(uint256[] memory _comps) { | ||
for(uint256 i; i<_comps.length; i++) { | ||
comps.push(_comps[i]); | ||
} | ||
} | ||
|
||
function returnLowest(uint256 input) external view virtual returns(uint256 lowest) { | ||
lowest = input; | ||
|
||
uint256 compsLen = comps.length; | ||
for(uint256 i; i<compsLen; i++) { | ||
// use a temporary variable for the comparison | ||
uint256 temp = comps[i]; | ||
|
||
if(temp < lowest) { | ||
lowest = temp; | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {IModifyInput, ModifyInputUnop} from "../../src/14-modify-input/ModifyInputUnop.sol"; | ||
import {ModifyInputFixed} from "../../src/14-modify-input/ModifyInputFixed.sol"; | ||
|
||
// Optimizer ON, 10000 runs | ||
// ======================== | ||
// Pre : 19260 gas | ||
// Post : 19223 gas (0.19% cheaper) | ||
// forge test --optimizer-runs 10000 --match-contract ModifyInputTest --match-test test_returnLowest -vvv | ||
// | ||
// Optimizer OFF | ||
// ============= | ||
// Pre : 19278 gas | ||
// Post : 19241 gas (0.19% cheaper) | ||
// forge test --match-contract ModifyInputTest --match-test test_returnLowest -vvv | ||
// | ||
// Conclusion | ||
// ========== | ||
// Modifying an input variable whose value doesn't need to be preserved | ||
// is more gas-efficient than using an additional temporary variable | ||
contract ModifyInputTest is Test { | ||
IModifyInput internal modifyInputUnop; | ||
IModifyInput internal modifyInputFixed; | ||
|
||
function setUp() external virtual { | ||
// create contracts being tested | ||
uint256[] memory comps = new uint256[](5); | ||
comps[0] = 5; | ||
comps[1] = 4; | ||
comps[2] = 3; | ||
comps[3] = 2; | ||
comps[4] = 1; | ||
|
||
modifyInputUnop = new ModifyInputUnop(comps); | ||
modifyInputFixed = new ModifyInputFixed(comps); | ||
|
||
assertEq(modifyInputUnop.returnLowest(10), comps[4]); | ||
assertEq(modifyInputFixed.returnLowest(10), comps[4]); | ||
} | ||
|
||
function test_returnLowest() external view { | ||
// call functions under test | ||
uint256 gasPre = gasleft(); | ||
modifyInputUnop.returnLowest(10); | ||
console.log("Unoptimized returnLowest() cost %d gas", gasPre - gasleft()); | ||
|
||
gasPre = gasleft(); | ||
modifyInputFixed.returnLowest(10); | ||
console.log("Optimized returnLowest() cost %d gas", gasPre - gasleft()); | ||
} | ||
} |