Skip to content

Commit 4edcc3e

Browse files
committed
perf: init
1 parent 748a372 commit 4edcc3e

File tree

9 files changed

+63
-46
lines changed

9 files changed

+63
-46
lines changed

crates/rspack_identifier/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,25 @@ impl From<Identifier> for Ustr {
5858
}
5959
}
6060

61+
impl Identifier {
62+
/// Convert [Identifier] to [String]
63+
///
64+
/// Shadowed the [fmt::Display] to specialize `to_string`,
65+
/// like how other structs are shadowed in the standard library.
66+
/// See: https://github.com/rust-lang/rust/pull/32586
67+
///
68+
/// Consistency:
69+
/// The result of `to_string` should be the same as the result of [fmt::Display::fmt].
70+
#[allow(clippy::inherent_to_string_shadow_display)]
71+
pub fn to_string(&self) -> String {
72+
self.0.to_owned()
73+
}
74+
}
75+
6176
impl fmt::Display for Identifier {
77+
/// Consistency:
78+
/// The result of `to_string` should be the same as the result of [fmt::Display::fmt].
6279
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63-
write!(f, "{}", self.0.as_str())
80+
write!(f, "{}", self.to_string())
6481
}
6582
}

crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl JavascriptParserPlugin for CompatibilityPlugin {
154154
}
155155
let tag_info = parser
156156
.definitions_db
157-
.expect_get_mut_tag_info(&parser.current_tag_info?);
157+
.expect_get_mut_tag_info(parser.current_tag_info?);
158158

159159
let mut nested_require_data = NestedRequireData::downcast(tag_info.data.take()?);
160160
let mut deps = Vec::with_capacity(2);

crates/rspack_plugin_javascript/src/parser_plugin/harmony_import_dependency_parser_plugin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl JavascriptParserPlugin for HarmonyImportDependencyParserPlugin {
124124
}
125125
let tag_info = parser
126126
.definitions_db
127-
.expect_get_tag_info(&parser.current_tag_info?);
127+
.expect_get_tag_info(parser.current_tag_info?);
128128
let settings = HarmonySpecifierData::downcast(tag_info.data.clone()?);
129129

130130
let dep = HarmonyImportSpecifierDependency::new(
@@ -178,7 +178,7 @@ impl JavascriptParserPlugin for HarmonyImportDependencyParserPlugin {
178178
}
179179
let tag_info = parser
180180
.definitions_db
181-
.expect_get_tag_info(&parser.current_tag_info?);
181+
.expect_get_tag_info(parser.current_tag_info?);
182182
let settings = HarmonySpecifierData::downcast(tag_info.data.clone()?);
183183

184184
let non_optional_members = get_non_optional_part(members, members_optionals);
@@ -243,7 +243,7 @@ impl JavascriptParserPlugin for HarmonyImportDependencyParserPlugin {
243243
}
244244
let tag_info = parser
245245
.definitions_db
246-
.expect_get_tag_info(&parser.current_tag_info?);
246+
.expect_get_tag_info(parser.current_tag_info?);
247247
let settings = HarmonySpecifierData::downcast(tag_info.data.clone()?);
248248

249249
let non_optional_members = get_non_optional_part(members, members_optionals);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl InnerGraphPlugin {
8686
}
8787

8888
if let Some(tag_info) = parser.current_tag_info {
89-
let tag_info = parser.definitions_db.expect_get_tag_info(&tag_info);
89+
let tag_info = parser.definitions_db.expect_get_tag_info(tag_info);
9090
let symbol = TopLevelSymbol::downcast(tag_info.data.clone().expect("should have data"));
9191
let usage = parser.inner_graph.get_top_level_symbol();
9292
parser.inner_graph.add_usage(
@@ -295,7 +295,7 @@ impl InnerGraphPlugin {
295295
let existing = parser.get_variable_info(name);
296296
if let Some(existing) = existing
297297
&& let Some(tag_info) = existing.tag_info
298-
&& let tag_info = parser.definitions_db.expect_get_mut_tag_info(&tag_info)
298+
&& let tag_info = parser.definitions_db.expect_get_mut_tag_info(tag_info)
299299
&& tag_info.tag == TOP_LEVEL_SYMBOL
300300
&& let Some(tag_data) = tag_info.data.clone()
301301
{

crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl JavascriptParserPlugin for WorkerPlugin {
304304
}
305305
let tag_info = parser
306306
.definitions_db
307-
.expect_get_tag_info(&parser.current_tag_info?);
307+
.expect_get_tag_info(parser.current_tag_info?);
308308
let data = WorkerSpecifierData::downcast(tag_info.data.clone()?);
309309
if let Some(value) = self.pattern_syntax.get(data.key.as_str())
310310
&& value.contains(&members.iter().map(|id| id.as_str()).join("."))
@@ -331,7 +331,7 @@ impl JavascriptParserPlugin for WorkerPlugin {
331331
if for_name == HARMONY_SPECIFIER_TAG {
332332
let tag_info = parser
333333
.definitions_db
334-
.expect_get_tag_info(&parser.current_tag_info?);
334+
.expect_get_tag_info(parser.current_tag_info?);
335335
let settings = HarmonySpecifierData::downcast(tag_info.data.clone()?);
336336
let ids = settings.ids.iter().map(|id| id.as_str()).join(".");
337337
if self
@@ -371,7 +371,7 @@ impl JavascriptParserPlugin for WorkerPlugin {
371371
if for_name == HARMONY_SPECIFIER_TAG {
372372
let tag_info = parser
373373
.definitions_db
374-
.expect_get_tag_info(&parser.current_tag_info?);
374+
.expect_get_tag_info(parser.current_tag_info?);
375375
let settings = HarmonySpecifierData::downcast(tag_info.data.clone()?);
376376
let ids = settings.ids.iter().map(|id| id.as_str()).join(".");
377377
if self

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ fn call_hooks_info<F, T>(
118118
where
119119
F: Fn(&mut JavascriptParser, &str) -> Option<T>,
120120
{
121-
let info = parser.definitions_db.expect_get_variable(&id);
121+
let info = parser.definitions_db.expect_get_variable(id);
122122
let mut next_tag_info = info.tag_info;
123123

124124
while let Some(tag_info_id) = next_tag_info {
125125
parser.current_tag_info = Some(tag_info_id);
126-
let tag_info = parser.definitions_db.expect_get_tag_info(&tag_info_id);
126+
let tag_info = parser.definitions_db.expect_get_tag_info(tag_info_id);
127127
let tag = tag_info.tag.to_string();
128128
let next = tag_info.next;
129129
let result = hook_call(parser, &tag);
@@ -134,7 +134,7 @@ where
134134
next_tag_info = next;
135135
}
136136

137-
let info = parser.definitions_db.expect_get_variable(&id);
137+
let info = parser.definitions_db.expect_get_variable(id);
138138
if let Some(FreeName::String(free_name)) = &info.free_name {
139139
let result = hook_call(parser, &free_name.to_string());
140140
if result.is_some() {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,33 +414,33 @@ impl<'parser> JavascriptParser<'parser> {
414414
}
415415

416416
pub fn get_mut_variable_info(&mut self, name: &str) -> Option<&mut VariableInfo> {
417-
let Some(id) = self.definitions_db.get(&self.definitions, name) else {
417+
let Some(id) = self.definitions_db.get(self.definitions, name) else {
418418
return None;
419419
};
420-
Some(self.definitions_db.expect_get_mut_variable(&id))
420+
Some(self.definitions_db.expect_get_mut_variable(id))
421421
}
422422

423423
pub fn get_variable_info(&mut self, name: &str) -> Option<&VariableInfo> {
424-
let Some(id) = self.definitions_db.get(&self.definitions, name) else {
424+
let Some(id) = self.definitions_db.get(self.definitions, name) else {
425425
return None;
426426
};
427-
Some(self.definitions_db.expect_get_variable(&id))
427+
Some(self.definitions_db.expect_get_variable(id))
428428
}
429429

430430
pub fn get_tag_data(&mut self, name: &Atom, tag: &str) -> Option<Box<dyn anymap::CloneAny>> {
431431
self
432432
.get_variable_info(name)
433433
.and_then(|variable_info| variable_info.tag_info)
434434
.and_then(|tag_info_id| {
435-
let mut tag_info = Some(self.definitions_db.expect_get_tag_info(&tag_info_id));
435+
let mut tag_info = Some(self.definitions_db.expect_get_tag_info(tag_info_id));
436436

437437
while let Some(cur_tag_info) = tag_info {
438438
if cur_tag_info.tag == tag {
439439
return cur_tag_info.data.clone();
440440
}
441441
tag_info = cur_tag_info
442442
.next
443-
.map(|tag_info_id| self.definitions_db.expect_get_tag_info(&tag_info_id))
443+
.map(|tag_info_id| self.definitions_db.expect_get_tag_info(tag_info_id))
444444
}
445445

446446
None
@@ -463,7 +463,7 @@ impl<'parser> JavascriptParser<'parser> {
463463
pub fn get_all_variables_from_current_scope(
464464
&self,
465465
) -> impl Iterator<Item = (&str, &VariableInfoId)> {
466-
let scope = self.definitions_db.expect_get_scope(&self.definitions);
466+
let scope = self.definitions_db.expect_get_scope(self.definitions);
467467
scope.variables()
468468
}
469469

@@ -505,8 +505,8 @@ impl<'parser> JavascriptParser<'parser> {
505505
data: Option<Data>,
506506
) {
507507
let data = data.map(|data| TagInfoData::into_any(data));
508-
let new_info = if let Some(old_info_id) = self.definitions_db.get(&self.definitions, &name) {
509-
let old_info = self.definitions_db.expect_get_variable(&old_info_id);
508+
let new_info = if let Some(old_info_id) = self.definitions_db.get(self.definitions, &name) {
509+
let old_info = self.definitions_db.expect_get_variable(old_info_id);
510510
if let Some(old_tag_info) = old_info.tag_info {
511511
let declared_scope = old_info.declared_scope;
512512
// FIXME: remove `.clone`
@@ -879,7 +879,7 @@ impl<'parser> JavascriptParser<'parser> {
879879
}
880880

881881
fn set_strict(&mut self, value: bool) {
882-
let current_scope = self.definitions_db.expect_get_mut_scope(&self.definitions);
882+
let current_scope = self.definitions_db.expect_get_mut_scope(self.definitions);
883883
current_scope.is_strict = value;
884884
}
885885

@@ -898,13 +898,13 @@ impl<'parser> JavascriptParser<'parser> {
898898
}
899899

900900
pub fn is_strict(&mut self) -> bool {
901-
let scope = self.definitions_db.expect_get_scope(&self.definitions);
901+
let scope = self.definitions_db.expect_get_scope(self.definitions);
902902
scope.is_strict
903903
}
904904

905905
// TODO: remove
906906
pub fn is_unresolved_ident(&mut self, str: &str) -> bool {
907-
self.definitions_db.get(&self.definitions, str).is_none()
907+
self.definitions_db.get(self.definitions, str).is_none()
908908
}
909909
}
910910

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'parser> JavascriptParser<'parser> {
4040
let old_in_tagged_template_tag = self.in_tagged_template_tag;
4141

4242
self.in_tagged_template_tag = false;
43-
self.definitions = self.definitions_db.create_child(&old_definitions);
43+
self.definitions = self.definitions_db.create_child(old_definitions);
4444
f(self);
4545

4646
self.definitions = old_definitions;
@@ -60,7 +60,7 @@ impl<'parser> JavascriptParser<'parser> {
6060

6161
self.in_try = false;
6262
self.in_tagged_template_tag = false;
63-
self.definitions = self.definitions_db.create_child(&old_definitions);
63+
self.definitions = self.definitions_db.create_child(old_definitions);
6464

6565
if has_this {
6666
self.undefined_variable("this".to_string());
@@ -87,7 +87,7 @@ impl<'parser> JavascriptParser<'parser> {
8787
let old_top_level_scope = self.top_level_scope;
8888
let old_in_tagged_template_tag = self.in_tagged_template_tag;
8989

90-
self.definitions = self.definitions_db.create_child(&old_definitions);
90+
self.definitions = self.definitions_db.create_child(old_definitions);
9191
self.in_tagged_template_tag = false;
9292
if has_this {
9393
self.undefined_variable("this".to_string());

crates/rspack_plugin_javascript/src/visitors/scope_info.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl ScopeInfoDB {
103103
}
104104
}
105105

106-
fn _create(&mut self, parent: Option<&ScopeInfoId>) -> ScopeInfoId {
106+
fn _create(&mut self, parent: Option<ScopeInfoId>) -> ScopeInfoId {
107107
let id = self.next();
108108
let stack = match parent {
109109
Some(parent) => {
@@ -131,57 +131,57 @@ impl ScopeInfoDB {
131131
self._create(None)
132132
}
133133

134-
pub fn create_child(&mut self, parent: &ScopeInfoId) -> ScopeInfoId {
134+
pub fn create_child(&mut self, parent: ScopeInfoId) -> ScopeInfoId {
135135
self._create(Some(parent))
136136
}
137137

138-
pub fn expect_get_scope(&self, id: &ScopeInfoId) -> &ScopeInfo {
138+
pub fn expect_get_scope(&self, id: ScopeInfoId) -> &ScopeInfo {
139139
self
140140
.map
141-
.get(id)
141+
.get(&id)
142142
.unwrap_or_else(|| panic!("{id:#?} should exist"))
143143
}
144144

145-
pub fn expect_get_mut_scope(&mut self, id: &ScopeInfoId) -> &mut ScopeInfo {
145+
pub fn expect_get_mut_scope(&mut self, id: ScopeInfoId) -> &mut ScopeInfo {
146146
self
147147
.map
148-
.get_mut(id)
148+
.get_mut(&id)
149149
.unwrap_or_else(|| panic!("{id:#?} should exist"))
150150
}
151151

152-
pub fn expect_get_variable(&self, id: &VariableInfoId) -> &VariableInfo {
152+
pub fn expect_get_variable(&self, id: VariableInfoId) -> &VariableInfo {
153153
self
154154
.variable_info_db
155155
.map
156-
.get(id)
156+
.get(&id)
157157
.unwrap_or_else(|| panic!("{id:#?} should exist"))
158158
}
159159

160-
pub fn expect_get_mut_variable(&mut self, id: &VariableInfoId) -> &mut VariableInfo {
160+
pub fn expect_get_mut_variable(&mut self, id: VariableInfoId) -> &mut VariableInfo {
161161
self
162162
.variable_info_db
163163
.map
164-
.get_mut(id)
164+
.get_mut(&id)
165165
.unwrap_or_else(|| panic!("{id:#?} should exist"))
166166
}
167167

168-
pub fn expect_get_tag_info(&self, id: &TagInfoId) -> &TagInfo {
168+
pub fn expect_get_tag_info(&self, id: TagInfoId) -> &TagInfo {
169169
self
170170
.tag_info_db
171171
.map
172-
.get(id)
172+
.get(&id)
173173
.unwrap_or_else(|| panic!("{id:#?} should exist"))
174174
}
175175

176-
pub fn expect_get_mut_tag_info(&mut self, id: &TagInfoId) -> &mut TagInfo {
176+
pub fn expect_get_mut_tag_info(&mut self, id: TagInfoId) -> &mut TagInfo {
177177
self
178178
.tag_info_db
179179
.map
180-
.get_mut(id)
180+
.get_mut(&id)
181181
.unwrap_or_else(|| panic!("{id:#?} should exist"))
182182
}
183183

184-
pub fn get<S: AsRef<str>>(&mut self, id: &ScopeInfoId, key: S) -> Option<VariableInfoId> {
184+
pub fn get<S: AsRef<str>>(&mut self, id: ScopeInfoId, key: S) -> Option<VariableInfoId> {
185185
let definitions = self.expect_get_scope(id);
186186
if let Some(&top_value) = definitions.map.get(key.as_ref()) {
187187
if top_value == VariableInfo::TOMBSTONE || top_value == VariableInfo::UNDEFINED {
@@ -193,7 +193,7 @@ impl ScopeInfoDB {
193193
for index in (0..definitions.stack.len() - 1).rev() {
194194
// SAFETY: boundary had been checked
195195
let id = unsafe { definitions.stack.get_unchecked(index) };
196-
if let Some(&value) = self.expect_get_scope(id).map.get(key.as_ref()) {
196+
if let Some(&value) = self.expect_get_scope(*id).map.get(key.as_ref()) {
197197
if value == VariableInfo::TOMBSTONE || value == VariableInfo::UNDEFINED {
198198
return None;
199199
} else {
@@ -212,12 +212,12 @@ impl ScopeInfoDB {
212212
}
213213

214214
pub fn set(&mut self, id: ScopeInfoId, key: String, variable_info_id: VariableInfoId) {
215-
let scope = self.expect_get_mut_scope(&id);
215+
let scope = self.expect_get_mut_scope(id);
216216
scope.map.insert(key, variable_info_id);
217217
}
218218

219219
pub fn delete<S: AsRef<str>>(&mut self, id: ScopeInfoId, key: S) {
220-
let scope = self.expect_get_mut_scope(&id);
220+
let scope = self.expect_get_mut_scope(id);
221221
if scope.stack.len() > 1 {
222222
scope
223223
.map

0 commit comments

Comments
 (0)