-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
match
for string slices
#6202
Merged
Merged
match
for string slices
#6202
Conversation
This file contains 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
Benchmark for fcf2380Click to view benchmark
|
Benchmark for f90f755Click to view benchmark
|
Benchmark for 69ca4b3Click to view benchmark
|
xunilrj
force-pushed
the
xunilrj/string-slice-match
branch
from
July 4, 2024 10:29
9e7c56b
to
98e704d
Compare
Benchmark for 3cd9fe7Click to view benchmark
|
JoshuaBatty
approved these changes
Jul 5, 2024
Benchmark for 30ff363Click to view benchmark
|
IGI-111
approved these changes
Jul 5, 2024
Benchmark for b4f0ac7Click to view benchmark
|
8 tasks
ironcev
added a commit
that referenced
this pull request
Jul 8, 2024
) ## Description This PR adds `string slices` to `CURRENTLY_SUPPORTED_TYPES_MESSAGE` in `match` expressions. The support for string slices is added in #6202. ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
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 implements
match
for string slices including radix trie optimization and is a task of #5110.For example a simple
match
likewill generate code following this logic:
In logical terms, this boils down to checking the length and an
O(N)
check on the string. Albeit the bytecode will be more complex because of all the branches.Another interesting optimization is the "packed string literal" that coalesces all "match arms string slices" into just one string. In the case above, given that one of the arms contains all the necessary strings for all other comparisons, we will create just one string literal. Saving a lot of bytes in the data section.
The section below describes how
rustc
deals with this desugaring. I think these choices make more sense to us for two reasons:1 - Avoid testing common prefixes multiple times will spend less gas in general (needs more testing);
2 - packing all strings will decrease the data section size.
This is the bytecode generated in this case:
How
rustc
desugarmatch
For comparison, this is the generated ASM with comments on how Rust tackles this.
First, this is the function used:
This is the LLVM IR generated. There is a match on the length of each string slice arms. The valid range is (5, 18), everything outside of this is the wildcard match arm. This range will be important later.
this is how "f" is called
this is
f
body.ja .LBB8_12
jumps into a simple return, returning EAX as 6. It is the wildcard return value. The cleverness of this is that whenRSI
is smaller than 5, it will become negative (because ofadd rsi, -5
, wrapping into huge unsigned ints, and will also triggerJA
(which stands forJump Above
), effectively jumping when the slice length is outside of the expected range which is (5, 18).After that, it uses a jump table based on the string length minus 5. Everywhere the string length is invalid, the jump address is
LBB8_12
., still returningEAX
as 6.This is the jump table used:
The interesting entry is entry 5, which has two strings: "get_method" and "get_tokens". Here we can see that
rust
actually compares the complete string slice twice. Even though they have an intersection.This is comparable to what
clang
is doing: rust-lang/rust#61961Code and Bytecode
This PR also implements code printing when printing bytecode. For now this is only enable for tests. It gnerates something like:
As we can see, not great, but helpful nonetheless. We can (should?) improve this by better "carrying" spans in all transformations and lowerings.
Checklist
Breaking*
orNew Feature
labels where relevant.