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: hardcode key/value schema in table libraries #2328

Merged
merged 11 commits into from
Feb 28, 2024

Conversation

holic
Copy link
Member

@holic holic commented Feb 27, 2024

similar to #2166

looks like decent gas savings but only on register/install methods and some bytecode savings when using table libs

Copy link

changeset-bot bot commented Feb 27, 2024

🦋 Changeset detected

Latest commit: 39fa542

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

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

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

@holic
Copy link
Member Author

holic commented Feb 27, 2024

seems to help with bytecode?

--- a/packages/world/sizes.txt
+++ b/packages/world/sizes.txt
@@ -25,7 +25,7 @@
 | HookInstance               | 0.045     | 24.531      |
 | HookLib                    | 0.045     | 24.531      |
 | Hooks                      | 0.045     | 24.531      |
-| InitModule                 | 19.677    | 4.899       |
+| InitModule                 | 18.258    | 6.318       |
 | InitModuleAddress          | 0.045     | 24.531      |
 | InstalledModules           | 0.045     | 24.531      |
 | LayoutOffsets              | 0.045     | 24.531      |

Copy link
Contributor

@yonadaa yonadaa left a comment

Choose a reason for hiding this comment

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

I like this - the more logic we have in TS, the better.

_keySchema[3] = SchemaType.INT120;

return SchemaLib.encode(_keySchema);
return Schema.wrap(0x0034040003601f2e000000000000000000000000000000000000000000000000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat! Is there any gas impact to declaring these as constants, ie. Schema constant KEY_SCHEMA = ...?

Wondering if we can go even further and remove the functions (getKeySchema), leaving only the constants. Seems more fitting if all the functions do is return a hardcoded value.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, that'd be the next step (see OP) but wanted to wait for the other PR to land first

@holic holic changed the title WIP hardcode schema refactor: hardcode key/value schema in table libraries Feb 28, 2024
@holic holic marked this pull request as ready for review February 28, 2024 16:11
@holic holic requested a review from alvrs as a code owner February 28, 2024 16:11
Copy link
Contributor

@yonadaa yonadaa left a comment

Choose a reason for hiding this comment

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

What about a changeset? Seems like a breaking change to remove getKeySchema


return SchemaLib.encode(_valueSchema);
}
// Hex-encoded key schema of ()
Copy link
Contributor

@yonadaa yonadaa Feb 28, 2024

Choose a reason for hiding this comment

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

() is kinda cursed but what can ya do

Copy link
Member Author

Choose a reason for hiding this comment

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

it's accurate at least!

Copy link
Member Author

Choose a reason for hiding this comment

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

I could add a conditional for this but seems messy

@@ -29,28 +26,10 @@ library ERC20Metadata {
FieldLayout constant _fieldLayout =
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be interesting to have a similar // Hex-encoded .... comment explaining how the field layout is generated!

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered that but didn't have an obvious way to generate a human-readable comment for it so I left it (it never had one)

@holic
Copy link
Member Author

holic commented Feb 28, 2024

What about a changeset? Seems like a breaking change to remove getKeySchema

I guess major if you're leaning on internals but that doesn't seem like a very supported path? I went for minor for now.

Comment on lines +81 to +88
// Hex-encoded key schema of (${keyTuple.map((field) => field.internalTypeId).join(", ")})
Schema constant _keySchema = Schema.wrap(${keySchemaToHex(
Object.fromEntries(keyTuple.map((field) => [field.name, field.internalTypeId])) as KeySchema
)});
// Hex-encoded value schema of (${fields.map((field) => field.internalTypeId).join(", ")})
Schema constant _valueSchema = Schema.wrap(${valueSchemaToHex(
Object.fromEntries(fields.map((field) => [field.name, field.internalTypeId])) as ValueSchema
)});
Copy link
Member

Choose a reason for hiding this comment

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

I generally agree with this change but am a little wary of this being a change to an audited code path. Are there any tests we can add to really make sure the typescript encoding matches the expected encoding? maybe a couple forge test cases where we encode with the new method and confirm it matches what would be produced by SchemaLib.encode?

Copy link
Member Author

Choose a reason for hiding this comment

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

how about this? 39fa542

Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

@@ -93,7 +93,7 @@
"file": "test/World.t.sol",
"test": "testDeleteRecord",
"name": "Delete record",
"gasUsed": 8957
"gasUsed": 8958
Copy link
Contributor

Choose a reason for hiding this comment

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

🙀

@holic holic merged commit 3042f86 into main Feb 28, 2024
11 checks passed
@holic holic deleted the holic/hardcoded-schema branch February 28, 2024 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants