Gas Optimization in Solidity Smart Contract

During deployment of a contract gas cost play a vital role. It increase the cost of a deployment cost. we can reduce it via inline assembly language check below example.

Without inline assembly the gas cost of the below contract is 106299:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract optimize{
    uint x;
    uint y;
    
    function set(uint _x, uint _y)public {
        x = _x;
        y = _y;
    }
}

With Assembly Language the gas cost of contract is: 105003

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract optimize{
    uint x;
    uint y;
    
    function set(uint _x, uint _y)public {
        assembly{
            sstore(x.slot,_x)
            sstore(y.slot,_y)
        }
    }
}

By Shabair

Frontend | Blockchain | Smart Contract Developer