mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
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:
parent
31dac229aa
commit
7a8200c20e
3 changed files with 7 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1829,6 +1829,7 @@ dependencies = [
|
|||
name = "oxc_mangler"
|
||||
version = "0.45.0"
|
||||
dependencies = [
|
||||
"compact_str",
|
||||
"itertools",
|
||||
"oxc_allocator",
|
||||
"oxc_ast",
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue