code-423n4 / 2022-02-aave-lens-findings

0 stars 0 forks source link

Gas Optimizations #75

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. Title : multiple .length call on byteHandle

Summary

It is cheaper to save the length value of the byteHandle in a local variable than call the .length in every if condition and loop

POC https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/libraries/PublishingLogic.sol#L400-L403

2. Title : many function calculation called with the same result

Summary

this function called many times when user interacting with LensHub. therefore it is cheaper to save the value to a variable in constructor

POC https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/base/LensNFTBase.sol#L173

3. Title : many boolean operation with the same result

Summary

It is cheaper to save boolean operation result in the local memory when It is going to be used many times.

POC https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L231-L264

before

function _moveDelegate( address from, address to, uint256 amount ) internal {
        unchecked {
            if (from != address(0)) {
        //statement
        }
        if (to != address(0)) {
            if (from == address(0)) {
            //statement
        }
        }
        else{
            if(from != address(0)){
            //statement
        }
        }
    }
    }

after

function _moveDelegate( address from, address to, uint256 amount ) internal {
        unchecked {
        bool fromZero = from == address(0);
            if (!fromZero) {
        //statement
        }
        if (to != address(0)) {
            if (fromZero) {
             //statement
        }
        }
        else{
            if(!fromZero){
             //statement
        }
        }
    }
    }
Zer0dot commented 2 years ago

Great! Except for the 2nd point, although this is valid, we're opting to keep the domain separator calculation as is, it's future-proof, but perhaps down the line in a contract upgrade we can optimize this. All in this PR: https://github.com/aave/lens-protocol/pull/80