-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Initializable readability using intermediate variables #4576
Improve Initializable readability using intermediate variables #4576
Conversation
🦋 Changeset detectedLatest commit: 175b1e9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: Francisco <fg@frang.io>
Co-authored-by: Francisco <fg@frang.io>
bool initialSetup = initialized == 0 && isTopLevelCall; | ||
bool construction = initialized == 1 && address(this).code.length == 0; | ||
|
||
if (!initialSetup && !construction) { | ||
revert AlreadyInitialized(); | ||
} | ||
$._initialized = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$._initialized = 1;
$._initializing = isTopLevelCall;
May be cheaper here, removing redundant SLOAD operation in case of !isTopLevelCall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand your proposal. Before doing _;
, we want to make sure the initializing
is true, this would possibly invert it to false.
Also I'm not sure where the duplicated sload is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I'm not sure where the duplicated sload is.
Since $
is a struct with two fields packed in one storage slot, compiler need to load this slot if you change only one field. If you write all the fields of the structure at once, the compiler does not need to read this slot before SSTORE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it could (in theory) save around 100 gas here
openzeppelin-contracts/contracts/proxy/utils/Initializable.sol
Lines 122 to 125 in 175b1e9
$._initialized = 1; | |
if (isTopLevelCall) { | |
$._initializing = true; | |
} |
If do it like
$._initialized = 1;
$._initializing = isTopLevelCall;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would possibly invert it to false
Upd: yes, in this case my proposal should be rather:
$._initialized = 1;
$._initializing = true;
And here:
$._initialized = 1;
$._initializing = false;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing it that way requires proving that the _;
part doesn't change the value of $._initialized
.
I think it is true, because $._initializing = true
prevents that value from changing in the rest of the contract, but it's more difficult to see.
I think I prefer to keep the current code at this stage (audit fixes) and come back to this for a future release. @vladyan18 can you open an issue?
Fixes LIB-1041
PR Checklist
npx changeset add
)