perf(es/minifier): Split frequently used functions (#4352)

This commit is contained in:
Donny/강동윤 2022-04-17 23:22:38 +09:00 committed by GitHub
parent 29ff9cb29f
commit 42e15aedc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 17 deletions

View File

@ -69,11 +69,29 @@ impl Scope {
preserved: &FxHashSet<Id>, preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<JsWord>, preserved_symbols: &FxHashSet<JsWord>,
) { ) {
let mut n = 0;
let mut queue = self.data.queue.take(); let mut queue = self.data.queue.take();
queue.sort_by(|a, b| b.1.cmp(&a.1)); queue.sort_by(|a, b| b.1.cmp(&a.1));
let mut cloned_reverse = reverse.clone(); let mut cloned_reverse = reverse.clone();
self.rename_one_scope(to, &mut cloned_reverse, queue, preserved, preserved_symbols);
for child in self.children.iter_mut() {
child.rename(to, &cloned_reverse, preserved, preserved_symbols);
}
}
#[inline(never)]
fn rename_one_scope(
&self,
to: &mut AHashMap<Id, JsWord>,
cloned_reverse: &mut FxHashMap<JsWord, Vec<Id>>,
queue: Vec<(Id, u32)>,
preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<JsWord>,
) {
let mut n = 0;
for (id, cnt) in queue { for (id, cnt) in queue {
if cnt == 0 || preserved.contains(&id) || to.get(&id).is_some() { if cnt == 0 || preserved.contains(&id) || to.get(&id).is_some() {
continue; continue;
@ -82,14 +100,12 @@ impl Scope {
loop { loop {
let sym = base54::encode(&mut n, true); let sym = base54::encode(&mut n, true);
let sym: JsWord = sym.into();
// TODO: Use base54::decode // TODO: Use base54::decode
if preserved_symbols.contains(&sym) { if preserved_symbols.contains(&sym) {
continue; continue;
} }
if self.can_rename(&id, &sym, &cloned_reverse) { if self.can_rename(&id, &sym, cloned_reverse) {
to.insert(id.clone(), sym.clone()); to.insert(id.clone(), sym.clone());
cloned_reverse.entry(sym).or_default().push(id.clone()); cloned_reverse.entry(sym).or_default().push(id.clone());
// self.data.decls.remove(&id); // self.data.decls.remove(&id);
@ -99,10 +115,6 @@ impl Scope {
} }
} }
} }
for child in self.children.iter_mut() {
child.rename(to, &cloned_reverse, preserved, preserved_symbols);
}
} }
#[inline(never)] #[inline(never)]

View File

@ -30,8 +30,6 @@ impl PrivateNameMangler {
} else { } else {
let sym = base54::encode(&mut self.private_n, true); let sym = base54::encode(&mut self.private_n, true);
let sym: JsWord = sym.into();
self.renamed_private.insert(id.clone(), sym.clone()); self.renamed_private.insert(id.clone(), sym.clone());
sym sym

View File

@ -86,9 +86,8 @@ impl ManglePropertiesState {
if let Some(cached) = self.cache.get(name) { if let Some(cached) = self.cache.get(name) {
Some(cached.clone()) Some(cached.clone())
} else { } else {
let sym = base54::encode(&mut self.n, true); let mangled_name = base54::encode(&mut self.n, true);
let mangled_name: JsWord = sym.into();
self.cache.insert(name.clone(), mangled_name.clone()); self.cache.insert(name.clone(), mangled_name.clone());
Some(mangled_name) Some(mangled_name)
} }

View File

@ -1,9 +1,11 @@
use swc_atoms::JsWord;
static BASE54_DEFAULT_CHARS: &[u8; 64] = static BASE54_DEFAULT_CHARS: &[u8; 64] =
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
/// givin a number, return a base54 encoded string /// givin a number, return a base54 encoded string
/// `usize -> [a-zA-Z$_][a-zA-Z$_0-9]*` /// `usize -> [a-zA-Z$_][a-zA-Z$_0-9]*`
pub(crate) fn encode(init: &mut usize, skip_reserved: bool) -> String { pub(crate) fn encode(init: &mut usize, skip_reserved: bool) -> JsWord {
if skip_reserved { if skip_reserved {
while init.is_reserved() while init.is_reserved()
|| init.is_reserved_in_strict_bind() || init.is_reserved_in_strict_bind()
@ -38,10 +40,12 @@ pub(crate) fn encode(init: &mut usize, skip_reserved: bool) -> String {
ret.push(c); ret.push(c);
} }
unsafe { let s = unsafe {
// Safety: We are only using ascii characters // Safety: We are only using ascii characters
String::from_utf8_unchecked(ret) String::from_utf8_unchecked(ret)
} };
s.into()
} }
#[allow(unused)] #[allow(unused)]
@ -175,7 +179,7 @@ mod tests {
fn gen(&mut self, expected: &str) { fn gen(&mut self, expected: &str) {
let generated = encode(&mut self.n, true); let generated = encode(&mut self.n, true);
assert_eq!(generated, expected); assert_eq!(&*generated, expected);
} }
} }
@ -342,7 +346,7 @@ mod tests {
let mut init = RESERVED[0]; let mut init = RESERVED[0];
let target = init + 2; let target = init + 2;
let gen = encode(&mut init, true); let gen = encode(&mut init, true);
assert_eq!(gen, "dp"); assert_eq!(&*gen, "dp");
assert_eq!(init, target); assert_eq!(init, target);
} }