Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active June 24, 2023 13:48
Show Gist options
  • Save IllIllI000/e075d189c1b23dce256cd166e28f3397 to your computer and use it in GitHub Desktop.
Save IllIllI000/e075d189c1b23dce256cd166e28f3397 to your computer and use it in GitHub Desktop.

STACK VARIABLES DO NOT SAVE GAS BY SKIPPING INITIALIZATION TO ZERO

If you've found this page via one of my old gas reports, please see this link

SetVsDefault_s.sol

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

/**
 * @title SetVsDefault
 * @author IllIllI
 */
contract SetVsDefault {
    function test(uint256 x) external pure returns(uint256) {
        for (uint256 i = 0; i < 10; ++i) {
	    x += 2;
	}
	return x;
    }
}

SetVsDefault_d.sol

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

/**
 * @title SetVsDefault
 * @author IllIllI
 */
contract SetVsDefault {
    function test(uint256 x) external pure returns(uint256) {
        for (uint256 i; i < 10; ++i) {
	    x += 2;
	}
	return x;
    }
}

Diff

diff --git a/SetVsDefault_s.sol b/SetVsDefault_d.sol
index 7594b22..541d4cd 100644
--- a/SetVsDefault_s.sol
+++ b/SetVsDefault_d.sol
@@ -7,7 +7,7 @@ pragma solidity 0.8.13; // optimize 200
  */
 contract SetVsDefault {
     function test(uint256 x) external pure returns(uint256) {
-        for (uint256 i = 0; i < 10; ++i) {
+        for (uint256 i; i < 10; ++i) {
 	    x += 2;
 	}
 	return x;

Compiled binary

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

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

Opcodes

no bytecode differences

@0xean
Copy link

0xean commented Sep 22, 2022

Adding some more info here for the next person that takes a look. (make sure optimizer is enabled). I wanted to confirm this held for a non pure function. It does.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17; 


contract SetVsDefault1 {
    uint256 randomVar;
    

    function test(uint256 x) external returns(uint256) {
        for (uint256 i = 0; i < 10; ++i) {
	        x = x + 2;
	    }
        randomVar = x;
	    return x;
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17; 


contract SetVsDefault2 {
    uint256 randomVar;
    

    function test(uint256 x) external returns(uint256) {
        for (uint256 i; i < 10; ++i) {
	        x = x + 2;
	    }
        randomVar = x;
	    return x;
    }
}

Screen Shot 2022-09-22 at 8 34 20 AM

@captain-l3vi
Copy link

noob question. How to get that beautiful output?

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