Skip to content

Instantly share code, notes, and snippets.

@drgorillamd
Created September 2, 2024 15:19
Show Gist options
  • Save drgorillamd/48f9636a0de5e7b0dd6d917a480f958b to your computer and use it in GitHub Desktop.
Save drgorillamd/48f9636a0de5e7b0dd6d917a480f958b to your computer and use it in GitHub Desktop.
calldata array length cached or nah?
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;
import {Test, console} from "forge-std/Test.sol";
// Ran 2 tests for test/Counter.t copy.sol:LenCalldata
// [PASS] test_test1() (gas: 15999)
// [PASS] test_test2() (gas: 15979)
contract LenCalldata is Test {
Test1 test1;
Test2 test2;
function setUp() public {
test1 = new Test1();
test2 = new Test2();
}
function test_test1() public {
uint256[] memory data = new uint256[](10);
test1.set(data);
}
function test_test2() public {
uint256[] memory data = new uint256[](10);
test2.set(data);
}
}
contract Test1 {
function set(uint256[] calldata data) external {
uint256 len = data.length;
for (uint256 i = 0; i < len; i++) {
console.log('meh');
}
}
}
contract Test2 {
function set(uint256[] calldata data) public {
for (uint256 i = 0; i < data.length; i++) {
console.log('meh');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment