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 encodeKeyTuple function to tablegen #753

Merged
merged 8 commits into from
May 9, 2023
Merged

Conversation

yonadaaa
Copy link
Contributor

@yonadaaa yonadaaa commented May 8, 2023

fixes #751

This simplifies the Table contracts, but replacing the inlined code with an internal function increases gas use :(

It also causes stack too deep error on worlds with too many keys + fields.

@yonadaaa yonadaaa requested review from alvrs and holic as code owners May 8, 2023 13:58
@vercel
Copy link

vercel bot commented May 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mud ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 9, 2023 11:24am

@dk1a
Copy link
Contributor

dk1a commented May 8, 2023

It also causes stack too deep error on worlds with too many keys + fields.

I'm pretty sure too many keys+fields always caused stack too deep

@dk1a
Copy link
Contributor

dk1a commented May 8, 2023

This simplifies the Table contracts, but replacing the inlined code with an internal function increases gas use :(

You could add the function but not replace inlined code, the rendering function encapsulates the logic already, and the solidity code is autogenerated and code repetition doesn't matter

@holic
Copy link
Member

holic commented May 8, 2023

the solidity code is autogenerated and code repetition doesn't matter

+1 to this, I think we have a lot of room to optimize gas over time by just repeating code in areas of codegen

@yonadaaa
Copy link
Contributor Author

yonadaaa commented May 8, 2023

I'm pretty sure too many keys+fields always caused stack too deep

Yes, it will always happen eventually, but now it happens on the Statics table in the CLI tests, which it didn't before. I'm not sure why because _primaryKeys was there before.

the solidity code is autogenerated and code repetition doesn't matter

Fair, but the encode function for data is called and not inlined, right? IMO it's nice if the contracts stand alone as Solidity - but yes, in reality they are always codegen'ed.

@dk1a
Copy link
Contributor

dk1a commented May 8, 2023

Fair, but the encode function for data is called and not inlined, right?

yes, I wasn't thinking of gas at the time. You don't have to either at this moment, we'll need separate gas optimization PRs later anyways, I just mentioned it now when I had the thought

@dk1a
Copy link
Contributor

dk1a commented May 8, 2023

Yes, it will always happen eventually, but now it happens on the Statics table in the CLI tests, which it didn't before. I'm not sure why because _primaryKeys was there before.

yeah probably some nuances of additional stack space for internal method calls. Inlining would solve it, so even if you don't wanna optimize gas here you might still wanna go for it

@yonadaaa
Copy link
Contributor Author

yonadaaa commented May 8, 2023

Inlining would solve it, so even if you don't wanna optimize gas here you might still wanna go for it

I agree, I don't want to leave this error or have to modify the test example. I will inline the encodeKey logic!

holic
holic previously approved these changes May 8, 2023
@@ -129,6 +130,15 @@ library ${libraryName} {
])});
}

/** Encode keys as a bytes32 array using this table's schema */
function encodeKey(${renderArguments([_typedKeyArgs])}) internal pure returns (bytes32[] memory _primaryKeys) {
Copy link
Member

@holic holic May 8, 2023

Choose a reason for hiding this comment

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

not blocking but thinking we will want to consolidate our language around "key" vs "primary key" vs "key tuple" and wondering if we wanna just go ahead and name this encodeKeyTuple here (see #760)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - I will use encodeKeyTuple. I much prefer "key tuple" as we represent primary keys in many ways.

To be clear I don't think we should rename the _primaryKeys vars in this PR (not that you are proposing that).

Copy link
Member

Choose a reason for hiding this comment

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

yep! agreed

@yonadaaa yonadaaa changed the title feat: add encodeKey function to tablegen feat: add encodeKeyTuple function to tablegen May 9, 2023
@yonadaaa yonadaaa changed the title feat: add encodeKeyTuple function to tablegen feat: add encodeKeyTuple function to tablegen May 9, 2023
@yonadaaa yonadaaa requested a review from holic May 9, 2023 11:44
@holic holic changed the title feat: add encodeKeyTuple function to tablegen feat(store): add encodeKeyTuple function to tablegen May 9, 2023
@holic holic merged commit d29b0bb into main May 9, 2023
@holic holic deleted the add-encode-key branch June 23, 2023 11:34
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.

Proposal: Tables should expose an encodeKey() view
3 participants