Skip to content

Optimize std::str::Chars::next and std::str::Chars::next_back #142038

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Kmeakin
Copy link
Contributor

@Kmeakin Kmeakin commented Jun 4, 2025

Before/after for next: https://godbolt.org/z/bqcvaYxz1
Before/after for next_back: https://godbolt.org/z/MvTeeWKsP

std::sys_common::wtf8::Wtf8CodePoints will also benefit from this, since it uses the same next_code_point and next_code_point_reverse functions internally.

I also added tests for all codepoints in the range 0..=char::MAX (including surrogats that can only appear in WTF-8), so the new implementations have been exhaustively tested

Kmeakin added 2 commits June 4, 2025 19:55
There are only 0x10FFFF possible codepoints, so we can exhaustively test
all of them.
By inlining the helper functions, we can expose some opportunites for
CSE. Also convert the series of nested `if` branches to early return,
which IMO makes the code clearer.

Comparison of assembly before and after for `next_code_point`:
https://godbolt.org/z/bqcvaYxz1

Comparison of assembly before and after for `next_code_point_reverse`:
https://godbolt.org/z/n9WM7hPxv
@rustbot
Copy link
Collaborator

rustbot commented Jun 4, 2025

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 4, 2025
@Kmeakin
Copy link
Contributor Author

Kmeakin commented Jul 3, 2025

ping @scottmcm ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants