Skip to content

Commit

Permalink
perf(mangler): allocate base54 name without heap allocation (#8472)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Boshen and autofix-ci[bot] authored Jan 14, 2025
1 parent 31dac22 commit 7a8200c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_mangler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
9 changes: 5 additions & 4 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use compact_str::CompactString;
use itertools::Itertools;
use rustc_hash::FxHashSet;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7a8200c

Please sign in to comment.