Skip to content

Commit 1b9ae8f

Browse files
Rollup merge of #145515 - Kmeakin:km/optimize-char-encode-utf8, r=Mark-Simulacrum
Optimize `char::encode_utf8` Save a few instructions in `encode_utf8_raw_unchecked` by performing manual CSE.
2 parents 5a14685 + c9ce45c commit 1b9ae8f

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

library/core/src/char/methods.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,28 +1872,33 @@ pub const unsafe fn encode_utf8_raw_unchecked(code: u32, dst: *mut u8) {
18721872
// SAFETY: The caller must guarantee that the buffer pointed to by `dst`
18731873
// is at least `len` bytes long.
18741874
unsafe {
1875-
match len {
1876-
1 => {
1877-
*dst = code as u8;
1878-
}
1879-
2 => {
1880-
*dst = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
1881-
*dst.add(1) = (code & 0x3F) as u8 | TAG_CONT;
1882-
}
1883-
3 => {
1884-
*dst = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
1885-
*dst.add(1) = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1886-
*dst.add(2) = (code & 0x3F) as u8 | TAG_CONT;
1887-
}
1888-
4 => {
1889-
*dst = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
1890-
*dst.add(1) = (code >> 12 & 0x3F) as u8 | TAG_CONT;
1891-
*dst.add(2) = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1892-
*dst.add(3) = (code & 0x3F) as u8 | TAG_CONT;
1893-
}
1894-
// SAFETY: `char` always takes between 1 and 4 bytes to encode in UTF-8.
1895-
_ => crate::hint::unreachable_unchecked(),
1875+
if len == 1 {
1876+
*dst = code as u8;
1877+
return;
1878+
}
1879+
1880+
let last1 = (code >> 0 & 0x3F) as u8 | TAG_CONT;
1881+
let last2 = (code >> 6 & 0x3F) as u8 | TAG_CONT;
1882+
let last3 = (code >> 12 & 0x3F) as u8 | TAG_CONT;
1883+
let last4 = (code >> 18 & 0x3F) as u8 | TAG_FOUR_B;
1884+
1885+
if len == 2 {
1886+
*dst = last2 | TAG_TWO_B;
1887+
*dst.add(1) = last1;
1888+
return;
18961889
}
1890+
1891+
if len == 3 {
1892+
*dst = last3 | TAG_THREE_B;
1893+
*dst.add(1) = last2;
1894+
*dst.add(2) = last1;
1895+
return;
1896+
}
1897+
1898+
*dst = last4;
1899+
*dst.add(1) = last3;
1900+
*dst.add(2) = last2;
1901+
*dst.add(3) = last1;
18971902
}
18981903
}
18991904

0 commit comments

Comments
 (0)