Skip to content

Commit 904d026

Browse files
authored
fix: unstable render dependency template (#11473)
1 parent df5f3d3 commit 904d026

File tree

10 files changed

+92
-96
lines changed

10 files changed

+92
-96
lines changed

crates/rspack_core/src/artifacts/module_graph_cache_artifact.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub(super) mod concatenated_module_entries {
194194
use super::*;
195195
use crate::ModuleIdentifier;
196196

197-
pub type ConcatenatedModuleEntriesCacheKey = (ModuleIdentifier, String);
197+
pub type ConcatenatedModuleEntriesCacheKey = (ModuleIdentifier, Option<RuntimeKey>);
198198

199199
#[derive(Debug, Default)]
200200
pub struct ConcatenatedModuleEntriesCache {

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ use crate::{
4646
EntryOptions, EntryRuntime, Entrypoint, Filename, ImportVarMap, Logger, MemoryGCStorage,
4747
ModuleFactory, ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphPartial, ModuleIdentifier,
4848
ModuleIdsArtifact, ModuleStaticCacheArtifact, PathData, ResolverFactory, RuntimeGlobals,
49-
RuntimeMode, RuntimeModule, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
50-
SideEffectsOptimizeArtifact, SourceType, Stats, ValueCacheVersions,
49+
RuntimeKeyMap, RuntimeMode, RuntimeModule, RuntimeSpec, RuntimeSpecMap, RuntimeTemplate,
50+
SharedPluginDriver, SideEffectsOptimizeArtifact, SourceType, Stats, ValueCacheVersions,
5151
build_chunk_graph::{build_chunk_graph, build_chunk_graph_new},
5252
compilation::make::{
5353
ExecuteModuleId, MakeArtifact, ModuleExecutor, UpdateParam, finish_make, make,
@@ -280,7 +280,7 @@ pub struct Compilation {
280280

281281
pub value_cache_versions: ValueCacheVersions,
282282

283-
import_var_map: IdentifierDashMap<ImportVarMap>,
283+
import_var_map: IdentifierDashMap<RuntimeKeyMap<ImportVarMap>>,
284284

285285
// TODO move to MakeArtifact
286286
pub module_executor: Option<ModuleExecutor>,
@@ -588,7 +588,7 @@ impl Compilation {
588588
}
589589

590590
// TODO move out from compilation
591-
pub fn get_import_var(&self, dep_id: &DependencyId) -> String {
591+
pub fn get_import_var(&self, dep_id: &DependencyId, runtime: Option<&RuntimeSpec>) -> String {
592592
let module_graph = self.get_module_graph();
593593
let parent_module_id = module_graph
594594
.get_parent_module(dep_id)
@@ -601,7 +601,10 @@ impl Compilation {
601601
.and_then(|dep| dep.as_module_dependency())
602602
.expect("should be module dependency");
603603
let user_request = to_identifier(module_dep.user_request());
604-
let mut import_var_map_of_module = self.import_var_map.entry(*parent_module_id).or_default();
604+
let mut runtime_map = self.import_var_map.entry(*parent_module_id).or_default();
605+
let import_var_map_of_module = runtime_map
606+
.entry(runtime.map(get_runtime_key).unwrap_or_default())
607+
.or_default();
605608
let len = import_var_map_of_module.len();
606609

607610
match import_var_map_of_module.entry(module_id) {
@@ -1782,7 +1785,7 @@ impl Compilation {
17821785
}
17831786

17841787
for runtime_key in runtime_map.map.keys() {
1785-
if !module_runtime_keys.contains(&runtime_key.as_str()) {
1788+
if !module_runtime_keys.contains(runtime_key) {
17861789
modules.insert(*mi);
17871790
break;
17881791
}

crates/rspack_core/src/concatenated_module.rs

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

1762-
let imports = imports_map.get(&root_module).expect("should have imports");
1763-
for i in imports {
1764-
self.enter_module(
1765-
&module_set,
1766-
runtime,
1767-
mg,
1768-
&i.connection,
1769-
i.runtime_condition.clone(),
1770-
&mut exists_entries,
1771-
&mut list,
1772-
&imports_map,
1773-
);
1774-
}
1775-
list.push(ConcatenationEntry::Concatenated(
1776-
ConcatenationEntryConcatenated {
1777-
module: root_module,
1778-
},
1779-
));
1780-
list
1781-
},
1782-
)
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+
})
17831774
}
17841775

17851776
#[allow(clippy::too_many_arguments)]

crates/rspack_core/src/dependency/runtime_template.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ pub fn module_id(
378378

379379
pub fn import_statement(
380380
module: &dyn Module,
381+
runtime: Option<&RuntimeSpec>,
381382
compilation: &Compilation,
382383
runtime_requirements: &mut RuntimeGlobals,
383384
id: &DependencyId,
@@ -396,7 +397,7 @@ pub fn import_statement(
396397

397398
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
398399

399-
let import_var = compilation.get_import_var(id);
400+
let import_var = compilation.get_import_var(id, runtime);
400401

401402
let opt_declaration = if update { "" } else { "var " };
402403

crates/rspack_core/src/runtime.rs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use rspack_cacheable::{
44
cacheable,
55
with::{AsRefStr, AsVec},
66
};
7-
use rustc_hash::FxHashMap as HashMap;
7+
use ustr::{Ustr, UstrMap, UstrSet};
88

99
use crate::{EntryOptions, EntryRuntime};
1010

1111
#[cacheable]
1212
#[derive(Debug, Default, Clone)]
1313
pub struct RuntimeSpec {
1414
#[cacheable(with=AsVec<AsRefStr>)]
15-
inner: ustr::UstrSet,
16-
key: String,
15+
inner: UstrSet,
16+
#[cacheable(with=AsRefStr)]
17+
key: Ustr,
1718
}
1819

1920
impl std::fmt::Display for RuntimeSpec {
@@ -45,39 +46,39 @@ impl std::cmp::PartialEq for RuntimeSpec {
4546
impl std::cmp::Eq for RuntimeSpec {}
4647

4748
impl Deref for RuntimeSpec {
48-
type Target = ustr::UstrSet;
49+
type Target = UstrSet;
4950

5051
fn deref(&self) -> &Self::Target {
5152
&self.inner
5253
}
5354
}
5455

55-
impl From<ustr::UstrSet> for RuntimeSpec {
56-
fn from(value: ustr::UstrSet) -> Self {
56+
impl From<UstrSet> for RuntimeSpec {
57+
fn from(value: UstrSet) -> Self {
5758
Self::new(value)
5859
}
5960
}
6061

61-
impl FromIterator<ustr::Ustr> for RuntimeSpec {
62-
fn from_iter<T: IntoIterator<Item = ustr::Ustr>>(iter: T) -> Self {
63-
Self::new(ustr::UstrSet::from_iter(iter))
62+
impl FromIterator<Ustr> for RuntimeSpec {
63+
fn from_iter<T: IntoIterator<Item = Ustr>>(iter: T) -> Self {
64+
Self::new(UstrSet::from_iter(iter))
6465
}
6566
}
6667

6768
impl IntoIterator for RuntimeSpec {
68-
type Item = ustr::Ustr;
69-
type IntoIter = <ustr::UstrSet as IntoIterator>::IntoIter;
69+
type Item = Ustr;
70+
type IntoIter = <UstrSet as IntoIterator>::IntoIter;
7071

7172
fn into_iter(self) -> Self::IntoIter {
7273
self.inner.into_iter()
7374
}
7475
}
7576

7677
impl RuntimeSpec {
77-
pub fn new(inner: ustr::UstrSet) -> Self {
78+
pub fn new(inner: UstrSet) -> Self {
7879
let mut this = Self {
7980
inner,
80-
key: String::new(),
81+
key: Ustr::from(""),
8182
};
8283
this.update_key();
8384
this
@@ -105,7 +106,7 @@ impl RuntimeSpec {
105106
Self::new(res)
106107
}
107108

108-
pub fn insert(&mut self, r: ustr::Ustr) -> bool {
109+
pub fn insert(&mut self, r: Ustr) -> bool {
109110
let update = self.inner.insert(r);
110111
if update {
111112
self.update_key();
@@ -126,20 +127,23 @@ impl RuntimeSpec {
126127
if self.key.is_empty() {
127128
return;
128129
}
129-
self.key = String::new();
130+
self.key = Ustr::from("");
130131
return;
131132
}
132133
let mut ordered = self.inner.iter().map(|s| s.as_str()).collect::<Vec<_>>();
133134
ordered.sort_unstable();
134-
self.key = ordered.join("_");
135+
let key = ordered.join("_");
136+
self.key = Ustr::from(&key);
135137
}
136138

137139
pub fn as_str(&self) -> &str {
138140
&self.key
139141
}
140142
}
141143

142-
pub type RuntimeKey = String;
144+
pub type RuntimeKey = Ustr;
145+
146+
pub type RuntimeKeyMap<T> = UstrMap<T>;
143147

144148
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
145149
pub enum RuntimeMode {
@@ -209,7 +213,7 @@ pub fn filter_runtime(
209213
Some(runtime) => {
210214
let mut some = false;
211215
let mut every = true;
212-
let mut result = ustr::UstrSet::default();
216+
let mut result = UstrSet::default();
213217

214218
for &r in runtime.iter() {
215219
let cur = RuntimeSpec::from_iter([r]);
@@ -287,7 +291,7 @@ pub fn subtract_runtime_condition(
287291
if let Some(a) = runtime {
288292
a.difference(b).copied().collect()
289293
} else {
290-
ustr::UstrSet::default()
294+
UstrSet::default()
291295
}
292296
}
293297
};
@@ -297,8 +301,8 @@ pub fn subtract_runtime_condition(
297301
RuntimeCondition::Spec(merged.into())
298302
}
299303

300-
pub fn get_runtime_key(runtime: &RuntimeSpec) -> &str {
301-
&runtime.key
304+
pub fn get_runtime_key(runtime: &RuntimeSpec) -> RuntimeKey {
305+
runtime.key
302306
}
303307

304308
pub fn compare_runtime(a: &RuntimeSpec, b: &RuntimeSpec) -> Ordering {
@@ -308,7 +312,7 @@ pub fn compare_runtime(a: &RuntimeSpec, b: &RuntimeSpec) -> Ordering {
308312
#[derive(Default, Clone, Debug, PartialEq, Eq)]
309313
pub struct RuntimeSpecMap<T> {
310314
pub mode: RuntimeMode,
311-
pub map: HashMap<RuntimeKey, T>,
315+
pub map: RuntimeKeyMap<T>,
312316

313317
pub single_runtime: Option<RuntimeSpec>,
314318
pub single_value: Option<T>,
@@ -342,7 +346,7 @@ impl<T> RuntimeSpecMap<T> {
342346
None
343347
}
344348
}
345-
RuntimeMode::Map => self.map.get(get_runtime_key(runtime)),
349+
RuntimeMode::Map => self.map.get(&get_runtime_key(runtime)),
346350
}
347351
}
348352

@@ -358,7 +362,7 @@ impl<T> RuntimeSpecMap<T> {
358362
None
359363
}
360364
}
361-
RuntimeMode::Map => self.map.get_mut(get_runtime_key(runtime)),
365+
RuntimeMode::Map => self.map.get_mut(&get_runtime_key(runtime)),
362366
}
363367
}
364368

@@ -388,16 +392,12 @@ impl<T> RuntimeSpecMap<T> {
388392

389393
self
390394
.map
391-
.insert(get_runtime_key(&single_runtime).to_string(), single_value);
392-
self
393-
.map
394-
.insert(get_runtime_key(&runtime).to_string(), value);
395+
.insert(get_runtime_key(&single_runtime), single_value);
396+
self.map.insert(get_runtime_key(&runtime), value);
395397
}
396398
}
397399
RuntimeMode::Map => {
398-
self
399-
.map
400-
.insert(get_runtime_key(&runtime).to_string(), value);
400+
self.map.insert(get_runtime_key(&runtime), value);
401401
}
402402
}
403403
}
@@ -439,22 +439,20 @@ impl<'a, T> Iterator for RuntimeSpecMapValues<'a, T> {
439439

440440
#[derive(Default, Debug)]
441441
pub struct RuntimeSpecSet {
442-
map: HashMap<RuntimeKey, RuntimeSpec>,
442+
map: RuntimeKeyMap<RuntimeSpec>,
443443
}
444444

445445
impl RuntimeSpecSet {
446446
pub fn get(&self, runtime: &RuntimeSpec) -> Option<&RuntimeSpec> {
447-
self.map.get(get_runtime_key(runtime))
447+
self.map.get(&get_runtime_key(runtime))
448448
}
449449

450450
pub fn set(&mut self, runtime: RuntimeSpec) {
451-
self
452-
.map
453-
.insert(get_runtime_key(&runtime).to_string(), runtime);
451+
self.map.insert(get_runtime_key(&runtime), runtime);
454452
}
455453

456454
pub fn contains(&self, runtime: &RuntimeSpec) -> bool {
457-
self.map.contains_key(get_runtime_key(runtime))
455+
self.map.contains_key(&get_runtime_key(runtime))
458456
}
459457

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

0 commit comments

Comments
 (0)