Skip to content

Commit

Permalink
perf(mangler): reduce unnecessary allocation (#4498)
Browse files Browse the repository at this point in the history
There seems to be no noticeable change in performance.
  • Loading branch information
Dunqing committed Jul 27, 2024
1 parent 5db7bed commit 963a2d1
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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::<Vec<_>>();

let mut names = Vec::with_capacity(total_number_of_slots);

let generate_name = if self.debug { debug_name } else { base54 };
Expand All @@ -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;
}
});
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 963a2d1

Please sign in to comment.