1

Take the following code:

pragma solidity >=0.8.19 <0.9.0;

contract Foo { uint256 public constant SIZE = 10;

function func(uint256 x) external pure returns (uint256) {
    uint256 y = SIZE * 3;
    return x + y;
}

}

Since this function doesn't wrap the calculations in an unchecked block, x + y is calculated using checked arithmetic.

But will SIZE * 3 also be calculated like that? Or will Solidity inline this value (30 in this case) in the contract's bytecode because SIZE is a constant?

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
  • 1
    I would think the solidity compiler might optimize this if it is compiled with the --optimize flag with solc https://docs.soliditylang.org/en/v0.8.17/internals/optimizer.html, https://github.com/ethereum/solidity/blob/develop/libevmasm/RuleList.h – Bruce May 25 '23 at 22:55

0 Answers0