Skip to content

Commit

Permalink
Auto merge of #125129 - cuviper:branchless-len_utf8, r=<try>
Browse files Browse the repository at this point in the history
Remove the branches from `len_utf8`

This changes `len_utf8` to add all of the range comparisons together,
rather than branching on each one. We should definitely test performance
though, because it's possible that this will pessimize mostly-ascii
inputs that would have had a short branch-predicted path before.
  • Loading branch information
bors committed May 15, 2024
2 parents ade234d + ba2f5a9 commit 681b867
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,12 +1741,8 @@ impl EscapeDebugExtArgs {
const fn len_utf8(code: u32) -> usize {
if code < MAX_ONE_B {
1
} else if code < MAX_TWO_B {
2
} else if code < MAX_THREE_B {
3
} else {
4
2 + ((code >= MAX_TWO_B) as usize) + ((code >= MAX_THREE_B) as usize)
}
}

Expand Down

0 comments on commit 681b867

Please sign in to comment.