Skip to content

Commit 4a3241a

Browse files
committed
Benchmark more possibles impls of [u8]::make_ascii_uppercase
1 parent b4faa9b commit 4a3241a

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

src/libcore/benches/ascii.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ benches! {
5959
}
6060
}
6161

62-
fn case02_lookup(bytes: &mut [u8]) {
62+
fn case02_lookup_table(bytes: &mut [u8]) {
6363
for byte in bytes {
6464
*byte = ASCII_UPPERCASE_MAP[*byte as usize]
6565
}
@@ -141,6 +141,55 @@ benches! {
141141
}
142142
}
143143

144+
fn case09_mask_mult_bool_branchy_lookup_table(bytes: &mut [u8]) {
145+
fn is_ascii_lowercase(b: u8) -> bool {
146+
if b >= 0x80 { return false }
147+
match ASCII_CHARACTER_CLASS[b as usize] {
148+
L | Lx => true,
149+
_ => false,
150+
}
151+
}
152+
for byte in bytes {
153+
*byte &= !(0x20 * (is_ascii_lowercase(*byte) as u8))
154+
}
155+
}
156+
157+
fn case10_mask_mult_bool_lookup_table(bytes: &mut [u8]) {
158+
fn is_ascii_lowercase(b: u8) -> bool {
159+
match ASCII_CHARACTER_CLASS[b as usize] {
160+
L | Lx => true,
161+
_ => false
162+
}
163+
}
164+
for byte in bytes {
165+
*byte &= !(0x20 * (is_ascii_lowercase(*byte) as u8))
166+
}
167+
}
168+
169+
fn case11_mask_mult_bool_match_range(bytes: &mut [u8]) {
170+
fn is_ascii_lowercase(b: u8) -> bool {
171+
match b {
172+
b'a'...b'z' => true,
173+
_ => false
174+
}
175+
}
176+
for byte in bytes {
177+
*byte &= !(0x20 * (is_ascii_lowercase(*byte) as u8))
178+
}
179+
}
180+
181+
fn case12_mask_shifted_bool_match_range(bytes: &mut [u8]) {
182+
fn is_ascii_lowercase(b: u8) -> bool {
183+
match b {
184+
b'a'...b'z' => true,
185+
_ => false
186+
}
187+
}
188+
for byte in bytes {
189+
*byte &= !((is_ascii_lowercase(*byte) as u8) << 5)
190+
}
191+
}
192+
144193
@iter
145194

146195
is_ascii,
@@ -219,3 +268,36 @@ const ASCII_UPPERCASE_MAP: [u8; 256] = [
219268
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
220269
];
221270

271+
enum AsciiCharacterClass {
272+
C, // control
273+
Cw, // control whitespace
274+
W, // whitespace
275+
D, // digit
276+
L, // lowercase
277+
Lx, // lowercase hex digit
278+
U, // uppercase
279+
Ux, // uppercase hex digit
280+
P, // punctuation
281+
N, // Non-ASCII
282+
}
283+
use self::AsciiCharacterClass::*;
284+
285+
static ASCII_CHARACTER_CLASS: [AsciiCharacterClass; 256] = [
286+
// _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _a _b _c _d _e _f
287+
C, C, C, C, C, C, C, C, C, Cw,Cw,C, Cw,Cw,C, C, // 0_
288+
C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, // 1_
289+
W, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, // 2_
290+
D, D, D, D, D, D, D, D, D, D, P, P, P, P, P, P, // 3_
291+
P, Ux,Ux,Ux,Ux,Ux,Ux,U, U, U, U, U, U, U, U, U, // 4_
292+
U, U, U, U, U, U, U, U, U, U, U, P, P, P, P, P, // 5_
293+
P, Lx,Lx,Lx,Lx,Lx,Lx,L, L, L, L, L, L, L, L, L, // 6_
294+
L, L, L, L, L, L, L, L, L, L, L, P, P, P, P, C, // 7_
295+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
296+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
297+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
298+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
299+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
300+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
301+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
302+
N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
303+
];

0 commit comments

Comments
 (0)