Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f3868b

Browse filesBrowse files
authored
perf(turbopack): Remove extra indirection (#79553)
### What? Remove `VisitorFactory` in favor of directly using `AstModifier`. ### Why? We don't need to allocate twice in this case.
1 parent c810c30 commit 5f3868b
Copy full SHA for 5f3868b

File tree

Expand file treeCollapse file tree

2 files changed

+18
-37
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-37
lines changed

‎turbopack/crates/turbopack-ecmascript/src/code_gen.rs

Copy file name to clipboardExpand all lines: turbopack/crates/turbopack-ecmascript/src/code_gen.rs
+7-19Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::references::{
3939
pub struct CodeGeneration {
4040
/// ast nodes matching the span will be visitor by the visitor
4141
pub root_visitors: Vec<Box<dyn PassFactory>>,
42-
pub visitors: Vec<(Vec<AstParentKind>, Box<dyn VisitorFactory>)>,
42+
pub visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>,
4343
pub hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
4444
pub early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
4545
}
@@ -53,7 +53,7 @@ impl CodeGeneration {
5353

5454
pub fn new(
5555
root_visitors: Vec<Box<dyn PassFactory>>,
56-
visitors: Vec<(Vec<AstParentKind>, Box<dyn VisitorFactory>)>,
56+
visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>,
5757
hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
5858
early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
5959
) -> Self {
@@ -72,7 +72,7 @@ impl CodeGeneration {
7272
}
7373
}
7474

75-
pub fn visitors(visitors: Vec<(Vec<AstParentKind>, Box<dyn VisitorFactory>)>) -> Self {
75+
pub fn visitors(visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>) -> Self {
7676
#[cfg(debug_assertions)]
7777
for (path, _) in visitors.iter() {
7878
if path.is_empty() {
@@ -113,10 +113,6 @@ impl CodeGenerationHoistedStmt {
113113
}
114114
}
115115

116-
pub trait VisitorFactory: Send + Sync {
117-
fn create<'a>(&'a self) -> Box<dyn AstModifier + 'a>;
118-
}
119-
120116
pub trait PassFactory: Send + Sync {
121117
fn create<'a>(&'a self) -> Box<dyn Pass + Send + Sync + 'a>;
122118
}
@@ -279,16 +275,8 @@ macro_rules! create_visitor {
279275
$name: T,
280276
}
281277

282-
impl<T: Fn(&mut swc_core::ecma::ast::$ty) + Send + Sync> $crate::code_gen::VisitorFactory
283-
for Box<Visitor<T>>
284-
{
285-
fn create<'a>(&'a self) -> Box<dyn $crate::code_gen::AstModifier + 'a> {
286-
Box::new(&**self)
287-
}
288-
}
289-
290-
impl<'a, T: Fn(&mut swc_core::ecma::ast::$ty) + Send + Sync> $crate::code_gen::AstModifier
291-
for &'a Visitor<T>
278+
impl<T: Fn(&mut swc_core::ecma::ast::$ty) + Send + Sync> $crate::code_gen::AstModifier
279+
for Visitor<T>
292280
{
293281
fn $name(&self, $arg: &mut swc_core::ecma::ast::$ty) {
294282
(self.$name)($arg);
@@ -303,9 +291,9 @@ macro_rules! create_visitor {
303291

304292
(
305293
$ast_path,
306-
Box::new(Box::new(Visitor {
294+
Box::new(Visitor {
307295
$name: move |$arg: &mut swc_core::ecma::ast::$ty| $b,
308-
})) as Box<dyn $crate::code_gen::VisitorFactory>,
296+
}) as Box<dyn $crate::code_gen::AstModifier>,
309297
)
310298
}
311299
}};

‎turbopack/crates/turbopack-ecmascript/src/path_visitor.rs

Copy file name to clipboardExpand all lines: turbopack/crates/turbopack-ecmascript/src/path_visitor.rs
+11-18Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ use swc_core::{
88
},
99
};
1010

11-
use crate::code_gen::{ModifiableAst, VisitorFactory};
11+
use crate::code_gen::{AstModifier, ModifiableAst};
1212

1313
pub type AstPath = Vec<AstParentKind>;
1414

1515
// Invariant: Each [AstPath] in `visitors` contains a value at position `index`.
1616
pub struct ApplyVisitors<'a, 'b> {
1717
/// `VisitMut` should be shallow. In other words, it should not visit
1818
/// children of the node.
19-
visitors: Cow<'b, [(&'a AstPath, &'a dyn VisitorFactory)]>,
19+
visitors: Cow<'b, [(&'a AstPath, &'a dyn AstModifier)]>,
2020

2121
index: usize,
2222
}
2323

2424
/// Do two binary searches to find the sub-slice that has `path[index] == kind`.
2525
/// Returns None if no item matches that. `visitors` need to be sorted by path.
2626
fn find_range<'a, 'b>(
27-
visitors: &'b [(&'a AstPath, &'a dyn VisitorFactory)],
27+
visitors: &'b [(&'a AstPath, &'a dyn AstModifier)],
2828
kind: &AstParentKind,
2929
index: usize,
30-
) -> Option<&'b [(&'a AstPath, &'a dyn VisitorFactory)]> {
30+
) -> Option<&'b [(&'a AstPath, &'a dyn AstModifier)]> {
3131
// Precondition: visitors is never empty
3232
if visitors.first().unwrap().0[index] > *kind || visitors.last().unwrap().0[index] < *kind {
3333
// Fast path: If ast path of the first visitor is already out of range, then we
@@ -67,7 +67,7 @@ fn find_range<'a, 'b>(
6767

6868
impl<'a> ApplyVisitors<'a, '_> {
6969
/// `visitors` must have an non-empty [AstPath].
70-
pub fn new(mut visitors: Vec<(&'a AstPath, &'a dyn VisitorFactory)>) -> Self {
70+
pub fn new(mut visitors: Vec<(&'a AstPath, &'a dyn AstModifier)>) -> Self {
7171
assert!(!visitors.is_empty());
7272
visitors.sort_by_key(|(path, _)| *path);
7373
Self {
@@ -109,7 +109,7 @@ impl<'a> ApplyVisitors<'a, '_> {
109109
);
110110
}
111111
for (_, visitor) in visitors[..nested_visitors_start].iter() {
112-
n.modify(&*visitor.create());
112+
n.modify(&**visitor);
113113
}
114114
return;
115115
} else {
@@ -172,8 +172,7 @@ mod tests {
172172
testing::run_test,
173173
};
174174

175-
use super::{ApplyVisitors, VisitorFactory};
176-
use crate::code_gen::AstModifier;
175+
use super::{ApplyVisitors, AstModifier};
177176

178177
fn parse(fm: &SourceFile) -> Module {
179178
let mut m = parse_file_as_module(
@@ -198,20 +197,14 @@ mod tests {
198197
to: &'a str,
199198
}
200199

201-
impl VisitorFactory for Box<StrReplacer<'_>> {
202-
fn create<'a>(&'a self) -> Box<dyn AstModifier + 'a> {
203-
Box::new(&**self)
204-
}
205-
}
206-
207-
impl AstModifier for &'_ StrReplacer<'_> {
200+
impl AstModifier for StrReplacer<'_> {
208201
fn visit_mut_str(&self, s: &mut Str) {
209202
s.value = s.value.replace(self.from, self.to).into();
210203
s.raw = None;
211204
}
212205
}
213206

214-
fn replacer(from: &'static str, to: &'static str) -> impl VisitorFactory {
207+
fn replacer(from: &'static str, to: &'static str) -> Box<dyn AstModifier + 'static> {
215208
Box::new(StrReplacer { from, to })
216209
}
217210

@@ -264,7 +257,7 @@ mod tests {
264257

265258
let mut m = m.clone();
266259
m.visit_mut_with_ast_path(
267-
&mut ApplyVisitors::new(vec![(&path, &bar_replacer)]),
260+
&mut ApplyVisitors::new(vec![(&path, &*bar_replacer)]),
268261
&mut Default::default(),
269262
);
270263

@@ -289,7 +282,7 @@ mod tests {
289282

290283
let mut m = m.clone();
291284
m.visit_mut_with_ast_path(
292-
&mut ApplyVisitors::new(vec![(&wrong_path, &bar_replacer)]),
285+
&mut ApplyVisitors::new(vec![(&wrong_path, &*bar_replacer)]),
293286
&mut Default::default(),
294287
);
295288

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.