Skip to content

Commit 681b867

Browse files
committed
Auto merge of #125129 - cuviper:branchless-len_utf8, r=<try>
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.
2 parents ade234d + ba2f5a9 commit 681b867

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

library/core/src/char/methods.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1741,12 +1741,8 @@ impl EscapeDebugExtArgs {
17411741
const fn len_utf8(code: u32) -> usize {
17421742
if code < MAX_ONE_B {
17431743
1
1744-
} else if code < MAX_TWO_B {
1745-
2
1746-
} else if code < MAX_THREE_B {
1747-
3
17481744
} else {
1749-
4
1745+
2 + ((code >= MAX_TWO_B) as usize) + ((code >= MAX_THREE_B) as usize)
17501746
}
17511747
}
17521748

0 commit comments

Comments
 (0)