perf(mangler): reduce unnecessary allocation (#4498)

There seems to be no noticeable change in performance.
This commit is contained in:
Dunqing 2024-07-27 10:55:49 +00:00
parent 5db7bed4f5
commit 963a2d1b85

View file

@ -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::<Vec<_>>();
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 =