Skip to content

Commit 42b4caf

Browse files
authored
fix(es/renamer): Remove FastJsWord (#9136)
**Description:** Actually it was slower. ``` Starting recording with the Time Profiler template. Launching process: full-4ec72b7d24d0fcd8. Ctrl-C to stop the recording Gnuplot not found, using plotters backend Benchmarking es/minify/libraries/antd Benchmarking es/minify/libraries/antd: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 5.2s. Benchmarking es/minify/libraries/antd: Collecting 10 samples in estimated 5.1501 s (10 iterations) Benchmarking es/minify/libraries/antd: Analyzing es/minify/libraries/antd time: [517.09 ms 521.65 ms 527.04 ms] change: [-5.0960% -4.1320% -3.0084%] (p = 0.00 < 0.05) Performance has improved. Benchmarking es/minify/libraries/d3 Benchmarking es/minify/libraries/d3: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 7.3s or enable flat sampling. Benchmarking es/minify/libraries/d3: Collecting 10 samples in estimated 7.3000 s (55 iterations) Benchmarking es/minify/libraries/d3: Analyzing es/minify/libraries/d3 time: [132.42 ms 132.66 ms 132.93 ms] change: [-5.0809% -4.6687% -4.2423%] (p = 0.00 < 0.05) Performance has improved. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high mild Benchmarking es/minify/libraries/echarts Benchmarking es/minify/libraries/echarts: Warming up for 3.0000 s Benchmarking es/minify/libraries/echarts: Collecting 10 samples in estimated 8.8664 s (20 iterations) Benchmarking es/minify/libraries/echarts: Analyzing es/minify/libraries/echarts time: [441.89 ms 442.98 ms 444.21 ms] change: [-5.8436% -5.4580% -5.0604%] (p = 0.00 < 0.05) Performance has improved. Benchmarking es/minify/libraries/jquery Benchmarking es/minify/libraries/jquery: Warming up for 3.0000 s Benchmarking es/minify/libraries/jquery: Collecting 10 samples in estimated 7.2124 s (165 iterations) Benchmarking es/minify/libraries/jquery: Analyzing es/minify/libraries/jquery time: [43.549 ms 43.624 ms 43.703 ms] change: [-3.3699% -3.0686% -2.7597%] (p = 0.00 < 0.05) Performance has improved. Benchmarking es/minify/libraries/lodash Benchmarking es/minify/libraries/lodash: Warming up for 3.0000 s Benchmarking es/minify/libraries/lodash: Collecting 10 samples in estimated 6.5048 s (110 iterations) Benchmarking es/minify/libraries/lodash: Analyzing es/minify/libraries/lodash time: [58.951 ms 59.114 ms 59.290 ms] change: [-1.2723% -0.7019% -0.0217%] (p = 0.06 > 0.05) No change in performance detected. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe Benchmarking es/minify/libraries/moment Benchmarking es/minify/libraries/moment: Warming up for 3.0000 s Benchmarking es/minify/libraries/moment: Collecting 10 samples in estimated 5.5497 s (220 iterations) Benchmarking es/minify/libraries/moment: Analyzing es/minify/libraries/moment time: [25.261 ms 25.313 ms 25.355 ms] change: [-1.4771% -0.9851% -0.5957%] (p = 0.00 < 0.05) Change within noise threshold. Benchmarking es/minify/libraries/react Benchmarking es/minify/libraries/react: Warming up for 3.0000 s Benchmarking es/minify/libraries/react: Collecting 10 samples in estimated 5.1132 s (605 iterations) Benchmarking es/minify/libraries/react: Analyzing es/minify/libraries/react time: [8.4265 ms 8.4431 ms 8.4624 ms] change: [-1.3741% -1.0134% -0.6710%] (p = 0.00 < 0.05) Change within noise threshold. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high mild Benchmarking es/minify/libraries/terser Benchmarking es/minify/libraries/terser: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 5.6s or enable flat sampling. Benchmarking es/minify/libraries/terser: Collecting 10 samples in estimated 5.5614 s (55 iterations) Benchmarking es/minify/libraries/terser: Analyzing es/minify/libraries/terser time: [100.87 ms 100.98 ms 101.15 ms] change: [-6.7656% -6.1477% -5.4618%] (p = 0.00 < 0.05) Performance has improved. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe Benchmarking es/minify/libraries/three Benchmarking es/minify/libraries/three: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 9.3s or enable flat sampling. Benchmarking es/minify/libraries/three: Collecting 10 samples in estimated 9.3499 s (55 iterations) Benchmarking es/minify/libraries/three: Analyzing es/minify/libraries/three time: [170.12 ms 170.44 ms 170.81 ms] change: [-6.9164% -6.4564% -5.9450%] (p = 0.00 < 0.05) Performance has improved. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high severe Benchmarking es/minify/libraries/typescript Benchmarking es/minify/libraries/typescript: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 9.3s. Benchmarking es/minify/libraries/typescript: Collecting 10 samples in estimated 9.2642 s (10 iterations) Benchmarking es/minify/libraries/typescript: Analyzing es/minify/libraries/typescript time: [927.57 ms 929.26 ms 930.85 ms] change: [-4.4792% -4.1803% -3.9000%] (p = 0.00 < 0.05) Performance has improved. Benchmarking es/minify/libraries/victory Benchmarking es/minify/libraries/victory: Warming up for 3.0000 s Benchmarking es/minify/libraries/victory: Collecting 10 samples in estimated 7.0301 s (30 iterations) Benchmarking es/minify/libraries/victory: Analyzing es/minify/libraries/victory time: [233.98 ms 234.44 ms 234.96 ms] change: [-7.2681% -6.7396% -6.2554%] (p = 0.00 < 0.05) Performance has improved. Benchmarking es/minify/libraries/vue Benchmarking es/minify/libraries/vue: Warming up for 3.0000 s Benchmarking es/minify/libraries/vue: Collecting 10 samples in estimated 6.7241 s (110 iterations) Benchmarking es/minify/libraries/vue: Analyzing es/minify/libraries/vue time: [61.051 ms 61.124 ms 61.225 ms] change: [-2.3890% -2.1697% -1.9536%] (p = 0.00 < 0.05) Performance has improved. ``` **Related issue:** - Closes #9126
1 parent f70e071 commit 42b4caf

File tree

3 files changed

+24
-70
lines changed

3 files changed

+24
-70
lines changed

crates/swc_ecma_transforms_base/src/rename/analyzer/reverse_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
use rustc_hash::FxHashMap;
2-
3-
use super::scope::{FastId, FastJsWord};
2+
use swc_atoms::Atom;
3+
use swc_ecma_ast::Id;
44

55
#[derive(Debug, Default)]
66
pub(crate) struct ReverseMap<'a> {
77
prev: Option<&'a ReverseMap<'a>>,
88

9-
inner: FxHashMap<FastJsWord, Vec<FastId>>,
9+
inner: FxHashMap<Atom, Vec<Id>>,
1010
}
1111

1212
impl ReverseMap<'_> {
13-
pub fn push_entry(&mut self, key: FastJsWord, id: FastId) {
13+
pub fn push_entry(&mut self, key: Atom, id: Id) {
1414
self.inner.entry(key).or_default().push(id);
1515
}
1616

1717
fn iter(&self) -> Iter {
1818
Iter { cur: Some(self) }
1919
}
2020

21-
pub fn get<'a>(&'a self, key: &'a FastJsWord) -> impl Iterator<Item = &'a FastId> + 'a {
21+
pub fn get<'a>(&'a self, key: &'a Atom) -> impl Iterator<Item = &'a Id> + 'a {
2222
self.iter()
2323
.filter_map(|v| v.inner.get(key))
2424
.flat_map(|v| v.iter())

crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs

+18-62
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
#[cfg(feature = "concurrent-renamer")]
99
use rayon::prelude::*;
1010
use rustc_hash::FxHashSet;
11-
use swc_atoms::JsWord;
11+
use swc_atoms::Atom;
1212
use swc_common::{collections::AHashMap, util::take::Take, Mark, SyntaxContext};
1313
use swc_ecma_ast::*;
1414
use tracing::debug;
@@ -36,38 +36,7 @@ pub(crate) struct Scope {
3636
pub(super) children: Vec<Scope>,
3737
}
3838

39-
/// [JsWord] without clone or drop. This is unsafe and creator should ensure
40-
/// that [JsWord] stored in this type is not dropped until all operations are
41-
/// finished.
42-
#[repr(transparent)]
43-
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
44-
pub(crate) struct FastJsWord(ManuallyDrop<JsWord>);
45-
46-
impl Clone for FastJsWord {
47-
fn clone(&self) -> Self {
48-
unsafe { Self(ManuallyDrop::new(transmute_copy(&self.0))) }
49-
}
50-
}
51-
52-
impl Display for FastJsWord {
53-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54-
Display::fmt(&*self.0, f)
55-
}
56-
}
57-
58-
impl FastJsWord {
59-
pub fn new(src: JsWord) -> Self {
60-
FastJsWord(ManuallyDrop::new(src))
61-
}
62-
63-
pub fn into_inner(self) -> JsWord {
64-
ManuallyDrop::into_inner(self.0)
65-
}
66-
}
67-
68-
pub(crate) type FastId = (FastJsWord, SyntaxContext);
69-
70-
pub(crate) type RenameMap = AHashMap<FastId, JsWord>;
39+
pub(crate) type RenameMap = AHashMap<Id, Atom>;
7140

7241
#[derive(Debug, Default)]
7342
pub(super) struct ScopeData {
@@ -77,7 +46,7 @@ pub(super) struct ScopeData {
7746
///
7847
/// If the add-only contraint is violated, it is very likely to be a bug,
7948
/// because we merge every items in children to current scope.
80-
all: FxHashSet<FastId>,
49+
all: FxHashSet<Id>,
8150

8251
queue: Vec<Id>,
8352
}
@@ -88,9 +57,7 @@ impl Scope {
8857
return;
8958
}
9059

91-
let fid = fast_id(id.clone());
92-
93-
self.data.all.insert(fid);
60+
self.data.all.insert(id.clone());
9461

9562
if !self.data.queue.contains(id) {
9663
if has_eval && id.1.outer().is_descendant_of(top_level_mark) {
@@ -106,7 +73,7 @@ impl Scope {
10673
return;
10774
}
10875

109-
self.data.all.insert(fast_id(id));
76+
self.data.all.insert(id);
11077
}
11178

11279
/// Copy `children.data.all` to `self.data.all`.
@@ -124,7 +91,7 @@ impl Scope {
12491
to: &mut RenameMap,
12592
previous: &RenameMap,
12693
reverse: &mut ReverseMap,
127-
preserved_symbols: &FxHashSet<JsWord>,
94+
preserved_symbols: &FxHashSet<Atom>,
12895
) where
12996
R: Renamer,
13097
{
@@ -159,15 +126,14 @@ impl Scope {
159126
previous: &RenameMap,
160127
reverse: &mut ReverseMap,
161128
queue: Vec<Id>,
162-
preserved_symbols: &FxHashSet<JsWord>,
129+
preserved_symbols: &FxHashSet<Atom>,
163130
) where
164131
R: Renamer,
165132
{
166133
let mut n = 0;
167134

168135
for id in queue {
169-
let fid = fast_id(id.clone());
170-
if to.get(&fid).is_some() || previous.get(&fid).is_some() || id.0 == "eval" {
136+
if to.get(&id).is_some() || previous.get(&id).is_some() || id.0 == "eval" {
171137
continue;
172138
}
173139

@@ -181,29 +147,27 @@ impl Scope {
181147
if preserved_symbols.contains(&sym) {
182148
continue;
183149
}
184-
let sym = FastJsWord::new(sym);
185150

186151
if self.can_rename(&id, &sym, reverse) {
187152
if cfg!(debug_assertions) {
188153
debug!("Renaming `{}{:?}` to `{}`", id.0, id.1, sym);
189154
}
190155

191-
let fid = fast_id(id);
192-
reverse.push_entry(sym.clone(), fid.clone());
193-
to.insert(fid, sym.into_inner());
156+
reverse.push_entry(sym.clone(), id.clone());
157+
to.insert(id, sym);
194158

195159
break;
196160
}
197161
}
198162
}
199163
}
200164

201-
fn can_rename(&self, id: &Id, symbol: &FastJsWord, reverse: &ReverseMap) -> bool {
165+
fn can_rename(&self, id: &Id, symbol: &Atom, reverse: &ReverseMap) -> bool {
202166
// We can optimize this
203167
// We only need to check the current scope and parents (ignoring `a` generated
204168
// for unrelated scopes)
205169
for left in reverse.get(symbol) {
206-
if left.1 == id.1 && *left.0 .0 == id.0 {
170+
if left.1 == id.1 && *left.0 == id.0 {
207171
continue;
208172
}
209173

@@ -223,7 +187,7 @@ impl Scope {
223187
previous: &RenameMap,
224188
reverse: &ReverseMap,
225189
preserved: &FxHashSet<Id>,
226-
preserved_symbols: &FxHashSet<JsWord>,
190+
preserved_symbols: &FxHashSet<Atom>,
227191
parallel: bool,
228192
) where
229193
R: Renamer,
@@ -294,17 +258,16 @@ impl Scope {
294258
reverse: &mut ReverseMap,
295259
queue: Vec<Id>,
296260
preserved: &FxHashSet<Id>,
297-
preserved_symbols: &FxHashSet<JsWord>,
261+
preserved_symbols: &FxHashSet<Atom>,
298262
) where
299263
R: Renamer,
300264
{
301265
let mut n = 0;
302266

303267
for id in queue {
304-
let fid = fast_id(id.clone());
305268
if preserved.contains(&id)
306-
|| to.get(&fid).is_some()
307-
|| previous.get(&fid).is_some()
269+
|| to.get(&id).is_some()
270+
|| previous.get(&id).is_some()
308271
|| id.0 == "eval"
309272
{
310273
continue;
@@ -318,17 +281,14 @@ impl Scope {
318281
continue;
319282
}
320283

321-
let sym = FastJsWord::new(sym);
322-
323284
if self.can_rename(&id, &sym, reverse) {
324285
#[cfg(debug_assertions)]
325286
{
326287
debug!("mangle: `{}{:?}` -> {}", id.0, id.1, sym);
327288
}
328289

329-
let fid = fast_id(id.clone());
330-
reverse.push_entry(sym.clone(), fid.clone());
331-
to.insert(fid.clone(), sym.into_inner());
290+
reverse.push_entry(sym.clone(), id.clone());
291+
to.insert(id.clone(), sym);
332292
// self.data.decls.remove(&id);
333293
// self.data.usages.remove(&id);
334294

@@ -343,7 +303,3 @@ impl Scope {
343303
self.data.queue.len() + children.iter().map(|v| v.rename_cost()).sum::<usize>()
344304
}
345305
}
346-
347-
fn fast_id(id: Id) -> FastId {
348-
(FastJsWord::new(id.0), id.1)
349-
}

crates/swc_ecma_transforms_base/src/rename/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ where
184184
);
185185
}
186186

187-
map.into_iter()
188-
.map(|((s, ctxt), v)| ((s.into_inner(), ctxt), v))
189-
.collect()
187+
map
190188
}
191189
}
192190

0 commit comments

Comments
 (0)