Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active June 24, 2023 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IllIllI000/bf2c3120f24a69e489f12b3213c06c94 to your computer and use it in GitHub Desktop.
Save IllIllI000/bf2c3120f24a69e489f12b3213c06c94 to your computer and use it in GitHub Desktop.

0.8.12

RequireGtVsNe_g.sol

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

/**
 * @title RequireGtVsNe
 * @author IllIllI
 */
contract RequireGtVsNe {
    uint256 varr;
    function test () external {
        require(varr > 0);
        varr = uint256(uint160(msg.sender));
    }
}

RequireGtVsNe_n.sol

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

/**
 * @title RequireGtVsNe
 * @author IllIllI
 */
contract RequireGtVsNe {
    uint256 varr;
    function test () external {
        require(varr != 0);
        varr = uint256(uint160(msg.sender));
    }
}

Diff

diff --git a/RequireGtVsNe_g.sol b/RequireGtVsNe_n.sol
index 34a7755..9f2a8c6 100644
--- a/RequireGtVsNe_g.sol
+++ b/RequireGtVsNe_n.sol
@@ -8,7 +8,7 @@ pragma solidity 0.8.12; // optimize 200
 contract RequireGtVsNe {
     uint256 varr;
     function test () external {
-        require(varr > 0);
+        require(varr != 0);
         varr = uint256(uint160(msg.sender));
     }
 }

Compiled binary

$ solc RequireGtVsNe_g.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052348015600f57600080fd5b50607e8061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336035565b005b6000805411604257600080fd5b3360005556fea2646970667358221220a2bdc2c10199699efab540153e43368388f342cef9ebad935f99c02a5f9b18dc64736f6c634300080c0033

$ solc RequireGtVsNe_n.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052348015600f57600080fd5b50607c8061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336035565b005b600054604057600080fd5b3360005556fea2646970667358221220c0c86608a0253b22ff5079617574397462b988f92249f92d252928d9b86b7c7064736f6c634300080c0033

Opcodes

PC.Gt Operation.Gt Gas.Gt PC.Ne Operation.Ne Gas.Ne
83(0x53) JUMPDEST(0x5b)@0x53 1 83(0x53) JUMPDEST(0x5b)@0x53 1
84(0x54) PUSH1(0x60) ["0x00"] 3 84(0x54) PUSH1(0x60) ["0x00"] 3
86(0x56) DUP1(0x80) 3
87(0x57) SLOAD(0x54) 800 86(0x56) SLOAD(0x54) 800
88(0x58) GT(0x11) 3
89(0x59) PUSH1(0x60) ["0x42"] 3 87(0x57) PUSH1(0x60) ["0x40"] 3
91(0x5b) JUMPI(0x57) 10 89(0x59) JUMPI(0x57) 10
Total.Gt: 823 Total.Ne: 817

The change provides a savings of 6 gas

0.8.13

RequireGtVsNe_g.sol

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

/**
 * @title RequireGtVsNe
 * @author IllIllI
 */
contract RequireGtVsNe {
    uint256 varr;
    function test () external {
        require(varr > 0);
        varr = uint256(uint160(msg.sender));
    }
}

RequireGtVsNe_n.sol

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

/**
 * @title RequireGtVsNe
 * @author IllIllI
 */
contract RequireGtVsNe {
    uint256 varr;
    function test () external {
        require(varr != 0);
        varr = uint256(uint160(msg.sender));
    }
}

Diff

diff --git a/RequireGtVsNe_g.sol b/RequireGtVsNe_n.sol
index da482a3..d288397 100644
--- a/RequireGtVsNe_g.sol
+++ b/RequireGtVsNe_n.sol
@@ -8,7 +8,7 @@ pragma solidity 0.8.13; // optimize 200
 contract RequireGtVsNe {
     uint256 varr;
     function test () external {
-        require(varr > 0);
+        require(varr != 0);
         varr = uint256(uint160(msg.sender));
     }
 }

Compiled binary

$ solc RequireGtVsNe_g.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052348015600f57600080fd5b50607e8061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336035565b005b6000805411604257600080fd5b3360005556fea264697066735822122023e7ecec0d879c536d1404b1720a0105b1d3497a91a413641f5fa7f177a2e6fe64736f6c634300080d0033

$ solc RequireGtVsNe_n.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
6080604052348015600f57600080fd5b50607f8061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f8a8fd6d14602d575b600080fd5b60336035565b005b600054600003604357600080fd5b3360005556fea2646970667358221220b524bc5f817e5755a1c803d5770038d54b179591b1d038022f8026982a09b97164736f6c634300080d0033

Opcodes

PC.Gt Operation.Gt Gas.Gt PC.Ne Operation.Ne Gas.Ne
83(0x53) JUMPDEST(0x5b)@0x53 1 83(0x53) JUMPDEST(0x5b)@0x53 1
84(0x54) PUSH1(0x60) ["0x00"] 3 84(0x54) PUSH1(0x60) ["0x00"] 3
86(0x56) DUP1(0x80) 3
87(0x57) SLOAD(0x54) 800 86(0x56) SLOAD(0x54) 800
87(0x57) PUSH1(0x60) ["0x00"] 3
89(0x59) SUB(0x3) 3
88(0x58) GT(0x11) 3
89(0x59) PUSH1(0x60) ["0x42"] 3 90(0x5a) PUSH1(0x60) ["0x43"] 3
91(0x5b) JUMPI(0x57) 10 92(0x5c) JUMPI(0x57) 10
Total.Gt: 823 Total.Ne: 823

The change provides a savings of zero gas

This is a regression in gas savings when going from 0.8.12 to 0.8.13. The PUSH1(0x60) and SUB(0x3) appear to be unnecessary

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