DEV Community

tumf
tumf

Posted on

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 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
Enter fullscreen mode Exit fullscreen mode

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,
    },
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Solidity optimizer off

The result looks like this:

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

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%)
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)