Skip to content

Commit 6aa89e8

Browse files
committed
perf(mangler): use a single allocation space for temporary vecs
1 parent 9963533 commit 6aa89e8

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

crates/oxc_mangler/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl Mangler {
127127
}
128128

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

@@ -139,9 +140,10 @@ impl Mangler {
139140

140141
if !bindings.is_empty() {
141142
// Sort `bindings` in declaration order.
142-
let mut bindings = bindings.values().copied().collect::<std::vec::Vec<_>>();
143-
bindings.sort_unstable();
144-
for symbol_id in bindings {
143+
tmp_bindings.clear();
144+
tmp_bindings.extend(bindings.values().copied());
145+
tmp_bindings.sort_unstable();
146+
for symbol_id in &tmp_bindings {
145147
slots[symbol_id.index()] = slot;
146148
slot += 1;
147149
}
@@ -203,6 +205,7 @@ impl Mangler {
203205
// function fa() { .. } function ga() { .. }
204206

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

219-
debug_assert!(symbols_renamed_in_this_batch.len() == slice_of_same_len_strings.len());
221+
debug_assert_eq!(symbols_renamed_in_this_batch.len(), slice_of_same_len_strings.len());
220222

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

0 commit comments

Comments
 (0)