mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +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"
|
name = "oxc_mangler"
|
||||||
version = "0.45.0"
|
version = "0.45.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"compact_str",
|
||||||
"itertools",
|
"itertools",
|
||||||
"oxc_allocator",
|
"oxc_allocator",
|
||||||
"oxc_ast",
|
"oxc_ast",
|
||||||
|
|
|
||||||
|
|
@ -27,5 +27,6 @@ oxc_index = { workspace = true }
|
||||||
oxc_semantic = { workspace = true }
|
oxc_semantic = { workspace = true }
|
||||||
oxc_span = { workspace = true }
|
oxc_span = { workspace = true }
|
||||||
|
|
||||||
|
compact_str = { workspace = true }
|
||||||
itertools = { workspace = true }
|
itertools = { workspace = true }
|
||||||
rustc-hash = { workspace = true }
|
rustc-hash = { workspace = true }
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use compact_str::CompactString;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustc_hash::FxHashSet;
|
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
|
// Base 54 at first because these are the usable first characters in JavaScript identifiers
|
||||||
// <https://tc39.es/ecma262/#prod-IdentifierStart>
|
// <https://tc39.es/ecma262/#prod-IdentifierStart>
|
||||||
let base = 54usize;
|
let base = 54usize;
|
||||||
let mut ret = String::new();
|
// SAFETY: `BASE54_CHARS` is utf8.
|
||||||
ret.push(BASE54_CHARS[num % base] as char);
|
let mut s = unsafe { CompactString::from_utf8_unchecked([BASE54_CHARS[num % base]]) };
|
||||||
num /= base;
|
num /= base;
|
||||||
// Base 64 for the rest because after the first character we can also use 0-9 too
|
// Base 64 for the rest because after the first character we can also use 0-9 too
|
||||||
// <https://tc39.es/ecma262/#prod-IdentifierPart>
|
// <https://tc39.es/ecma262/#prod-IdentifierPart>
|
||||||
let base = 64usize;
|
let base = 64usize;
|
||||||
while num > 0 {
|
while num > 0 {
|
||||||
num -= 1;
|
num -= 1;
|
||||||
ret.push(BASE54_CHARS[num % base] as char);
|
s.push(BASE54_CHARS[num % base] as char);
|
||||||
num /= base;
|
num /= base;
|
||||||
}
|
}
|
||||||
CompactStr::new(&ret)
|
CompactStr::from(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn debug_name(n: usize) -> CompactStr {
|
fn debug_name(n: usize) -> CompactStr {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue