Skip to content

Commit 9e927f2

Browse files
authored
perf: revert runtime key to string (#11522)
1 parent 09a81db commit 9e927f2

File tree

6 files changed

+73
-61
lines changed

6 files changed

+73
-61
lines changed

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,11 @@ impl Compilation {
603603
let user_request = to_identifier(module_dep.user_request());
604604
let mut runtime_map = self.import_var_map.entry(*parent_module_id).or_default();
605605
let import_var_map_of_module = runtime_map
606-
.entry(runtime.map(get_runtime_key).unwrap_or_default())
606+
.entry(
607+
runtime
608+
.map(|r| get_runtime_key(r).to_string())
609+
.unwrap_or_default(),
610+
)
607611
.or_default();
608612
let len = import_var_map_of_module.len();
609613

@@ -1751,7 +1755,7 @@ impl Compilation {
17511755
let module_runtime_keys = module_runtimes
17521756
.values()
17531757
.map(get_runtime_key)
1754-
.collect::<Vec<_>>();
1758+
.collect::<HashSet<_>>();
17551759

17561760
if let Some(runtime_map) = self.cgm_hash_artifact.get_runtime_map(mi) {
17571761
if module_runtimes.is_empty() {

crates/rspack_core/src/concatenated_module.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,42 +1735,46 @@ impl ConcatenatedModule {
17351735
mg: &ModuleGraph,
17361736
mg_cache: &ModuleGraphCacheArtifact,
17371737
) -> Vec<ConcatenationEntry> {
1738-
mg_cache.cached_concatenated_module_entries((self.id, runtime.map(get_runtime_key)), || {
1739-
let root_module = self.root_module_ctxt.id;
1740-
let module_set: IdentifierIndexSet = self.modules.iter().map(|item| item.id).collect();
1741-
1742-
let mut list = vec![];
1743-
let mut exists_entries = IdentifierMap::default();
1744-
exists_entries.insert(root_module, RuntimeCondition::Boolean(true));
1745-
1746-
let imports_map = module_set
1747-
.par_iter()
1748-
.map(|module| {
1749-
let imports = self.get_concatenated_imports(module, &root_module, runtime, mg, mg_cache);
1750-
(*module, imports)
1751-
})
1752-
.collect::<IdentifierMap<_>>();
1738+
mg_cache.cached_concatenated_module_entries(
1739+
(self.id, runtime.map(|r| get_runtime_key(r).to_string())),
1740+
|| {
1741+
let root_module = self.root_module_ctxt.id;
1742+
let module_set: IdentifierIndexSet = self.modules.iter().map(|item| item.id).collect();
1743+
1744+
let mut list = vec![];
1745+
let mut exists_entries = IdentifierMap::default();
1746+
exists_entries.insert(root_module, RuntimeCondition::Boolean(true));
1747+
1748+
let imports_map = module_set
1749+
.par_iter()
1750+
.map(|module| {
1751+
let imports =
1752+
self.get_concatenated_imports(module, &root_module, runtime, mg, mg_cache);
1753+
(*module, imports)
1754+
})
1755+
.collect::<IdentifierMap<_>>();
17531756

1754-
let imports = imports_map.get(&root_module).expect("should have imports");
1755-
for i in imports {
1756-
self.enter_module(
1757-
&module_set,
1758-
runtime,
1759-
mg,
1760-
&i.connection,
1761-
i.runtime_condition.clone(),
1762-
&mut exists_entries,
1763-
&mut list,
1764-
&imports_map,
1765-
);
1766-
}
1767-
list.push(ConcatenationEntry::Concatenated(
1768-
ConcatenationEntryConcatenated {
1769-
module: root_module,
1770-
},
1771-
));
1772-
list
1773-
})
1757+
let imports = imports_map.get(&root_module).expect("should have imports");
1758+
for i in imports {
1759+
self.enter_module(
1760+
&module_set,
1761+
runtime,
1762+
mg,
1763+
&i.connection,
1764+
i.runtime_condition.clone(),
1765+
&mut exists_entries,
1766+
&mut list,
1767+
&imports_map,
1768+
);
1769+
}
1770+
list.push(ConcatenationEntry::Concatenated(
1771+
ConcatenationEntryConcatenated {
1772+
module: root_module,
1773+
},
1774+
));
1775+
list
1776+
},
1777+
)
17741778
}
17751779

17761780
#[allow(clippy::too_many_arguments)]

crates/rspack_core/src/runtime.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rspack_cacheable::{
44
cacheable,
55
with::{AsRefStr, AsVec},
66
};
7-
use ustr::{Ustr, UstrMap, UstrSet};
7+
use rustc_hash::FxHashMap;
8+
use ustr::{Ustr, UstrSet};
89

910
use crate::{EntryOptions, EntryRuntime};
1011

@@ -13,8 +14,7 @@ use crate::{EntryOptions, EntryRuntime};
1314
pub struct RuntimeSpec {
1415
#[cacheable(with=AsVec<AsRefStr>)]
1516
inner: UstrSet,
16-
#[cacheable(with=AsRefStr)]
17-
key: Ustr,
17+
key: String,
1818
}
1919

2020
impl std::fmt::Display for RuntimeSpec {
@@ -78,7 +78,7 @@ impl RuntimeSpec {
7878
pub fn new(inner: UstrSet) -> Self {
7979
let mut this = Self {
8080
inner,
81-
key: Ustr::from(""),
81+
key: String::new(),
8282
};
8383
this.update_key();
8484
this
@@ -127,23 +127,22 @@ impl RuntimeSpec {
127127
if self.key.is_empty() {
128128
return;
129129
}
130-
self.key = Ustr::from("");
130+
self.key = String::new();
131131
return;
132132
}
133133
let mut ordered = self.inner.iter().map(|s| s.as_str()).collect::<Vec<_>>();
134134
ordered.sort_unstable();
135-
let key = ordered.join("_");
136-
self.key = Ustr::from(&key);
135+
self.key = ordered.join("_");
137136
}
138137

139138
pub fn as_str(&self) -> &str {
140139
&self.key
141140
}
142141
}
143142

144-
pub type RuntimeKey = Ustr;
143+
pub type RuntimeKey = String;
145144

146-
pub type RuntimeKeyMap<T> = UstrMap<T>;
145+
pub type RuntimeKeyMap<T> = FxHashMap<RuntimeKey, T>;
147146

148147
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
149148
pub enum RuntimeMode {
@@ -301,8 +300,8 @@ pub fn subtract_runtime_condition(
301300
RuntimeCondition::Spec(merged.into())
302301
}
303302

304-
pub fn get_runtime_key(runtime: &RuntimeSpec) -> RuntimeKey {
305-
runtime.key
303+
pub fn get_runtime_key(runtime: &RuntimeSpec) -> &RuntimeKey {
304+
&runtime.key
306305
}
307306

308307
pub fn compare_runtime(a: &RuntimeSpec, b: &RuntimeSpec) -> Ordering {
@@ -346,7 +345,7 @@ impl<T> RuntimeSpecMap<T> {
346345
None
347346
}
348347
}
349-
RuntimeMode::Map => self.map.get(&get_runtime_key(runtime)),
348+
RuntimeMode::Map => self.map.get(get_runtime_key(runtime)),
350349
}
351350
}
352351

@@ -362,7 +361,7 @@ impl<T> RuntimeSpecMap<T> {
362361
None
363362
}
364363
}
365-
RuntimeMode::Map => self.map.get_mut(&get_runtime_key(runtime)),
364+
RuntimeMode::Map => self.map.get_mut(get_runtime_key(runtime)),
366365
}
367366
}
368367

@@ -392,12 +391,16 @@ impl<T> RuntimeSpecMap<T> {
392391

393392
self
394393
.map
395-
.insert(get_runtime_key(&single_runtime), single_value);
396-
self.map.insert(get_runtime_key(&runtime), value);
394+
.insert(get_runtime_key(&single_runtime).to_string(), single_value);
395+
self
396+
.map
397+
.insert(get_runtime_key(&runtime).to_string(), value);
397398
}
398399
}
399400
RuntimeMode::Map => {
400-
self.map.insert(get_runtime_key(&runtime), value);
401+
self
402+
.map
403+
.insert(get_runtime_key(&runtime).to_string(), value);
401404
}
402405
}
403406
}
@@ -444,15 +447,17 @@ pub struct RuntimeSpecSet {
444447

445448
impl RuntimeSpecSet {
446449
pub fn get(&self, runtime: &RuntimeSpec) -> Option<&RuntimeSpec> {
447-
self.map.get(&get_runtime_key(runtime))
450+
self.map.get(get_runtime_key(runtime))
448451
}
449452

450453
pub fn set(&mut self, runtime: RuntimeSpec) {
451-
self.map.insert(get_runtime_key(&runtime), runtime);
454+
self
455+
.map
456+
.insert(get_runtime_key(&runtime).to_string(), runtime);
452457
}
453458

454459
pub fn contains(&self, runtime: &RuntimeSpec) -> bool {
455-
self.map.contains_key(&get_runtime_key(runtime))
460+
self.map.contains_key(get_runtime_key(runtime))
456461
}
457462

458463
pub fn values(&self) -> hash_map::Values<'_, RuntimeKey, RuntimeSpec> {

crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl ESMExportImportedSpecifierDependency {
138138
runtime: Option<&RuntimeSpec>,
139139
module_graph_cache: &ModuleGraphCacheArtifact,
140140
) -> ExportMode {
141-
let key = (self.id, runtime.map(get_runtime_key));
141+
let key = (self.id, runtime.map(|r| get_runtime_key(r).to_string()));
142142
module_graph_cache.cached_get_mode(key, || {
143143
self.get_mode_inner(module_graph, module_graph_cache, runtime)
144144
})

crates/rspack_plugin_split_chunks/src/plugin/module_group.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Combinator {
9797
let chunk = chunk_by_ukey.expect_get(&chunk_ukey);
9898
let runtime = chunk.runtime();
9999
let usage_key = runtime_key_map
100-
.entry(get_runtime_key(runtime))
100+
.entry(get_runtime_key(runtime).to_string())
101101
.or_insert_with(|| exports_info.get_usage_key(Some(runtime)))
102102
.clone();
103103

tests/e2e/cases/lazy-compilation/default-prefix/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ test("should use default prefix for lazy compilation", async ({ page }) => {
77
console.log(`server url: ${response.url()}`)
88
return response.url().includes("/lazy-compilation-using-") &&
99
response.request().method() === "GET"
10-
},
11-
{ timeout: 5000 }
10+
}
1211
);
1312

1413
// Click the button that triggers dynamic import
@@ -19,7 +18,7 @@ test("should use default prefix for lazy compilation", async ({ page }) => {
1918
expect(response.status()).toBe(200);
2019

2120
// Wait for the component to appear with a more reliable wait
22-
await page.waitForSelector('div:has-text("Component")', { timeout: 5000 });
21+
await page.waitForSelector('div:has-text("Component")');
2322

2423
// Check that the component was loaded and displayed
2524
const component_count = await page.getByText("Component").count();

0 commit comments

Comments
 (0)