openzeppelin-contracts icon indicating copy to clipboard operation
openzeppelin-contracts copied to clipboard

Redundant SLOADs in Initializable

Open 0xVolosnikov opened this issue 2 years ago • 0 comments

Opening issue as mentioned in https://github.com/OpenZeppelin/openzeppelin-contracts/pull/4576#discussion_r1316443220

📝 Details

Since InitializableStorage is a struct with two fields packed in one storage slot, compiler need to load this slot when writing new value only for one of the fields, like here:

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/175b1e9e09b19f90d96b472a0e0b4ebea9a47762/contracts/proxy/utils/Initializable.sol#L127-L130

Since (when) we know values for other fields, explicit usage of known values can let the compiler to prevent excessive SLOADs before SSTORE. Example:

$._initialized = 1;
$._initializing = false;

PS: this is applicable not only in this place, but also in some other contracts.

0xVolosnikov avatar Sep 06 '23 11:09 0xVolosnikov