Faster reverse() string function for ASCII-only case#14195
Merged
alamb merged 2 commits intoapache:mainfrom Jan 22, 2025
Merged
Faster reverse() string function for ASCII-only case#14195alamb merged 2 commits intoapache:mainfrom
alamb merged 2 commits intoapache:mainfrom
Conversation
1010906 to
7e91bc8
Compare
Dandandan
reviewed
Jan 21, 2025
| // SAFETY: Since the original string was ASCII, reversing the bytes still results in valid UTF-8. | ||
| let reversed = unsafe { | ||
| // reverse bytes directly since ASCII characters are single bytes | ||
| String::from_utf8_unchecked(s.bytes().rev().collect::<Vec<u8>>()) |
Contributor
There was a problem hiding this comment.
We could avoid a allocation here by copying into a Vec<u8> buffer
// slightly faster than using iterator `bytes`
bytes_buf.extend(s.as_bytes())
bytes_buf.reverse()
...
bytes_buf.clear()
Contributor
Author
There was a problem hiding this comment.
Got an improvement of ~30% at most after add bytes_buf
028333c to
e8b44b5
Compare
Dandandan
approved these changes
Jan 21, 2025
Contributor
That is some sweet sweet performance |
Contributor
|
Thanks @UBarney and @Dandandan |
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
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.
Which issue does this PR close?
Closes #12445.
Rationale for this change
See the issue.
What changes are included in this PR?
gen_string_arrayinbenches/character_length.rsto helper.rsAre these changes tested?
Yes. Using existing unit test
bench result
Are there any user-facing changes?
No