Skip to content

draft: gas optimization#1475

Closed
Boffee wants to merge 17 commits intolatticexyz:mainfrom
Boffee:boffee/gas-optimization
Closed

draft: gas optimization#1475
Boffee wants to merge 17 commits intolatticexyz:mainfrom
Boffee:boffee/gas-optimization

Conversation

@Boffee
Copy link
Contributor

@Boffee Boffee commented Sep 13, 2023

Pasted from discord:

Feature request: bring real world mud2 read/write gas cost to parity with sload and sstore.

Almost all of the overhead that mud2 adds on top of sload and sstore comes from computing static constants onchain.

For the current version of mud2, warm Table.get[StaticField] cost in the ballpark of 2000 + 500 * (# of fields in key schema) + 500 * (# of fields in value schema). This can probably be reduced by 90%+ to < 400 gas:

  • Hardcoding the values for getValueSchema and getKeySchema reduces the gas down to a flat 1500.
  • Hardcoding everything inside StoreCore.getField except for storage location reduces the gas down to 500.
  • simplifying the storage location hash function can reduces the gas down to Storage.load which is 400.
  • a custom Storage.loadField might reduce gas further, though not sure by how much.

The same logic is applicable to writes.

@Boffee Boffee requested review from alvrs and frolic as code owners September 13, 2023 23:10
@changeset-bot
Copy link

changeset-bot bot commented Sep 13, 2023

⚠️ No Changeset found

Latest commit: 5072790

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@frolic frolic requested a review from dk1a September 13, 2023 23:28
Copy link
Contributor

@dk1a dk1a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far looks really good, I wanted to make basically the same thing and hadn't gotten to it yet. Do tell if you want me to take it off your hands or if u wanna finish it urself


return FieldLayoutLib.encode(_fieldLayout, 5);
return _fieldLayout;
}
Copy link
Member

@frolic frolic Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we even need getFieldLayout if we have it as a constant above and it's more gas efficient to use that? wonder if it's worth just exporting that as a named constant like we do for table ID

@frolic frolic marked this pull request as draft September 14, 2023 10:16
Internal methods skip StoreSwitch and call StoreCore directly.
This reduce the gas cost by ~300 per call.
@Boffee
Copy link
Contributor Author

Boffee commented Sep 14, 2023

so far looks really good, I wanted to make basically the same thing and hadn't gotten to it yet. Do tell if you want me to take it off your hands or if u wanna finish it urself

Looks like I snatched up all the low hanging fruits lol. Take over if your hands are free. I can help if you're busy with other stuff.

@frolic
Copy link
Member

frolic commented Oct 30, 2023

I think ~all of these were merged as separate smaller PRs, so closing this one!

@frolic frolic closed this Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants