Skip to content

Instantly share code, notes, and snippets.

@MiniGlome
Forked from IllIllI000/StateVarPlusEqVsEqPlus.md
Created January 29, 2023 16:14
Show Gist options
  • Save MiniGlome/f462d69a30f68c89175b0ce24ce37cae to your computer and use it in GitHub Desktop.
Save MiniGlome/f462d69a30f68c89175b0ce24ce37cae to your computer and use it in GitHub Desktop.

StateVarPlusEqVsEqPlus_p.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0; // optimize 200

/**
 * @title StateVarPlusEqVsEqPlus
 * @author IllIllI
 */
contract StateVarPlusEqVsEqPlus {
    uint256 varr = 1;

    function test() external returns (uint256) {
        varr += block.number;
        return varr;
    }
}

StateVarPlusEqVsEqPlus_e.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0; // optimize 200

/**
 * @title StateVarPlusEqVsEqPlus
 * @author IllIllI
 */
contract StateVarPlusEqVsEqPlus {
    uint256 varr = 1;

    function test() external returns (uint256) {
        varr = varr + block.number;
        return varr;
    }
}

Diff

diff --git a/StateVarPlusEqVsEqPlus_p.sol b/StateVarPlusEqVsEqPlus_e.sol
index 31775a1..623d154 100644
--- a/StateVarPlusEqVsEqPlus_p.sol
+++ b/StateVarPlusEqVsEqPlus_e.sol
@@ -9,7 +9,7 @@ contract StateVarPlusEqVsEqPlus {
     uint256 varr = 1;
 
     function test() external returns (uint256) {
-        varr += block.number;
+        varr = varr + block.number;
         return varr;
     }
 }

Compiled binary

$ solc StateVarPlusEqVsEqPlus_p.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052600160005534801561001557600080fd5b5060c7806100246000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336047565b604051603e91906065565b60405180910390f35b60004360008082825460589190606e565b9091555050600054905090565b90815260200190565b60008219821115608c57634e487b7160e01b81526011600452602481fd5b50019056fea264697066735822122084f18c25ca3843d1f0f1ddf7bc90bef1163ea6a5ac7dba8b97f40fa3fd2ef83a64736f6c63430008000033

$ solc StateVarPlusEqVsEqPlus_e.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052600160005534801561001557600080fd5b5060c1806100246000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336047565b604051603e9190605f565b60405180910390f35b600043600054605591906068565b6000819055905090565b90815260200190565b60008219821115608657634e487b7160e01b81526011600452602481fd5b50019056fea2646970667358221220aa5b50873df0045c32f138c5b8119fe52dbd818b3e305f88bbe93079ce8e4cd964736f6c63430008000033

Opcodes

PC.Pe Operation.Pe Gas.Pe PC.Ep Operation.Ep Gas.Ep
110(0x6e) NUMBER(0x43) 2 110(0x6e) NUMBER(0x43) 2
111(0x6f) PUSH1(0x60) ["0x00"] 3 111(0x6f) PUSH1(0x60) ["0x00"] 3
113(0x71) DUP1(0x80) 3
114(0x72) DUP3(0x82) 3
115(0x73) DUP3(0x82) 3
116(0x74) SLOAD(0x54) 2100 113(0x71) SLOAD(0x54) 2100
117(0x75) PUSH1(0x60) ["0x58"] 3 114(0x72) PUSH1(0x60) ["0x55"] 3
119(0x77) SWAP2(0x91) 3 116(0x74) SWAP2(0x91) 3
120(0x78) SWAP1(0x90) 3 117(0x75) SWAP1(0x90) 3
121(0x79) PUSH1(0x60) ["0x6e"] 3 118(0x76) PUSH1(0x60) ["0x68"] 3
123(0x7b) JUMP(0x56) 8 120(0x78) JUMP(0x56) 8
124(0x7c) JUMPDEST(0x5b)@0x7c 1 121(0x79) JUMPDEST(0x5b)@0x79 1
122(0x7a) PUSH1(0x60) ["0x00"] 3
124(0x7c) DUP2(0x81) 3
125(0x7d) SWAP1(0x90) 3 125(0x7d) SWAP1(0x90) 3
126(0x7e) SWAP2(0x91) 3
127(0x7f) SSTORE(0x55) 20000 126(0x7e) SSTORE(0x55) 20000
128(0x80) POP(0x50) 2
129(0x81) POP(0x50) 2
130(0x82) PUSH1(0x60) ["0x00"] 3
132(0x84) SLOAD(0x54) 100
133(0x85) SWAP1(0x90) 3 127(0x7f) SWAP1(0x90) 3
134(0x86) POP(0x50) 2 128(0x80) POP(0x50) 2
135(0x87) SWAP1(0x90) 3 129(0x81) SWAP1(0x90) 3
136(0x88) JUMP(0x56) 8 130(0x82) JUMP(0x56) 8
Total.Pe: 22264 Total.Ep: 22151

The change provides a savings of 113 gas

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