Skip to content

Commit

Permalink
adding 08 comparing external to public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
devdacian committed Jan 22, 2025
1 parent 44b1d5e commit 5224bda
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ It is cheaper to use named return variables and remove explicit `return` stateme
- return owners;
}
```

### #8 Use External Instead Of Public Functions: NOT EFFECTIVE ###
Marking a `public` function as `external` appears to have no effect on gas costs:
```solidity
// both implementations cost the same gas
function resetId(uint256 id) external {
idToOwner[id] = address(0);
}
function resetId(uint256 id) public {
idToOwner[id] = address(0);
}
```
14 changes: 14 additions & 0 deletions src/08-use-ext-func/IdRegUseExtFunc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {IdRegUnop} from "../IdRegUnop.sol";

contract IdRegUseExtFunc is IdRegUnop {
// @audit change from `external` to `public` to see if
// gas costs increase (compile breaks the other way)
function resetId(uint256 id) public override {
idToOwner[id] = address(0);
}
}


4 changes: 2 additions & 2 deletions test/05-del-to-def-val/IdRegCalldataArrayInputTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {IdRegDelToDefVal} from "../../src/05-del-to-def-val/IdRegDelToDefVal.sol

// Optimizer ON, 10000 runs
// ========================
// Pre : 979 gas
// Pre : 1002 gas
// forge test --optimizer-runs 10000 --match-contract IdRegUnopTest --match-test test_resetId -vvv
// Post : 979 gas (no improvement)
// Post : 1002 gas (no improvement)
// forge test --optimizer-runs 10000 --match-contract IdRegDelToDefVal --match-test test_resetId -vvv
//
// Optimizer OFF
Expand Down
29 changes: 29 additions & 0 deletions test/08-use-ext-func/IdRegUseExtFuncTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {IdRegUnopTest} from "../IdRegUnopTest.sol";
import {IdRegUseExtFunc} from "../../src/08-use-ext-func/IdRegUseExtFunc.sol";

// Optimizer ON, 10000 runs
// ========================
// Pre : 1002 gas
// forge test --optimizer-runs 10000 --match-contract IdRegUnopTest --match-test test_resetId -vvv
// Post : 1002 gas (no improvement)
// forge test --optimizer-runs 10000 --match-contract IdRegUseExtFuncTest --match-test test_resetId -vvv
//
// Optimizer OFF
// =============
// Pre : 1018 gas
// forge test --match-contract IdRegUnopTest --match-test test_resetId -vvv
// Post : 1018 gas (no improvement)
// forge test --match-contract IdRegUseExtFuncTest --match-test test_resetId -vvv
//
// Conclusion
// ==========
// Setting storage to default values costs the same
// as using the `delete` keyword
contract IdRegUseExtFuncTest is IdRegUnopTest {
function setUp() external override {
idReg = new IdRegUseExtFunc();
}
}

0 comments on commit 5224bda

Please sign in to comment.