Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #94

Open
code423n4 opened this issue Jul 18, 2022 · 0 comments
Open

Gas Optimizations #94

code423n4 opened this issue Jul 18, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

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:

  • contracts/BytesUtils.sol:266
  • contracts/dnssec-oracle/DNSSECImpl.sol:93
  • contracts/dnssec-oracle/algorithms/EllipticCurve.sol:304
  • contracts/ethregistrar/BulkRenewal.sol:41,56
  • contracts/ethregistrar/ETHRegistrarController.sol:256

<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:

  • contracts/dnssec-oracle/DNSSECImpl.sol:93
  • contracts/ethregistrar/BulkRenewal.sol:41,56
  • contracts/ethregistrar/ETHRegistrarController.sol:256

Many thanks!

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 18, 2022
code423n4 added a commit that referenced this issue Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant