Skip to content

Commit cf3db7b

Browse files
committed
perf(javascript): streamline parser walker hot paths
1 parent 90d54b4 commit cf3db7b

5 files changed

Lines changed: 47 additions & 46 deletions

File tree

crates/rspack_plugin_javascript/src/parser_plugin/inner_graph/plugin.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ impl InnerGraphParserPlugin {
105105
non_terminal[symbol.index()] = true;
106106
remaining += 1;
107107
}
108-
let mut processed = (0..symbol_count)
109-
.map(|_| HashSet::default())
110-
.collect::<Vec<HashSet<InnerGraphMapSetValue>>>();
108+
let mut processed = vec![InnerGraphMapSet::default(); symbol_count];
111109

112110
while remaining != 0 {
113111
let mut keys_to_remove = vec![];
@@ -116,7 +114,7 @@ impl InnerGraphParserPlugin {
116114
continue;
117115
}
118116
let key = TopLevelSymbol::from_index(index);
119-
let mut new_set = HashSet::default();
117+
let mut new_set = InnerGraphMapSet::default();
120118
// Using enum to manipulate original is pretty hard, so I use an extra variable to
121119
// flagging the new set has changed to boolean `true`
122120
// you could refer https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/optimize/InnerGraph.js#L150
@@ -169,7 +167,7 @@ impl InnerGraphParserPlugin {
169167
} else if new_set.is_empty() {
170168
state.set_graph(key, InnerGraphMapValue::Nil);
171169
} else {
172-
state.set_graph(key, InnerGraphMapValue::Set(new_set.into()));
170+
state.set_graph(key, InnerGraphMapValue::Set(new_set));
173171
}
174172
}
175173

crates/rspack_plugin_javascript/src/parser_plugin/inner_graph/state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl InnerGraphMapSet {
120120
}
121121
}
122122

123+
pub(super) fn is_empty(&self) -> bool {
124+
self.len() == 0
125+
}
126+
123127
pub(super) fn iter(
124128
&self,
125129
) -> Either<

crates/rspack_plugin_javascript/src/visitors/dependency/parser/call_hooks_name.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ where
7575
{
7676
let ast = parser.ast.ast;
7777
let name = ast.get_utf8(identifier.name(ast));
78-
let variable = parser.get_variable_info_id_for_identifier(identifier);
79-
let result = if let Some(id) = variable {
80-
call_hooks_info(id, parser, hook_call)
81-
} else {
82-
hook_call(parser, name)
78+
let variable = parser.get_variable_info_id_for_identifier(identifier, name);
79+
let result = match variable {
80+
Some(id) if id == parser.semantic_normal_variable => None,
81+
Some(id) => call_hooks_info(id, parser, hook_call),
82+
None => hook_call(parser, name),
8383
};
8484
(result, variable)
8585
}
@@ -133,7 +133,7 @@ impl CallHooksName for MemberExpression {
133133
if members.is_empty() {
134134
expr_name.root_info.call_hooks_name(parser, hook_call)
135135
} else {
136-
expr_name.name.call_hooks_name(parser, hook_call)
136+
hook_call(parser, &expr_name.name)
137137
}
138138
}
139139
}
@@ -160,7 +160,7 @@ impl CallHooksName for ChainExpression {
160160
if members.is_empty() {
161161
expr_name.root_info.call_hooks_name(parser, hook_call)
162162
} else {
163-
expr_name.name.call_hooks_name(parser, hook_call)
163+
hook_call(parser, &expr_name.name)
164164
}
165165
}
166166
}

crates/rspack_plugin_javascript/src/visitors/dependency/parser/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ impl<'parser> JavascriptParser<'parser> {
684684
let definitions = db.create();
685685
let semantic_normal_variable =
686686
VariableInfo::create(&mut db, definitions, None, VariableInfoFlags::NORMAL, None);
687-
688687
Self {
689688
last_esm_import_order: 0,
690689
ast,
@@ -1133,9 +1132,8 @@ impl<'parser> JavascriptParser<'parser> {
11331132
pub(super) fn get_variable_info_id_for_identifier(
11341133
&mut self,
11351134
identifier: IdentifierReference,
1135+
name: &str,
11361136
) -> Option<VariableInfoId> {
1137-
let ast = self.ast.ast;
1138-
let name = ast.get_utf8(identifier.name(ast));
11391137
let Some(reference) = self
11401138
.ast
11411139
.semantic
@@ -1190,8 +1188,9 @@ impl<'parser> JavascriptParser<'parser> {
11901188
pub(super) fn get_variable_info_for_identifier(
11911189
&mut self,
11921190
identifier: IdentifierReference,
1191+
name: &str,
11931192
) -> Option<&VariableInfo> {
1194-
let id = self.get_variable_info_id_for_identifier(identifier)?;
1193+
let id = self.get_variable_info_id_for_identifier(identifier, name)?;
11951194
self.variable_info_from_id(id)
11961195
}
11971196

@@ -1348,7 +1347,7 @@ impl<'parser> JavascriptParser<'parser> {
13481347
) -> Option<NameInfo<'_>> {
13491348
let ast = self.ast.ast;
13501349
let name = ast.get_utf8(identifier.name(ast));
1351-
let Some(info) = self.get_variable_info_for_identifier(identifier) else {
1350+
let Some(info) = self.get_variable_info_for_identifier(identifier, name) else {
13521351
return Some(NameInfo { name, info: None });
13531352
};
13541353
let Some(resolved_name) = &info.name else {
@@ -1612,9 +1611,9 @@ impl<'parser> JavascriptParser<'parser> {
16121611
} else {
16131612
(ExprRef::from_expr(ast, callee), RawAtomMembers::new())
16141613
};
1615-
let root_name = root.get_root_name(ast)?;
16161614
let NameInfo {
1617-
info: root_info, ..
1615+
name: root_name,
1616+
info: root_info,
16181617
} = self.get_name_info_from_root(root)?;
16191618

16201619
let mut root_members = materialize_member_atoms(ast, root_members);
@@ -1639,8 +1638,6 @@ impl<'parser> JavascriptParser<'parser> {
16391638
if !allowed_types.contains(AllowedMemberTypes::Expression) {
16401639
return None;
16411640
}
1642-
let root_name = object.get_root_name(ast)?;
1643-
16441641
let NameInfo {
16451642
name: resolved_root,
16461643
info: root_info,
@@ -1654,7 +1651,7 @@ impl<'parser> JavascriptParser<'parser> {
16541651
Some(MemberExpressionInfo::Expression(ExpressionExpressionInfo {
16551652
name,
16561653
root_info: root_info.map_or_else(
1657-
|| ExportedVariableInfo::Name(Atom::from(root_name)),
1654+
|| ExportedVariableInfo::Name(Atom::from(resolved_root)),
16581655
|i| ExportedVariableInfo::VariableInfo(i.id()),
16591656
),
16601657
members,
@@ -2127,6 +2124,9 @@ impl<'parser> JavascriptParser<'parser> {
21272124
});
21282125
evaluated.or_else(|| {
21292126
if let Some(variable) = variable {
2127+
if variable == self.semantic_normal_variable {
2128+
return None;
2129+
}
21302130
let info = self.definitions_db.expect_get_variable(variable);
21312131
if let Some(name) = &info.name
21322132
&& (info.is_free() || info.is_tagged())

crates/rspack_plugin_javascript/src/visitors/dependency/parser/walk.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,14 @@ impl JavascriptParser<'_> {
914914
// we use `AllowedMemberTypes::Expression` above
915915
unreachable!();
916916
};
917-
if expr_info
918-
.name
919-
.call_hooks_name(self, |this, for_name| drive.r#typeof(this, expr, for_name))
920-
.unwrap_or_default()
921-
{
917+
let handled = if expr_info.members.is_empty() {
918+
expr_info
919+
.root_info
920+
.call_hooks_name(self, |this, for_name| drive.r#typeof(this, expr, for_name))
921+
} else {
922+
drive.r#typeof(self, expr, &expr_info.name)
923+
};
924+
if handled.unwrap_or_default() {
922925
return;
923926
}
924927
};
@@ -1108,11 +1111,8 @@ impl JavascriptParser<'_> {
11081111
member_ranges,
11091112
};
11101113
let drive = self.plugin_drive.clone();
1111-
if expression_info
1112-
.name
1113-
.call_hooks_name(self, |this, for_name| {
1114-
drive.member(this, member.into(), for_name)
1115-
})
1114+
if drive
1115+
.member(self, member.into(), &expression_info.name)
11161116
.unwrap_or_default()
11171117
{
11181118
return;
@@ -1224,12 +1224,10 @@ impl JavascriptParser<'_> {
12241224
.new_expression(parser, expr, for_name)
12251225
})
12261226
} else {
1227-
info.name.call_hooks_name(self, |parser, for_name| {
1228-
parser
1229-
.plugin_drive
1230-
.clone()
1231-
.new_expression(parser, expr, for_name)
1232-
})
1227+
self
1228+
.plugin_drive
1229+
.clone()
1230+
.new_expression(self, expr, &info.name)
12331231
};
12341232
if result.unwrap_or_default() {
12351233
return;
@@ -1367,11 +1365,8 @@ impl JavascriptParser<'_> {
13671365
if let Some(expr_info) = expr_info {
13681366
match expr_info {
13691367
MemberExpressionInfo::Expression(expr_info) => {
1370-
if expr_info
1371-
.name
1372-
.call_hooks_name(self, |this, for_name| {
1373-
drive.member(this, expr.into(), for_name)
1374-
})
1368+
if drive
1369+
.member(self, expr.into(), &expr_info.name)
13751370
.unwrap_or_default()
13761371
{
13771372
return;
@@ -1942,11 +1937,15 @@ impl JavascriptParser<'_> {
19421937
}
19431938

19441939
fn walk_identifier(&mut self, identifier: IdentifierReference) {
1945-
let ast = self.ast.ast;
1946-
let span = identifier.span(ast);
19471940
let drive = self.plugin_drive.clone();
19481941
identifier.call_hooks_name(self, |this, for_name| {
1949-
drive.identifier(this, &Identifier { span }, for_name)
1942+
drive.identifier(
1943+
this,
1944+
&Identifier {
1945+
span: identifier.span(this.ast.ast),
1946+
},
1947+
for_name,
1948+
)
19501949
});
19511950
}
19521951

0 commit comments

Comments
 (0)