-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Improve method selector on contracts #7458
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CodSpeed Performance ReportMerging #7458 will not alter performanceComparing Summary
|
048a736 to
5f2f098
Compare
0ac99b2 to
1598d82
Compare
tritao
reviewed
Nov 3, 2025
2f3cfa1 to
9265277
Compare
tritao
approved these changes
Nov 3, 2025
JoshuaBatty
approved these changes
Nov 3, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR continues the optimisation of contract calls on top of #7455. Now the contract method selector is optimised. Ideally, the method selection would just be a
matchand we would let the compiler generate the best code.But we are not there yet. So, for now, we are going to first test the called method length. And based on its length we are going to check the whole string. Something like:
This alone reduces gas a lot.
To understand why
match_expressions_allgas usage is much worse see below.Radix Trie
This PR also turns off the radix trie optimisation at
sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_match_expression.rs. I am not 100% sure of its correctness and heuristics.We need a better way to decide when not to use it. One of the examples where the generated code is worse is when all strings are very similar. For example:
get_a,get_b. The generated trie will first test its length. Both have length 5. Not much was gained. Then it tests if forget_, which makes sense as it is the common substring. And the last step will test foraorb.The issue is that all branches and jumps of the last step are not worthy to test just one character. Is probably cheaper to just test the whole string for each option.
In the end, we need a better heuristic to determine when to use this optimisation.
Checklist
Breaking*orNew Featurelabels where relevant.