Optimize std::str::Chars::next and std::str::Chars::next_back by Kmeakin · Pull Request #142038 · rust-lang/rust (original) (raw)

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

[ Show hidden characters]({{ revealButtonHref }})

@Kmeakin

@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

scottmcm

scottmcm

scottmcm

@rustbot rustbot added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jul 4, 2025

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Aug 9, 2025

tgross35

Comment on lines 29 to 37

@rustbot rustbot added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Sep 5, 2025

rust-bors bot added a commit that referenced this pull request

Sep 5, 2025

@rust-bors

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

@Kmeakin

There are only 0x10FFFF possible codepoints, so we can exhaustively test all of them.

@Kmeakin

@Kmeakin

By reordering some operations, 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/9Te84YzhK

Comparison of assembly before and after for next_code_point_reverse: https://godbolt.org/z/fTx1a7oz1