Does Solidity optimizer avoid changing storage variables?

The original Post is here in Japanese

In developing smart contracts on the blockchain, it is important to reduce the cost of execution, which is the cost of gas. There are several famous tricks to reduce Gas consumption, one of which is “Avoid changi…


This content originally appeared on DEV Community and was authored by tumf

The original Post is here in Japanese

In developing smart contracts on the blockchain, it is important to reduce the cost of execution, which is the cost of gas. There are several famous tricks to reduce Gas consumption, one of which is "Avoid changing storage data". storage data".

For example, the UniswapV2 smart contract has the gas savings trick all over it like this.

Uniswap/v2-core

I'm always careful about this when I write or review smart contracts, but the other day I suddenly thought, "This is such a classic trick, isn't it already solved by Solidity's optimizer?" So I tested it with the latest Solidity compiler 0.8.11.

Tested Smart Contracts

I created a Hardhat project and wrote the following smart contract. complete source code

counter1() is the non-gas-optimized code, counter2() is the gas-optimized code. The difference is that the Storage variable counter is copied to the local variable (memory) j and the result of counting in j is returned to counter.

Test code

The code for gas consumption measurement is as follows:

Measure the gas consumption

I use the useful Hardhat plugin hardhat-gas-reporter to measure the gas bill.

Installation is as follows:

$ yarn add --dev hardhat-gas-reporter

Add require("hardhat-gas-reporter"); to the beginning of hardhat.confg.js as

For configuration, add gasReporter to hardhat.confg.js. If you set the API key of Coinmarketcap to the environment variable CMC_API_KEY here, the cost in USD will be reported.

module.exports = {
    ....
    gasReporter: {
        currency: "USD",
        gasPrice: 100,
        coinmarketcap: process.env.CMC_API_KEY,
    },
};

Verification

Now that we're ready, let's get started with the verification. First, let's run it without using Solidity's optimizer.

$ npx hardhat test

Solidity optimizer off

The result looks like this:

disable Solidity optimizer
without code optimization (counter1): 630,118
with code optimization (counter2): 407,289 (-25%)

It seems that gas is reduced by about 25%, which is almost as expected.

Next, let's turn on Solidity's optimizer (runs:1,000).

Solidity optimizer on

The result is as follows:

enable Solidity optimizer
without code optimization (counter1): 439,118
with code optimization (counter2): 218,271 (-50%)

Both with and without Solidity's optimizer, there is a large gas saving, but the effect of code optimization is still significant.

Conclusion

It turns out that the gas saving trick of "avoid changing storage variables" still works even with the latest Solidity optimizer enabled. We should continue to be careful about changing storage variables.


This content originally appeared on DEV Community and was authored by tumf


Print Share Comment Cite Upload Translate Updates
APA

tumf | Sciencx (2022-02-14T03:12:35+00:00) Does Solidity optimizer avoid changing storage variables?. Retrieved from https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/

MLA
" » Does Solidity optimizer avoid changing storage variables?." tumf | Sciencx - Monday February 14, 2022, https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/
HARVARD
tumf | Sciencx Monday February 14, 2022 » Does Solidity optimizer avoid changing storage variables?., viewed ,<https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/>
VANCOUVER
tumf | Sciencx - » Does Solidity optimizer avoid changing storage variables?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/
CHICAGO
" » Does Solidity optimizer avoid changing storage variables?." tumf | Sciencx - Accessed . https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/
IEEE
" » Does Solidity optimizer avoid changing storage variables?." tumf | Sciencx [Online]. Available: https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/. [Accessed: ]
rf:citation
» Does Solidity optimizer avoid changing storage variables? | tumf | Sciencx | https://www.scien.cx/2022/02/14/does-solidity-optimizer-avoid-changing-storage-variables/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.