Skip to content

Commit ad48846

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

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

crates/oxc_mangler/src/lib.rs

+13-9
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,20 +205,22 @@ 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);
209+
let mut slice_of_same_len_strings = std::vec::Vec::with_capacity(100);
206210
// 2. "N number of vars are going to be assigned names of the same length"
207211
for (_, slice_of_same_len_strings_group) in
208212
&reserved_names.into_iter().chunk_by(CompactStr::len)
209213
{
210214
// 1. "The most frequent vars get the shorter names"
211215
// (freq_iter is sorted by frequency from highest to lowest,
212216
// so taking means take the N most frequent symbols remaining)
213-
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+
slice_of_same_len_strings.clear();
218+
slice_of_same_len_strings.extend(slice_of_same_len_strings_group);
219+
symbols_renamed_in_this_batch.clear();
220+
symbols_renamed_in_this_batch
221+
.extend(freq_iter.by_ref().take(slice_of_same_len_strings.len()));
218222

219-
debug_assert!(symbols_renamed_in_this_batch.len() == slice_of_same_len_strings.len());
223+
debug_assert_eq!(symbols_renamed_in_this_batch.len(), slice_of_same_len_strings.len());
220224

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

0 commit comments

Comments
 (0)