Skip to content

Commit 1c3131c

Browse files
committed
Auto merge of rust-lang#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 ac385a5 + 38f14be commit 1c3131c

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

library/core/src/char/methods.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1739,15 +1739,9 @@ impl EscapeDebugExtArgs {
17391739

17401740
#[inline]
17411741
const fn len_utf8(code: u32) -> usize {
1742-
if code < MAX_ONE_B {
1743-
1
1744-
} else if code < MAX_TWO_B {
1745-
2
1746-
} else if code < MAX_THREE_B {
1747-
3
1748-
} else {
1749-
4
1750-
}
1742+
1 + ((code >= MAX_ONE_B) as usize)
1743+
+ ((code >= MAX_TWO_B) as usize)
1744+
+ ((code >= MAX_THREE_B) as usize)
17511745
}
17521746

17531747
/// Encodes a raw u32 value as UTF-8 into the provided byte buffer,

0 commit comments

Comments
 (0)