Hi what I noticed is that immutable storage variables are increasing contract size more then normal storage variable. Is this expected behaviour. Probably in terms of gas are more efficient, but I find it strange that immutable variables are increasing contract size more then normal variables.
Can somebody confirm this is normal behavior?
On the other hand constant storage variables are more efficient then both normal and immutable variables, which I expected.
It's probably not the immutable variables by themselves, but the fact that you MUST initialize them in the constructor, i.e., as part of the contract code, which increases its size.
The comparison that you should do is by taking your contract with immutable variable, and simply remove all occurrences of that keyword, while keeping the initialization in place.
I believe that the compiler's mechanism for handling the initialization of an immutable variable, makes the corresponding bytecode slightly larger than that of a storage variable.
An example can be observed by compiling these two contracts:
pragma solidity 0.8.19;
contract ImmutableVariable {
uint256 public immutable x;
constructor() {
x = 42;
}
}
contract StorageVariable {
uint256 public x;
constructor() {
x = 42;
}
}
For which, the compilation outcome is:
·----------------------|------------------------------|------------------------------·
| Solc version: 0.8.19 · Optimizer enabled: true · Runs: 200 │
·······················|······························|·······························
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
·······················|······························|·······························
| ImmutableVariable · 0.111 () · 0.155 () │
·······················|······························|·······························
| StorageVariable · 0.082 () · 0.115 () │
·······················|······························|·······························
One side note with regards to:
There is no such thing.
Constants (I wouldn't even say "constant variables" because that's an oxymoron) are not in storage.
They (i.e., their values) are embedded into the bytecode during the compilation process.