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

refactor(store): optimize Schema #1252

Merged
merged 10 commits into from
Aug 18, 2023
Merged

refactor(store): optimize Schema #1252

merged 10 commits into from
Aug 18, 2023

Conversation

dk1a
Copy link
Contributor

@dk1a dk1a commented Aug 7, 2023

  • Optimized all hot paths (everything except validate - only did a few obvious tweaks there)
  • encode can still be made much better by replacing SchemaType with a static length, but that's another PR
  • Another encode optimization would be to inline it in table libs, but not sure it's worth it, will think about it when I get to table libs

Case study of encode's gas optimizations, this function is particularly interesting. (the screenshot shows code before this PR, changes are mostly 1:1, but within the loop I use gas per average iteration, which depends on Schema composition, and I used somewhat different schemas before and after, expect it to vary by 5-10 gas. 56->47 could be mostly noise for example.)
Note that gas within the loop is more impactful, as it's multiplied by the number of schema fields.
Screenshot 2023-08-07 201658

@changeset-bot
Copy link

changeset-bot bot commented Aug 7, 2023

🦋 Changeset detected

Latest commit: bade5cb

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

This PR includes changesets to release 27 packages
Name Type
@latticexyz/store Patch
@latticexyz/world Patch
@latticexyz/cli Patch
@latticexyz/dev-tools Patch
@latticexyz/react Patch
@latticexyz/std-client Patch
@latticexyz/store-cache Patch
@latticexyz/store-indexer Patch
@latticexyz/store-sync Patch
@latticexyz/ecs-browser Patch
@latticexyz/block-logs-stream Patch
@latticexyz/common Patch
@latticexyz/config Patch
create-mud 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-contracts 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

@ludns
Copy link
Member

ludns commented Aug 7, 2023

great work!

@dk1a dk1a changed the title WIP optimize Schema refactor(store): optimize Schema Aug 9, 2023
@dk1a dk1a marked this pull request as ready for review August 9, 2023 14:18
@dk1a dk1a requested review from alvrs and holic as code owners August 9, 2023 14:18
Comment on lines 315 to +330
"file": "test/Schema.t.sol",
"test": "testGetNumDynamicFields",
"name": "get number of dynamic fields from schema",
"gasUsed": 74
"gasUsed": 68
},
{
"file": "test/Schema.t.sol",
"test": "testGetNumFields",
"name": "get number of all fields from schema",
"gasUsed": 34
},
{
"file": "test/Schema.t.sol",
"test": "testGetNumStaticFields",
"name": "get number of static fields from schema",
"gasUsed": 85
"gasUsed": 79
Copy link
Contributor Author

Choose a reason for hiding this comment

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

you may note that getting both dynamic and static is 2x cheaper than getting them separately. This is wrong and related to #1267
I wonder if these tests should be removed to avoid confusion?

@dk1a dk1a mentioned this pull request Aug 10, 2023
15 tasks
holic
holic previously approved these changes Aug 18, 2023
@dk1a dk1a merged commit 0d12db8 into main Aug 18, 2023
9 checks passed
@dk1a dk1a deleted the dk1a/schema-gas branch August 18, 2023 13:36
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