Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Changes from 3 commits
b7de50c
9249a0b
329dc22
175b1e9
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
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 theinitializing
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.
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 SSTOREThere 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
If do it like
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.
Upd: yes, in this case my proposal should be rather:
And here:
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?