Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active September 5, 2023 03:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IllIllI000/cbbfb267425b898e5be734d4008d4fe8 to your computer and use it in GitHub Desktop.
Save IllIllI000/cbbfb267425b898e5be734d4008d4fe8 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 slot0;
    uint256 varr = 1;

    function test(uint256 x) external returns (uint256) {
        x *= 2;
        varr += x;
        return block.timestamp + 1;
    }
}

StateVarPlusEqVsEqPlus_e.sol

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

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

    function test(uint256 x) external returns (uint256) {
        x *= 2;
        varr = varr + x;
        return block.timestamp + 1;
    }
}

Diff

diff --git a/StateVarPlusEqVsEqPlus_p.sol b/StateVarPlusEqVsEqPlus_e.sol
index 45d881b..6aa1d8f 100644
--- a/StateVarPlusEqVsEqPlus_p.sol
+++ b/StateVarPlusEqVsEqPlus_e.sol
@@ -11,7 +11,7 @@ contract StateVarPlusEqVsEqPlus {
 
     function test(uint256 x) external returns (uint256) {
         x *= 2;
-        varr += x;
+        varr = varr + x;
         return block.timestamp + 1;
     }
 }

Compiled binary

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

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

IR Diff

diff --git a/a b/b
index 83e1bb3..dc3b50d 100644
--- a/a
+++ b/b
@@ -7,22 +7,22 @@ IR:
  *******************************************************/
 
 
-object "StateVarPlusEqVsEqPlus_29" {
+object "StateVarPlusEqVsEqPlus_31" {
     code {
         mstore(64, 128)
         if callvalue() { revert(0, 0) }
 
-        constructor_StateVarPlusEqVsEqPlus_29()
+        constructor_StateVarPlusEqVsEqPlus_31()
 
-        codecopy(0, dataoffset("StateVarPlusEqVsEqPlus_29_deployed"), datasize("StateVarPlusEqVsEqPlus_29_deployed"))
+        codecopy(0, dataoffset("StateVarPlusEqVsEqPlus_31_deployed"), datasize("StateVarPlusEqVsEqPlus_31_deployed"))
 
-        return(0, datasize("StateVarPlusEqVsEqPlus_29_deployed"))
+        return(0, datasize("StateVarPlusEqVsEqPlus_31_deployed"))
 
         function cleanup_t_uint256(value) -> cleaned {
             cleaned := value
         }
 
-        function constructor_StateVarPlusEqVsEqPlus_29() {
+        function constructor_StateVarPlusEqVsEqPlus_31() {
 
             let expr_6 := 0x01
             let _1 := convert_t_rational_1_by_1_to_t_uint256(expr_6)
@@ -62,7 +62,7 @@ object "StateVarPlusEqVsEqPlus_29" {
         }
 
     }
-    object "StateVarPlusEqVsEqPlus_29_deployed" {
+    object "StateVarPlusEqVsEqPlus_31_deployed" {
         code {
             mstore(64, 128)
 
@@ -76,7 +76,7 @@ object "StateVarPlusEqVsEqPlus_29" {
                     // test(uint256)
                     if callvalue() { revert(0, 0) }
                     let param_0 :=  abi_decode_tuple_t_uint256(4, calldatasize())
-                    let ret_0 :=  fun_test_28(param_0)
+                    let ret_0 :=  fun_test_30(param_0)
                     let memPos := allocateMemory(0)
                     let memEnd := abi_encode_tuple_t_uint256__to_t_uint256__fromStack(memPos , ret_0)
                     return(memPos, sub(memEnd, memPos))
@@ -167,7 +167,7 @@ object "StateVarPlusEqVsEqPlus_29" {
                 value := cleanup_from_storage_t_uint256(shift_right_0_unsigned(slot_value))
             }
 
-            function fun_test_28(vloc_x_9) -> vloc__12 {
+            function fun_test_30(vloc_x_9) -> vloc__12 {
                 let zero_value_for_type_t_uint256_1 := zero_value_for_split_t_uint256()
                 vloc__12 := zero_value_for_type_t_uint256_1
 
@@ -177,17 +177,19 @@ object "StateVarPlusEqVsEqPlus_29" {
                 _2 := checked_mul_t_uint256(_3, _2)
                 vloc_x_9 := _2
                 let expr_16 := _2
-                let _4 := vloc_x_9
+                let _4 := read_from_storage_split_offset_0_t_uint256(0x01)
                 let expr_19 := _4
-                let _5 := read_from_storage_split_offset_0_t_uint256(0x01)
-                expr_19 := checked_add_t_uint256(_5, expr_19)
-                update_storage_value_offset_0t_uint256_to_t_uint256(0x01, expr_19)
-                let expr_20 := expr_19
-                let expr_23 := timestamp()
-                let expr_24 := 0x01
-                let expr_25 := checked_add_t_uint256(expr_23, convert_t_rational_1_by_1_to_t_uint256(expr_24))
-
-                vloc__12 := expr_25
+                let _5 := vloc_x_9
+                let expr_20 := _5
+                let expr_21 := checked_add_t_uint256(expr_19, expr_20)
+
+                update_storage_value_offset_0t_uint256_to_t_uint256(0x01, expr_21)
+                let expr_22 := expr_21
+                let expr_25 := timestamp()
+                let expr_26 := 0x01
+                let expr_27 := checked_add_t_uint256(expr_25, convert_t_rational_1_by_1_to_t_uint256(expr_26))
+
+                vloc__12 := expr_27
                 leave
 
             }

Opcodes

PC.Pe Operation.Pe Gas.Pe PC.Ep Operation.Ep Gas.Ep
130(0x82) DUP2(0x81) 3 130(0x82) DUP2(0x81) 3
131(0x83) PUSH1(0x60) ["0x01"] 3 131(0x83) PUSH1(0x60) ["0x01"] 3
133(0x85) PUSH1(0x60) ["0x00"] 3
135(0x87) DUP3(0x82) 3
136(0x88) DUP3(0x82) 3
137(0x89) SLOAD(0x54) 800 133(0x85) SLOAD(0x54) 800
138(0x8a) PUSH1(0x60) ["0x6d"] 3 134(0x86) PUSH1(0x60) ["0x69"] 3
140(0x8c) SWAP2(0x91) 3 136(0x88) SWAP2(0x91) 3
141(0x8d) SWAP1(0x90) 3 137(0x89) SWAP1(0x90) 3
142(0x8e) PUSH1(0x60) ["0xa2"] 3 138(0x8a) PUSH1(0x60) ["0x9d"] 3
144(0x90) JUMP(0x56) 8 140(0x8c) JUMP(0x56) 8
145(0x91) JUMPDEST(0x5b)@0x91 1 141(0x8d) JUMPDEST(0x5b)@0x8d 1
142(0x8e) PUSH1(0x60) ["0x01"] 3
146(0x92) SWAP1(0x90) 3 144(0x90) SWAP1(0x90) 3
145(0x91) DUP2(0x81) 3
147(0x93) SWAP2(0x91) 3
148(0x94) SSTORE(0x55) 20000 146(0x92) SSTORE(0x55) 20000
149(0x95) POP(0x50) 2
150(0x96) PUSH1(0x60) ["0x7c"] 3 147(0x93) PUSH1(0x60) ["0x77"] 3
152(0x98) SWAP1(0x90) 3 149(0x95) SWAP1(0x90) 3
153(0x99) POP(0x50) 2
154(0x9a) TIMESTAMP(0x42) 2 150(0x96) TIMESTAMP(0x42) 2
151(0x97) SWAP1(0x90) 3
155(0x9b) PUSH1(0x60) ["0x01"] 3 152(0x98) PUSH1(0x60) ["0x9d"] 3
157(0x9d) PUSH1(0x60) ["0xa2"] 3
159(0x9f) JUMP(0x56) 8 154(0x9a) JUMP(0x56) 8
Total.Pe: 20868 Total.Ep: 20858

The change provides a savings of 10 gas

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