Skip to content

Commit f7b212b

Browse files
authored
perf(es/minifier): Use Vec<u8> as a buffer for base54 (#3993)
Description: We are only using ASCII characters so we can avoid utf8 logics by using `Vec<u8>` as a buffer and converting it into `String` at the end.
1 parent 63177b7 commit f7b212b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

crates/swc_ecma_minifier/src/util/base54.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@ pub(crate) fn encode(init: &mut usize, skip_reserved: bool) -> String {
2424
base <<= 6;
2525
}
2626

27-
let mut ret = String::new();
27+
let mut ret = vec![];
2828

2929
base /= 54;
30-
let mut c = BASE54_DEFAULT_CHARS[n / base] as char;
30+
let mut c = BASE54_DEFAULT_CHARS[n / base];
3131
ret.push(c);
3232

3333
while base > 1 {
3434
n %= base;
3535
base >>= 6;
36-
c = BASE54_DEFAULT_CHARS[n / base] as char;
36+
c = BASE54_DEFAULT_CHARS[n / base];
3737

3838
ret.push(c);
3939
}
4040

41-
ret
41+
unsafe {
42+
// Safety: We are only using ascii characters
43+
String::from_utf8_unchecked(ret)
44+
}
4245
}
4346

4447
#[allow(unused)]

0 commit comments

Comments
 (0)