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

Reduce Wasm Code Size Impact of Host Trait #216

Merged
merged 3 commits into from
Feb 3, 2025
Merged

Conversation

rauljordan
Copy link
Contributor

Description

After this PR, the code impact of the Host trait addition on contracts is only 0.1Kb compared to the code from 0.7.0.

Using global functions that take in a &dyn Trait has a massive impact on WASM size, compared to instead implementing the method as a trait on your contract. Here’s the context:
We have a Contract struct in Rust, and we have a Router trait we are required to implement. Looks a bit like this today:

Screenshot 2025-01-31 at 12 09 44

We route each selector to a specific method of our contract struct, and before that, we check if we need to deny non-payable functions that have a tx with a value
Screenshot 2025-01-31 at 12 11 23
The problem is this deny_value method takes in a &dyn Host, which requires the compiler to produce more data to resolve vtables
We fixed this by instead defining a ValueDenier trait that contracts implement, so there is no need for a global deny_value method. Here is
Screenshot 2025-01-31 at 12 12 26
the fix, which has 0 impact on wasm size compared to 2Kb from the prior screenshots

@rauljordan rauljordan marked this pull request as draft January 31, 2025 18:25
@rauljordan rauljordan marked this pull request as ready for review January 31, 2025 18:25
@rauljordan rauljordan requested a review from rory-ocl January 31, 2025 21:50
Copy link
Contributor

@gligneul gligneul left a comment

Choose a reason for hiding this comment

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

Great find!

@rauljordan rauljordan merged commit 0354b77 into main Feb 3, 2025
10 checks passed
@rauljordan rauljordan deleted the reduce-code-size branch February 3, 2025 16:07
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.

2 participants