Last active
May 9, 2023 01:08
-
-
Save mds1/5814df610df021d23ffe25b2b3de0e66 to your computer and use it in GitHub Desktop.
`abi.encodePacked` vs. `string.concat` (optimizer on, 200 runs, no via-ir) — bytecode of test files was also identical
This file contains hidden or 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 test -vvv | |
[⠢] Compiling... | |
[⠒] Compiling 1 files with 0.8.19 | |
[⠆] Solc 0.8.19 finished in 887.23ms | |
Compiler run successful! | |
Running 1 test for test/Counter.t.sol:TestShort_EncodePacked | |
[PASS] test1() (gas: 4373) | |
Logs: | |
462 | |
helloworld | |
Test result: ok. 1 passed; 0 failed; finished in 328.75µs | |
Running 1 test for test/Counter.t.sol:TestShort_StringConcat | |
[PASS] test1() (gas: 4373) | |
Logs: | |
462 | |
helloworld | |
Test result: ok. 1 passed; 0 failed; finished in 328.71µs | |
Running 1 test for test/Counter.t.sol:TestLong_StringConcat | |
[PASS] test1() (gas: 5032) | |
Logs: | |
745 | |
hello hello hello hello hello hello hello hello hello hello hello hello hello hello helloworld world world world world world world world world world world world world world world | |
Test result: ok. 1 passed; 0 failed; finished in 277.92µs | |
Running 1 test for test/Counter.t.sol:TestLong_EncodePacked | |
[PASS] test1() (gas: 5032) | |
Logs: | |
745 | |
hello hello hello hello hello hello hello hello hello hello hello hello hello hello helloworld world world world world world world world world world world world world world world | |
Test result: ok. 1 passed; 0 failed; finished in 339.21µs |
This file contains hidden or 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: MIT | |
pragma solidity 0.8.19; | |
import "forge-std/Test.sol"; | |
contract TestShort_EncodePacked is Test { | |
function test1() public view { | |
string memory x = "hello"; | |
string memory y = "world"; | |
uint256 startGas = gasleft(); | |
string memory z = string(abi.encodePacked(x, y)); | |
console2.log(startGas - gasleft()); | |
console2.log(z); | |
} | |
} | |
contract TestShort_StringConcat is Test { | |
function test1() public view { | |
string memory x = "hello"; | |
string memory y = "world"; | |
uint256 startGas = gasleft(); | |
string memory z = string.concat(x, y); | |
console2.log(startGas - gasleft()); | |
console2.log(z); | |
} | |
} | |
contract TestLong_EncodePacked is Test { | |
function test1() public view { | |
string memory x = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello"; | |
string memory y = "world world world world world world world world world world world world world world world"; | |
uint256 startGas = gasleft(); | |
string memory z = string(abi.encodePacked(x, y)); | |
console2.log(startGas - gasleft()); | |
console2.log(z); | |
} | |
} | |
contract TestLong_StringConcat is Test { | |
function test1() public view { | |
string memory x = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello"; | |
string memory y = "world world world world world world world world world world world world world world world"; | |
uint256 startGas = gasleft(); | |
string memory z = string.concat(x, y); | |
console2.log(startGas - gasleft()); | |
console2.log(z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment