From 963a2d1b850fac2ab6fd779b2876547ec6545f05 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Sat, 27 Jul 2024 10:55:49 +0000 Subject: [PATCH] perf(mangler): reduce unnecessary allocation (#4498) There seems to be no noticeable change in performance. --- crates/oxc_mangler/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/oxc_mangler/src/lib.rs b/crates/oxc_mangler/src/lib.rs index 68457e1d16f66..041bd1ed2acb3 100644 --- a/crates/oxc_mangler/src/lib.rs +++ b/crates/oxc_mangler/src/lib.rs @@ -105,8 +105,8 @@ impl ManglerBuilder { if !bindings.is_empty() { // `bindings` are stored in order, traverse and increment slot - for symbol_id in bindings.values() { - slots[*symbol_id] = slot; + for symbol_id in bindings.values().copied() { + slots[symbol_id] = slot; slot += 1; } } @@ -121,9 +121,6 @@ impl ManglerBuilder { let frequencies = Self::tally_slot_frequencies(&symbol_table, total_number_of_slots, &slots); - let unresolved_references = - scope_tree.root_unresolved_references().keys().collect::>(); - let mut names = Vec::with_capacity(total_number_of_slots); let generate_name = if self.debug { debug_name } else { base54 }; @@ -133,7 +130,9 @@ impl ManglerBuilder { let name = generate_name(count); count += 1; // Do not mangle keywords and unresolved references - if !is_keyword(&name) && !unresolved_references.iter().any(|n| **n == name) { + if !is_keyword(&name) + && !scope_tree.root_unresolved_references().contains_key(name.as_str()) + { break name; } }); @@ -169,7 +168,7 @@ impl ManglerBuilder { // sorting by slot enables us to sort by the order at which the vars first appear in the source // (this is possible because the slots are discovered currently in a DFS method which is the same order // as variables appear in the source code) - symbols_renamed_in_this_batch.sort_by(|a, b| a.slot.cmp(&b.slot.clone())); + symbols_renamed_in_this_batch.sort_by(|a, b| a.slot.cmp(&b.slot)); // here we just zip the iterator of symbols to rename with the iterator of new names for the next for loop let symbols_to_rename_with_new_names =