-
Notifications
You must be signed in to change notification settings - Fork 196
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
feat(store): add Storage.loadField for optimized loading of 32 bytes or less from storage #1512
Conversation
🦋 Changeset detectedLatest commit: 28efb9d The changes in this PR will be included in the next version bump. This PR includes changesets to release 28 packages
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 |
aaaa78b
to
a83e4dd
Compare
a83e4dd
to
83f1828
Compare
Co-authored-by: Kevin Ingersoll <[email protected]> Co-authored-by: alvrs <[email protected]>
Co-authored-by: alvarius <[email protected]>
Co-authored-by: alvrs <[email protected]>
packages/store/src/Storage.sol
Outdated
* Load up to 32 bytes from storage at the given storagePointer and offset. | ||
* The return value is left-aligned, the bytes beyond the length are not zeroed out, | ||
* and the caller is expected to truncate as needed. | ||
* The field can span up to two storage slots. |
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.
why? can we expand this comment to explain the case in which this happens? (I think it's because fields are packed tightly so two bytes20
fields together will take up a 32-byte slot + an extra 8 bytes of the next slot?)
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.
Yeah so since we tightly pack a field can span over two slots (eg the example you mentioned, or eg a bytes1
field followed by a bytes32
field), but since the max field length is 32 bytes it can maximally span two fields.
Loading field currently uses
Storage.load
, which can load arbitrary number of slots. However, fields are always within bytes32.Storage.loadField
removes the logic for handling arbitrary lengths and allocating memory.This results in a 160 gas saving for fields that span a single slot and 80 gas saving for fields that span multiple slots. Decoding will also be cheaper because it would simple type casting.
Storage.loadField
should be used forgetStaticField
oncegetField
is split intogetStaticField
andgetDynamicField
.