You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello!
CRYP70 here, thank you so much for allowing me to participate in the ENS contest. I have found a couple of valid gas optimisations for this platform which may help keep platform transaction prices down - please see my findings below. :)
++i saves more gass than i++
++i generally costs less gas than i++ or i = i + 1 (about 5 units per increment) because i++ must increment a value and then "return" the old value which means the program may need to hold two numbers in memory. When ++i is used, it will only ever use one number in memory.
See the example below for an simplified illustration:
pragma solidity ^0.8.13;
contract MyFavouriteCounter {
uint public count;
function incrementPrefixCount() public returns (uint) {
count = 1;
return (++count); // returns 2
}
function incrementPostfixCount() public returns (uint) {
count = 1;
return (count++); // returns 1
}
}
I managed to identify this in the following locations:
<array>.length can be cached as opposed to looking up the length with each interation
<array>.length can be cached in a variable so the program doesn't need to spend additional gas to determine the length of the target array. I believe memory arrays use 3 gas units through the mload opcode and calldata arrays will use 3 gas units via the calldataload opcode. See the very simple example below with resulting gas units spent (note that this is excluding gas optimisation of the index incrementer and includes gas price spent after reading from storage for both function calls).
pragma solidity 0.8.13;
contract MyFavouriteLooper {
string[] testarr = ["a", "b"];
function stored() public { // gas units spent: 23713
uint256 len = testarr.length;
for(uint256 i = 0; i < len; i++){
}
}
function notstored() public { // gas units spent: 27479
for(uint256 i = 0; i < testarr.length; i++){
}
}
}
I managed to identify this in the following solidity files:
Hello!
CRYP70 here, thank you so much for allowing me to participate in the ENS contest. I have found a couple of valid gas optimisations for this platform which may help keep platform transaction prices down - please see my findings below. :)
++i
saves more gass thani++
++i
generally costs less gas thani++
ori = i + 1
(about 5 units per increment) becausei++
must increment a value and then "return" the old value which means the program may need to hold two numbers in memory. When++i
is used, it will only ever use one number in memory.See the example below for an simplified illustration:
I managed to identify this in the following locations:
<array>.length
can be cached as opposed to looking up the length with each interation<array>.length
can be cached in a variable so the program doesn't need to spend additional gas to determine the length of the target array. I believe memory arrays use 3 gas units through themload
opcode and calldata arrays will use 3 gas units via thecalldataload
opcode. See the very simple example below with resulting gas units spent (note that this is excluding gas optimisation of the index incrementer and includes gas price spent after reading from storage for both function calls).I managed to identify this in the following solidity files:
Many thanks!
The text was updated successfully, but these errors were encountered: