Skip to content
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

Merged
merged 8 commits into from
Sep 16, 2023

Conversation

Boffee
Copy link
Contributor

@Boffee Boffee commented Sep 15, 2023

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 for getStaticField once getField is split into getStaticField and getDynamicField.

@Boffee Boffee requested review from alvrs and dk1a as code owners September 15, 2023 22:08
@changeset-bot
Copy link

changeset-bot bot commented Sep 15, 2023

🦋 Changeset detected

Latest commit: 28efb9d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 28 packages
Name Type
@latticexyz/store Patch
@latticexyz/cli Patch
@latticexyz/dev-tools Patch
@latticexyz/react Patch
@latticexyz/store-indexer Patch
@latticexyz/store-sync Patch
@latticexyz/world Patch
@latticexyz/abi-ts Patch
@latticexyz/block-logs-stream Patch
@latticexyz/common Patch
@latticexyz/config Patch
create-mud Patch
@latticexyz/ecs-browser Patch
@latticexyz/gas-report Patch
@latticexyz/network Patch
@latticexyz/noise Patch
@latticexyz/phaserx Patch
@latticexyz/protocol-parser Patch
@latticexyz/recs Patch
@latticexyz/schema-type Patch
@latticexyz/services Patch
@latticexyz/solecs Patch
solhint-config-mud Patch
solhint-plugin-mud Patch
@latticexyz/std-client Patch
@latticexyz/std-contracts Patch
@latticexyz/store-cache Patch
@latticexyz/utils Patch

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

@Boffee Boffee force-pushed the boffee/optimize-load-field branch 2 times, most recently from aaaa78b to a83e4dd Compare September 15, 2023 22:12
alvrs
alvrs previously approved these changes Sep 16, 2023
@alvrs alvrs changed the title feat(store): optimize load field with custom loadField function feat(store): add Storage.loadField for optimized loading of 32 bytes or less from storage Sep 16, 2023
* 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.
Copy link
Member

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?)

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

4 participants