Skip to content

Gas optimization#227

Open
molly-ting wants to merge 1 commit into
compound-finance:masterfrom
molly-ting:master
Open

Gas optimization#227
molly-ting wants to merge 1 commit into
compound-finance:masterfrom
molly-ting:master

Conversation

@molly-ting

Copy link
Copy Markdown

If a state variable is read immediately after assignment, then replacing this read directly with the previously assigned local variable will change one SLOAD to one MLOAD to save some gases. In the following example, function test0 costs 188 more gases than function test1 and function test2 costs 203 more gases than function test3.

contract Demo {
    uint public sa;
    address public add;

    event opUpdate(uint b);
    event addUpdate(address c);

    function test0(uint va) public {
        sa = va;
        emit opUpdate(sa);
    }

    function test1(uint va) public {
        sa = va;
        emit opUpdate(va);
    }

    function test2() public {
        add = msg.sender;
        emit addUpdate(add);
    }

    function test3() public {
        add = msg.sender;
        emit addUpdate(msg.sender);
    }
}

@Tcola0f

Tcola0f commented Nov 13, 2022

Copy link
Copy Markdown

sorry

@songlh songlh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch is good to me. Clearly, it can save some gases.

@songlh

songlh commented Nov 15, 2022

Copy link
Copy Markdown

@jflatow can you take a look at this pull request?

@Tcola0f

Tcola0f commented Nov 15, 2022

Copy link
Copy Markdown

please disregard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants