Skip to content

Commit 6814a13

Browse files
committed
remove lock on outgoings module
1 parent bb1ef50 commit 6814a13

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

crates/rspack_core/src/build_chunk_graph/code_splitter.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
use indexmap::{IndexMap as RawIndexMap, IndexSet as RawIndexSet};
88
use itertools::Itertools;
99
use num_bigint::BigUint;
10+
use rayon::prelude::*;
1011
use rspack_collections::{
1112
impl_item_ukey, Database, DatabaseItem, IdentifierHasher, IdentifierIndexSet, IdentifierMap,
1213
Ukey, UkeyIndexMap, UkeyIndexSet, UkeyMap, UkeySet,
@@ -674,15 +675,27 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
674675
let mut input_entrypoints_and_modules: UkeyIndexMap<ChunkGroupUkey, Vec<ModuleIdentifier>> =
675676
UkeyIndexMap::default();
676677
let mut assign_depths_map = IdentifierMap::default();
678+
let outgoings = {
679+
let mg = compilation.get_module_graph();
680+
let all_modules = mg.modules();
681+
all_modules
682+
.keys()
683+
.par_bridge()
684+
.map(|mid| {
685+
(
686+
*mid,
687+
mg.get_outgoing_connections(mid)
688+
.cloned()
689+
.collect::<Vec<_>>(),
690+
)
691+
})
692+
.collect::<IdentifierMap<_>>()
693+
};
677694

678695
let entries = compilation.entries.keys().cloned().collect::<Vec<_>>();
679696
for name in &entries {
680697
let (entry_point, modules) = self.prepare_entry_input(name, compilation)?;
681-
assign_depths(
682-
&mut assign_depths_map,
683-
&compilation.get_module_graph(),
684-
modules.iter(),
685-
);
698+
assign_depths(&mut assign_depths_map, &modules, &outgoings);
686699
input_entrypoints_and_modules.insert(entry_point, modules);
687700
}
688701

crates/rspack_core/src/build_chunk_graph/new_code_splitter.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -754,12 +754,9 @@ impl CodeSplitter {
754754
&self,
755755
module: &ModuleIdentifier,
756756
runtime: &RuntimeSpec,
757-
) -> (Vec<ModuleIdentifier>, Vec<AsyncDependenciesBlockIdentifier>) {
757+
) -> Option<(Vec<ModuleIdentifier>, Vec<AsyncDependenciesBlockIdentifier>)> {
758758
let module_map = self.module_deps.get(runtime).expect("should have value");
759-
module_map
760-
.get(module)
761-
.expect("should have outgoing modules")
762-
.clone()
759+
module_map.get(module).cloned()
763760
}
764761

765762
pub fn outgoings_modules(
@@ -864,7 +861,13 @@ impl CodeSplitter {
864861
value
865862
});
866863

867-
let (mut outgoing_modules, blocks) = self.get_outgoings_modules(&target_module, runtime);
864+
let Some((mut outgoing_modules, blocks)) =
865+
self.get_outgoings_modules(&target_module, runtime)
866+
else {
867+
// not exists when error occurs
868+
// just skip it and the errors will be reported in stats
869+
continue;
870+
};
868871

869872
if ctx.chunk_loading {
870873
ctx.out_goings.extend(blocks);
@@ -987,7 +990,11 @@ impl CodeSplitter {
987990
if !self.module_deps.contains_key(runtime) {
988991
self.module_deps.insert(runtime.clone(), Default::default());
989992
}
990-
let (modules, blocks) = self.get_outgoings_modules(&m, runtime);
993+
let Some((modules, blocks)) = self.get_outgoings_modules(&m, runtime) else {
994+
// not exists when error occurs
995+
// just skip it and the errors will be reported in stats
996+
continue;
997+
};
991998

992999
for m in modules.iter().rev() {
9931000
queue.push(Task::Enter((*m, runtime)));
@@ -1154,6 +1161,22 @@ impl CodeSplitter {
11541161
let mut async_entrypoints = HashSet::default();
11551162
let mut entrypoints = HashMap::default();
11561163
let mut skipped = HashSet::<usize>::default();
1164+
let outgoings = {
1165+
let mg = compilation.get_module_graph();
1166+
let all_modules = mg.modules();
1167+
all_modules
1168+
.keys()
1169+
.par_bridge()
1170+
.map(|mid| {
1171+
(
1172+
*mid,
1173+
mg.get_outgoing_connections(mid)
1174+
.cloned()
1175+
.collect::<Vec<_>>(),
1176+
)
1177+
})
1178+
.collect::<IdentifierMap<_>>()
1179+
};
11571180

11581181
for (idx, (reuse, cache)) in finalize_result.chunks.into_iter().enumerate() {
11591182
let chunk_desc = cache.chunk_desc;
@@ -1335,11 +1358,7 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
13351358

13361359
if initial {
13371360
let mut assign_depths_map = IdentifierMap::default();
1338-
assign_depths(
1339-
&mut assign_depths_map,
1340-
&compilation.get_module_graph(),
1341-
entry_modules.iter(),
1342-
);
1361+
assign_depths(&mut assign_depths_map, &entry_modules, &outgoings);
13431362
let mut module_graph = compilation.get_module_graph_mut();
13441363
for (m, depth) in assign_depths_map {
13451364
module_graph.set_depth_if_lower(&m, depth);

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use rspack_cacheable::{
1717
with::{AsOption, AsPreset},
1818
};
1919
use rspack_collections::{
20-
DatabaseItem, Identifiable, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeyMap, UkeySet,
20+
DatabaseItem, Identifiable, Identifier, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeyMap,
21+
UkeySet,
2122
};
2223
use rspack_error::{
2324
error, miette::diagnostic, Diagnostic, DiagnosticExt, InternalError, Result, RspackSeverity,
@@ -54,9 +55,9 @@ use crate::{
5455
CompilationLogging, CompilerOptions, DependenciesDiagnosticsArtifact, DependencyCodeGeneration,
5556
DependencyId, DependencyTemplate, DependencyTemplateType, DependencyType, Entry, EntryData,
5657
EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, Filename, ImportVarMap, Logger,
57-
ModuleFactory, ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphPartial, ModuleIdentifier,
58-
ModuleIdsArtifact, ModuleStaticCacheArtifact, PathData, ResolverFactory, RuntimeGlobals,
59-
RuntimeMode, RuntimeModule, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
58+
ModuleFactory, ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleGraphPartial,
59+
ModuleIdentifier, ModuleIdsArtifact, ModuleStaticCacheArtifact, PathData, ResolverFactory,
60+
RuntimeGlobals, RuntimeMode, RuntimeModule, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
6061
SideEffectsOptimizeArtifact, SourceType, Stats,
6162
};
6263

@@ -2961,8 +2962,8 @@ impl AssetInfoRelated {
29612962
/// the same time.
29622963
pub fn assign_depths(
29632964
assign_map: &mut IdentifierMap<usize>,
2964-
mg: &ModuleGraph,
2965-
modules: impl Iterator<Item = &ModuleIdentifier>,
2965+
modules: &Vec<Identifier>,
2966+
outgoings: &IdentifierMap<Vec<ModuleGraphConnection>>,
29662967
) {
29672968
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/Compilation.js#L3720
29682969
let mut q = VecDeque::new();
@@ -2978,7 +2979,7 @@ pub fn assign_depths(
29782979
vac.insert(depth);
29792980
}
29802981
};
2981-
for con in mg.get_outgoing_connections(&id) {
2982+
for con in outgoings.get(&id).expect("should have value") {
29822983
q.push_back((*con.module_identifier(), depth + 1));
29832984
}
29842985
}

0 commit comments

Comments
 (0)