Skip to content

Instantly share code, notes, and snippets.

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

InvertIfCondVsNot_i.sol

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

/**
 * @title InvertIfCondVsNot
 * @author IllIllI
 */
contract InvertIfCondVsNot {
    function test(bool b) external view returns (uint256) {
        return !b ? block.number : uint256(uint160(msg.sender));
    }
}

InvertIfCondVsNot_n.sol

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

/**
 * @title InvertIfCondVsNot
 * @author IllIllI
 */
contract InvertIfCondVsNot {
    function test(bool b) external view returns (uint256) {
        return b ? uint256(uint160(msg.sender)) : block.number;
    }
}

Diff

diff --git a/InvertIfCondVsNot_i.sol b/InvertIfCondVsNot_n.sol
index 5f290bc..2af9617 100644
--- a/InvertIfCondVsNot_i.sol
+++ b/InvertIfCondVsNot_n.sol
@@ -7,7 +7,7 @@ pragma solidity ^0.8.0; // optimize 200
  */
 contract InvertIfCondVsNot {
     function test(bool b) external view returns (uint256) {
-        return !b ? block.number : uint256(uint160(msg.sender));
+        return b ? uint256(uint160(msg.sender)) : block.number;
     }
 }
 

Compiled binary

$ solc InvertIfCondVsNot_i.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060c88061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806336091dff14602d575b600080fd5b603c60383660046064565b6050565b604051604791906089565b60405180910390f35b60008115605c5733605e565b435b92915050565b6000602082840312156074578081fd5b813580151581146082578182fd5b9392505050565b9081526020019056fea2646970667358221220d3c09e7858bbfd32b0a37527592c4eba6ad0417317a7020b8097814d96678bf264736f6c63430008000033

$ solc InvertIfCondVsNot_n.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806336091dff14602d575b600080fd5b603c60383660046063565b6050565b604051604791906088565b60405180910390f35b600081605b5743605d565b335b92915050565b6000602082840312156073578081fd5b813580151581146081578182fd5b9392505050565b9081526020019056fea26469706673582212203fe4322ea75d9e3d96550591874328426952bae47d304ca80f2117641eac66f564736f6c63430008000033

Opcodes

PC.Inv Operation.Inv Gas.Inv PC.Not Operation.Not Gas.Not
112(0x70) PUSH1(0x60) ["0x00"] 3 112(0x70) PUSH1(0x60) ["0x00"] 3
114(0x72) DUP2(0x81) 3 114(0x72) DUP2(0x81) 3
115(0x73) ISZERO(0x15) 3
116(0x74) PUSH1(0x60) ["0x5c"] 3 115(0x73) PUSH1(0x60) ["0x5b"] 3
118(0x76) JUMPI(0x57) 10 117(0x75) JUMPI(0x57) 10
118(0x76) NUMBER(0x43) 2
119(0x77) CALLER(0x33) 2
120(0x78) PUSH1(0x60) ["0x5e"] 3 119(0x77) PUSH1(0x60) ["0x5d"] 3
122(0x7a) JUMP(0x56) 8 121(0x79) JUMP(0x56) 8
123(0x7b) JUMPDEST(0x5b)@0x7b 1 122(0x7a) JUMPDEST(0x5b)@0x7a 1
123(0x7b) CALLER(0x33) 2
124(0x7c) NUMBER(0x43) 2
125(0x7d) JUMPDEST(0x5b)@0x7d 1 124(0x7c) JUMPDEST(0x5b)@0x7c 1
126(0x7e) SWAP3(0x92) 3 125(0x7d) SWAP3(0x92) 3
127(0x7f) SWAP2(0x91) 3 126(0x7e) SWAP2(0x91) 3
128(0x80) POP(0x50) 2 127(0x7f) POP(0x50) 2
129(0x81) POP(0x50) 2 128(0x80) POP(0x50) 2
130(0x82) JUMP(0x56) 8 129(0x81) JUMP(0x56) 8
Total.Inv: 57 Total.Not: 54

The change provides a savings of 3 gas

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