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 #167

Open
code423n4 opened this issue May 23, 2022 · 0 comments
Open

Gas Optimizations #167

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

Comments

@code423n4
Copy link
Contributor

Gas Optimizations

Use unchecked{++i} instead of i++ in loops

When using i++ there is an extra operation that can be skipped if you use unchecked{++i}. This saves gas every iteration.

You can also use assembly to save much more gas. This may be challenging though in some for loops due to limitations of inline assembly, however this will save much more gas when applicable.

Examples

Occurrence0

Occurrence1

Occurrence2

Occurrence3

Occurrence4

Occurrence5

Occurrence6

Occurrence7

Occurrence8

Occurrence9

Occurrence10

Occurrence11

Occurrence12

Occurrence13

Occurrence14

Occurrence15

^^ many other instances like this throughout the codebase.

Optimization

contract Unoptimized {
    function unoptimizedGasTest(uint256[] calldata _toPids) public pure {
        uint256 j = 0;
        for (uint256 i = 0; i < _toPids.length; i++) {
            j++;
        }
    }
}

contract Optimized {
    function optimizedGasTest(uint256[] calldata _toPids) public pure {
        uint256 j = 0;
        for (uint256 i = 0; i < _toPids.length;) {
            j++;

            unchecked{
                ++i;
            }
        }
    }
}


contract OptimizedAssembly {
    function optimizedAssemblyGasTest(uint256[] calldata _toPids) public pure {
        uint256 j = 0;
        assembly {
            for {let i := 0} lt(i, _toPids.length) {i := add(i, 0x01)} {
                //code goes here
                j := add(j, 0x01)
            }
        }
    }
}

Gas report

╭──────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Unoptimized contract                                             
╞══════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost       Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 65917                 361                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    800              800  800     800  1       
╰──────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Optimized contract                                             
╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost     Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 60311               333                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    658              658  658     658  1       
╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
╭────────────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 OptimizedAssembly contract                                             
╞════════════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost             Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 51299                       288                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name               min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedAssemblyGasTest    534              534  534     534  1       
╰────────────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯

Use Solmate ERC20 vs Openzeppelin

Link to Solmate ERC20

Use assembly to check for address(0) or any other 0 value

When requiring that an address is not address(0), instead of using require(_address!=address(0)), you can use assembly to save gas. You can package the optimized assembly logic below into a function and call it instead of the require statement.
One limitation to this approach is that there is a limit to the error message length that you can pass in, however this forces you to make the revert message 32 bytes, ensuring that only one 256 bit slot is used.

Examples

Occurence0

Occurrence1

Occurrence2

^^ many other instances like this throughout the codebase.

Optimization

contract Unoptimized {
    function unoptimizedGasTest(address owner) public view {
        require(owner != address(0), "ERC4626: approve from address(0)");
    }
}

contract Optimized {
    function optimizedGasTest(address owner) public view {
        assembly {
            if iszero(owner) {
                mstore(0x00, "ERC4626: approve from address(0)")
                revert(0x00, 0x20)
            }
        }
    }
}

Gas report

╭──────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Unoptimized contract                                             
╞══════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost       Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 50899                 285                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    282              282  282     282  1       
╰──────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Optimized contract                                             
╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost     Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 42293               242                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    264              264  264     264  1       
╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯

Use assembly to check if msg.sender == owner (or check any stored value)

When requiring that the msg.sender is the owner of the contract, use assembly instead of a require statement. You can package this logic into a function and call the function instead of the require statement.
Using assembly to check any stored value will save gas.

Examples

Occurrence0

Occurrence1

Occurrence2

Occurrence3

Occurrence4

Occurrence5

Occurrence6

^^ many other instances like this throughout the codebase.

Optimization

contract Unoptimized {
    address owner = 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84;

    function unoptimizedGasTest() public view {
        require(msg.sender == owner, "!auth");
    }
}

contract Optimized {
    address owner = 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84;

    function optimizedGasTest() public view {
        assembly {
            if iszero(eq(sload(owner.slot), caller())) {
                mstore(0x00, "!auth")
                revert(0x00, 0x20)
            }
        }
    }
}

Gas report

╭──────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Unoptimized contract                                               
╞══════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost       Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 57823                 247                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    2262             2262  2262    2262  1       
╰──────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯
╭────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Optimized contract                                               
╞════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost     Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 49217               203                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    2244             2244  2244    2244  1       
╰────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯

Use assembly to update state variables

Using assembly to update the stored value will save gas with sstore(owner.slot, newOwnerAddress) instead of owner = newOwner;.

Examples

Occurrence0

Occurrence1

Occurrence2

^^ many other instances like this throughout the codebase.

Optimization

contract Unoptimized {
    address owner = 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84;

    function unoptimizedGasTest(address newOwner) public {
        owner = newOwner;
    }
}

contract Optimized {
    address owner = 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84;

    function optimizedGasTest(address newOwner) public {
        assembly {
            sstore(owner.slot, newOwner)
        }
    }
}

Gas report

╭──────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Unoptimized contract                                               
╞══════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost       Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 60623                 261                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    5302             5302  5302    5302  1       
╰──────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯
╭────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Optimized contract                                               
╞════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost     Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 54823               232                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    5236             5236  5236    5236  1       
╰────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯

Break up one multi-condition require statement into multiple require statements

When checking multiple conditions in a single require statement, break it up into multiple require statements.

Examples

Occurrence0

Occurrence1

Occurrence2

Occurrence3

Occurrence4

Occurrence5

Optimization

contract Unoptimized {
    address check0 = 0x158B28A1b1CB1BE12C6bD8f5a646a0e3B2024734;
    address check1 = 0x158B28A1b1CB1BE12C6bD8f5a646a0e3B2024734;
    uint256 check2 = 100;

    function unoptimizedGasTest() public returns (uint256 incr) {
        require(check0 != address(0) && check1 != address(0) && check2 > 50);
    }
}

contract Optimized {
    address check0 = 0x158B28A1b1CB1BE12C6bD8f5a646a0e3B2024734;
    address check1 = 0x158B28A1b1CB1BE12C6bD8f5a646a0e3B2024734;
    uint256 check2 = 100;

    function optimizedGasTest() public returns (uint256 incr) {
        require(check0 != address(0));
        require(check1 != address(0));
        require(check2 > 50);
    }
}

Gas report

╭──────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Unoptimized contract                                               
╞══════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost       Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 103068                273                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    6594             6594  6594    6594  1       
╰──────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯
╭────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 Optimized contract                                               
╞════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost     Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 102668              271                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    6566             6566  6566    6566  1       
╰────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯

You don't have to use the Safemath library when you are using Solidity version >= 0.8.0

Solidity >= 0.8.0 uses safe math by default. Using Safemath on top of versions >= 0.8.0 is redundant.

contract Unoptimized {
    using SafeMath for uint256;

    function unoptimizedGasTest(uint256 a, uint256 b) public {
        uint256 c = a.add(b);
    }
}

contract Optimized {
    function optimizedGasTest(uint256 a, uint256 b) public {
        uint256 c = a + b;
    }
}

Gas report

╭──────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Unoptimized contract                                             
╞══════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost       Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 43893                 250                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    348              348  348     348  1       
╰──────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Optimized contract                                             
╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost     Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 40493               233                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    303              303  303     303  1       
╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯

Pack structs by putting data types in ascending size

When defining a struct, pack the values so that the data types are in ascending order. This will make sure that data types that can be put into the same slot are packed together instead of each variable having a separate storage slot.

Examples

Occurrence0

Occurrence1

Occurrence2

Occurrence3

Optimization

contract Unoptimized {

    struct NewStruct {
        uint112 data0;
        uint256 data1;
        uint8 data2;
    }

}

contract Optimized {

    struct NewStruct {
        uint8 data0;
        uint112 data1;
        uint256 data2;
    }

}

Use assembly for add, sub, mul, div and other math library operations

In the Aura math lib, use assembly instead of solidity to save gas during arithmetic.

Examples

Occurrences

Optimization

contract Unoptimized {
    function unoptimizedGasTest(uint256 a, uint256 b) public {
        uint256 c = a + b;
    }
}

contract Optimized {
    function optimizedGasTest(uint256 a, uint256 b) public {
        assembly {
            let c := add(a, b)
        }
    }
}

Gas report

╭──────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Unoptimized contract                                             
╞══════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost       Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 40493                 233                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 unoptimizedGasTest    303              303  303     303  1       
╰──────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 Optimized contract                                             
╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost     Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 29881               179                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name       min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 optimizedGasTest    214              214  214     214  1       
╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯

Use assembly for merkle proof verification

Instead of using openzeppelin's merkle proof verification, use assembly to save significant gas.

Examples

Occurrences

Optimization

Here is a link to my optimized merkle proof verification implementation

Gas report

╭──────────────────────┬─────────────────┬──────┬────────┬──────┬─────────╮
 OZ MerkleProof                                                     
╞══════════════════════╪═════════════════╪══════╪════════╪══════╪═════════╡
 Deployment Cost       Deployment Size                              
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 105953                561                                          
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name         min              avg   median  max   # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 verify                1437             1437  1437    1437  1       
╰──────────────────────┴─────────────────┴──────┴────────┴──────┴─────────╯
╭───────────────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
 OptimizedMerkleProof                                                      
╞═══════════════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
 Deployment Cost                Deployment Size                            
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 66517                          364                                        
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 Function Name                  min              avg  median  max  # calls 
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
 verifyProof                    893              893  893     893  1       
╰───────────────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 23, 2022
code423n4 added a commit that referenced this issue May 23, 2022
@0xMaharishi 0xMaharishi added the duplicate This issue or pull request already exists label May 28, 2022
@dmvt dmvt removed the duplicate This issue or pull request already exists label Jun 25, 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

3 participants