perf(mangler): allocate base54 name without heap allocation (#8472)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Boshen 2025-01-14 10:36:21 +08:00 committed by GitHub
parent 31dac229aa
commit 7a8200c20e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 4 deletions

1
Cargo.lock generated
View file

@ -1829,6 +1829,7 @@ dependencies = [
name = "oxc_mangler"
version = "0.45.0"
dependencies = [
"compact_str",
"itertools",
"oxc_allocator",
"oxc_ast",

View file

@ -27,5 +27,6 @@ oxc_index = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }
compact_str = { workspace = true }
itertools = { workspace = true }
rustc-hash = { workspace = true }

View file

@ -1,3 +1,4 @@
use compact_str::CompactString;
use itertools::Itertools;
use rustc_hash::FxHashSet;
@ -334,18 +335,18 @@ fn base54(n: usize) -> CompactStr {
// Base 54 at first because these are the usable first characters in JavaScript identifiers
// <https://tc39.es/ecma262/#prod-IdentifierStart>
let base = 54usize;
let mut ret = String::new();
ret.push(BASE54_CHARS[num % base] as char);
// SAFETY: `BASE54_CHARS` is utf8.
let mut s = unsafe { CompactString::from_utf8_unchecked([BASE54_CHARS[num % base]]) };
num /= base;
// Base 64 for the rest because after the first character we can also use 0-9 too
// <https://tc39.es/ecma262/#prod-IdentifierPart>
let base = 64usize;
while num > 0 {
num -= 1;
ret.push(BASE54_CHARS[num % base] as char);
s.push(BASE54_CHARS[num % base] as char);
num /= base;
}
CompactStr::new(&ret)
CompactStr::from(s)
}
fn debug_name(n: usize) -> CompactStr {