Skip to content

Commit

Permalink
perf(mangler): use a single allocation space for temporary vecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 15, 2025
1 parent 9963533 commit 6aa89e8
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl Mangler {
}

// Walk the scope tree and compute the slot number for each scope
let mut tmp_bindings = std::vec::Vec::with_capacity(100);
for scope_id in scope_tree.descendants_from_root() {
let bindings = scope_tree.get_bindings(scope_id);

Expand All @@ -139,9 +140,10 @@ impl Mangler {

if !bindings.is_empty() {
// Sort `bindings` in declaration order.
let mut bindings = bindings.values().copied().collect::<std::vec::Vec<_>>();
bindings.sort_unstable();
for symbol_id in bindings {
tmp_bindings.clear();
tmp_bindings.extend(bindings.values().copied());
tmp_bindings.sort_unstable();
for symbol_id in &tmp_bindings {
slots[symbol_id.index()] = slot;
slot += 1;
}
Expand Down Expand Up @@ -203,6 +205,7 @@ impl Mangler {
// function fa() { .. } function ga() { .. }

let mut freq_iter = frequencies.iter();
let mut symbols_renamed_in_this_batch = std::vec::Vec::with_capacity(100);
// 2. "N number of vars are going to be assigned names of the same length"
for (_, slice_of_same_len_strings_group) in
&reserved_names.into_iter().chunk_by(CompactStr::len)
Expand All @@ -211,12 +214,11 @@ impl Mangler {
// (freq_iter is sorted by frequency from highest to lowest,
// so taking means take the N most frequent symbols remaining)
let slice_of_same_len_strings = slice_of_same_len_strings_group.collect_vec();
let mut symbols_renamed_in_this_batch = freq_iter
.by_ref()
.take(slice_of_same_len_strings.len())
.collect::<std::vec::Vec<_>>();
symbols_renamed_in_this_batch.clear();
symbols_renamed_in_this_batch
.extend(freq_iter.by_ref().take(slice_of_same_len_strings.len()));

debug_assert!(symbols_renamed_in_this_batch.len() == slice_of_same_len_strings.len());
debug_assert_eq!(symbols_renamed_in_this_batch.len(), slice_of_same_len_strings.len());

// 2. "we assign the N names based on the order at which the vars first appear in the source."
// sorting by slot enables us to sort by the order at which the vars first appear in the source
Expand Down

0 comments on commit 6aa89e8

Please sign in to comment.