To my understanding, running optimization on a contract will remove unused global storage variables from contracts. Is this correct?
If so, would this cause issues with upgradeability?
- Deploy an optimized upgradeable contract Version1 which does not use a certain storage variable
- Create a new implementation that does use the storage variable
- Deploy that optimized contract which now has the formerly unused variable.
Same to the reverse, in which a formerly used storage variable
oldFoo is used in optimized Version1 but not in optimized Version2.
If it's untrue that solc optimization removes unused global storage variables then this question is moot, but I can't find anything which definitively says yes or no in the docs: https://docs.soliditylang.org/en/v0.8.12/internals/optimizer.html
A more general and perhaps more useful question for the community:
Does optimization configuration affect upgradeability at all?
E.g. Deploying Version1 with optimization --runs=200, and later Version2 with optimization --runs=1000 as you discover more about gas usage on your contract.
Unused global storage variables do not contribute to gas costs and aren't "removed" by optimization. You can test this out to verify this e.g. with Remix.
For example, OpenZeppelin Contracts Upgradeable uses storage gaps which are reserved unused storage variables, and can be compiled with optimizations.
Hi, @ericglau , a quick additional question while we are in the matter of optimization..
I noticed that increasing optimizations runs from 200 to example 10000 only affects the implementation adres.. The proxy contracts remain in 200 runs.
Is there a reason for that, is there a need to optimize additional runs for proxy contracts if so how do I specify that.
Have been wondering for a while.. I´m hoping that doing 10000 optimization runs for a contract that will be called very often would make it cheaper to interact with in the long run.
Thanks in advance.
The proxy contracts themselves are precompiled in the Upgrades Plugins with 200 optimize runs, and those precompiled proxy contracts are what get deployed by the plugins. We don't think increasing that number would provide any benefit for the proxies.