Skip to content

Commit d851122

Browse files
authored
perf: improve try_to_add of module concatenation (#11279)
* perf: improve try_to_add * perf: improve try_to_add
1 parent c40ac65 commit d851122

File tree

2 files changed

+132
-61
lines changed

2 files changed

+132
-61
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ use swc_core::{
4040
use swc_node_comments::SwcComments;
4141

4242
use crate::{
43-
AsyncDependenciesBlockIdentifier, BoxDependency, BoxDependencyTemplate, BoxModule,
44-
BoxModuleDependency, BuildContext, BuildInfo, BuildMeta, BuildMetaDefaultObject,
45-
BuildMetaExportsType, BuildResult, ChunkGraph, ChunkInitFragments, ChunkRenderContext,
46-
CodeGenerationDataTopLevelDeclarations, CodeGenerationExportsFinalNames,
47-
CodeGenerationPublicPathAutoReplace, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
48-
ConcatenationScope, ConditionalInitFragment, ConnectionState, Context, DEFAULT_EXPORT,
49-
DependenciesBlock, DependencyId, DependencyType, ErrorSpan, ExportProvided, ExportsArgument,
50-
ExportsInfoGetter, ExportsType, FactoryMeta, GetUsedNameParam, IdentCollector, InitFragment,
51-
InitFragmentStage, LibIdentOptions, MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument,
52-
ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleIdentifier, ModuleLayer,
43+
AsyncDependenciesBlockIdentifier, BoxDependency, BoxDependencyTemplate, BoxModuleDependency,
44+
BuildContext, BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, BuildResult,
45+
ChunkGraph, ChunkInitFragments, ChunkRenderContext, CodeGenerationDataTopLevelDeclarations,
46+
CodeGenerationExportsFinalNames, CodeGenerationPublicPathAutoReplace, CodeGenerationResult,
47+
Compilation, ConcatenatedModuleIdent, ConcatenationScope, ConditionalInitFragment,
48+
ConnectionState, Context, DEFAULT_EXPORT, DependenciesBlock, DependencyId, DependencyType,
49+
ErrorSpan, ExportProvided, ExportsArgument, ExportsInfoGetter, ExportsType, FactoryMeta,
50+
GetUsedNameParam, IdentCollector, InitFragment, InitFragmentStage, LibIdentOptions,
51+
MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph,
52+
ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleIdentifier, ModuleLayer,
5353
ModuleStaticCacheArtifact, ModuleType, NAMESPACE_OBJECT_EXPORT, PrefetchExportsInfoMode, Resolve,
5454
RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, UsageState, UsedName,
5555
UsedNameItem, define_es_module_flag_statement, escape_identifier, filter_runtime,
@@ -737,9 +737,8 @@ impl Module for ConcatenatedModule {
737737
let mut escaped_names: HashMap<String, String> = HashMap::default();
738738
let mut escaped_identifiers: HashMap<String, Vec<String>> = HashMap::default();
739739
let readable_identifier = get_cached_readable_identifier(
740-
module_graph
741-
.module_by_identifier(&info.id())
742-
.expect("should have module identifier"),
740+
&info.id(),
741+
&module_graph,
743742
&compilation.module_static_cache_artifact,
744743
&context,
745744
);
@@ -783,8 +782,12 @@ impl Module for ConcatenatedModule {
783782
let module = module_graph
784783
.module_by_identifier(&info.id())
785784
.expect("should have module identifier");
786-
let readable_identifier =
787-
get_cached_readable_identifier(module, &compilation.module_static_cache_artifact, &context);
785+
let readable_identifier = get_cached_readable_identifier(
786+
&info.id(),
787+
&module_graph,
788+
&compilation.module_static_cache_artifact,
789+
&context,
790+
);
788791
let exports_type: BuildMetaExportsType = module.build_meta().exports_type;
789792
let default_object: BuildMetaDefaultObject = module.build_meta().default_object;
790793
match info {
@@ -1296,7 +1299,8 @@ impl Module for ConcatenatedModule {
12961299
.module_by_identifier(module_info_id)
12971300
.expect("should have box module");
12981301
let module_readable_identifier = get_cached_readable_identifier(
1299-
box_module,
1302+
module_info_id,
1303+
&module_graph,
13001304
&compilation.module_static_cache_artifact,
13011305
&context,
13021306
);
@@ -1398,11 +1402,9 @@ impl Module for ConcatenatedModule {
13981402
let info = module_to_info_map
13991403
.get(&module_info_id)
14001404
.expect("should have module info");
1401-
let box_module = module_graph
1402-
.module_by_identifier(&module_info_id)
1403-
.expect("should have box module");
14041405
let module_readable_identifier = get_cached_readable_identifier(
1405-
box_module,
1406+
&module_info_id,
1407+
&module_graph,
14061408
&compilation.module_static_cache_artifact,
14071409
&context,
14081410
);
@@ -2144,14 +2146,16 @@ impl ConcatenatedModule {
21442146
.get(&binding.info_id)
21452147
.and_then(|info| info.try_as_concatenated())
21462148
.expect("should have concatenate module info");
2147-
let module = module_graph
2148-
.module_by_identifier(&info.module)
2149-
.expect("should have module");
21502149
let name = info.internal_names.get(export_id).unwrap_or_else(|| {
21512150
panic!(
21522151
"The export \"{}\" in \"{}\" has no internal name (existing names: {})",
21532152
export_id,
2154-
get_cached_readable_identifier(module, module_static_cache_artifact, context),
2153+
get_cached_readable_identifier(
2154+
&info.module,
2155+
module_graph,
2156+
module_static_cache_artifact,
2157+
context
2158+
),
21552159
info
21562160
.internal_names
21572161
.iter()
@@ -2738,11 +2742,13 @@ impl FinalNameResult {
27382742
}
27392743

27402744
pub fn get_cached_readable_identifier(
2741-
module: &BoxModule,
2745+
mid: &ModuleIdentifier,
2746+
mg: &ModuleGraph,
27422747
module_static_cache_artifact: &ModuleStaticCacheArtifact,
27432748
compilation_context: &Context,
27442749
) -> String {
2745-
module_static_cache_artifact.cached_readable_identifier((module.identifier(), None), || {
2750+
module_static_cache_artifact.cached_readable_identifier((*mid, None), || {
2751+
let module = mg.module_by_identifier(mid).expect("should have module");
27462752
module.readable_identifier(compilation_context).to_string()
27472753
})
27482754
}

crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs

Lines changed: 100 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ impl ModuleConcatenationPlugin {
194194
if cached.runtime == *runtime {
195195
// runtime is same, use cached value
196196
*cached_active
197-
} else if cached.runtime.is_subset(runtime) && *cached_active {
197+
} else if *cached_active && cached.runtime.is_subset(runtime) {
198198
// cached runtime is subset and active, means it is also active in current runtime
199199
true
200-
} else if cached.runtime.is_superset(runtime) && !*cached_active {
200+
} else if !*cached_active && cached.runtime.is_superset(runtime) {
201201
// cached runtime is superset and inactive, means it is also inactive in current runtime
202202
false
203203
} else {
@@ -267,11 +267,9 @@ impl ModuleConcatenationPlugin {
267267
statistics.cache_hit += 1;
268268
incomings.clone()
269269
} else {
270-
let module = module_graph
271-
.module_by_identifier(module_id)
272-
.expect("should have module");
273270
let module_readable_identifier = get_cached_readable_identifier(
274-
module,
271+
module_id,
272+
&module_graph,
275273
&compilation.module_static_cache_artifact,
276274
&compilation.options.context,
277275
);
@@ -324,14 +322,29 @@ impl ModuleConcatenationPlugin {
324322
return Some(problem);
325323
}
326324

327-
let NoRuntimeModuleCache { incomings, .. } = module_cache
325+
let NoRuntimeModuleCache {
326+
incomings,
327+
active_incomings,
328+
runtime: cached_module_runtime,
329+
..
330+
} = module_cache
328331
.get(module_id)
329332
.expect("should have module cache");
330333

331334
if let Some(incoming_connections_from_non_modules) = incomings.get(&None) {
332-
let has_active_non_modules_connections = incoming_connections_from_non_modules
333-
.iter()
334-
.any(|connection| connection.is_active(&module_graph, runtime, module_graph_cache));
335+
let has_active_non_modules_connections =
336+
incoming_connections_from_non_modules
337+
.iter()
338+
.any(|connection| {
339+
is_connection_active_in_runtime(
340+
connection,
341+
runtime,
342+
active_incomings,
343+
cached_module_runtime,
344+
&module_graph,
345+
module_graph_cache,
346+
)
347+
});
335348

336349
// TODO: ADD module connection explanations
337350
if has_active_non_modules_connections {
@@ -366,23 +379,36 @@ impl ModuleConcatenationPlugin {
366379
continue;
367380
}
368381

369-
let mut origin_runtime = RuntimeSpec::default();
370-
for r in chunk_graph.get_module_runtimes_iter(*origin_module, chunk_by_ukey) {
371-
origin_runtime.extend(r);
372-
}
373-
374382
let is_intersect = if let Some(runtime) = runtime {
375-
runtime.intersection(&origin_runtime).count() > 0
383+
if let Some(origin_runtime) = module_cache.get(origin_module).map(|m| &m.runtime) {
384+
runtime.intersection(origin_runtime).count() > 0
385+
} else {
386+
let mut origin_runtime = RuntimeSpec::default();
387+
for r in chunk_graph.get_module_runtimes_iter(*origin_module, chunk_by_ukey) {
388+
origin_runtime.extend(r);
389+
}
390+
runtime.intersection(&origin_runtime).count() > 0
391+
}
376392
} else {
377393
false
378394
};
395+
379396
if !is_intersect {
380397
continue;
381398
}
382399

383400
let active_connections: Vec<_> = connections
384401
.iter()
385-
.filter(|&connection| connection.is_active(&module_graph, runtime, module_graph_cache))
402+
.filter(|&connection| {
403+
is_connection_active_in_runtime(
404+
connection,
405+
runtime,
406+
active_incomings,
407+
cached_module_runtime,
408+
&module_graph,
409+
module_graph_cache,
410+
)
411+
})
386412
.collect();
387413

388414
if !active_connections.is_empty() {
@@ -410,11 +436,9 @@ impl ModuleConcatenationPlugin {
410436
let mut names: Vec<_> = other_chunk_modules
411437
.into_iter()
412438
.map(|mid| {
413-
let m = module_graph
414-
.module_by_identifier(mid)
415-
.expect("should have module");
416439
get_cached_readable_identifier(
417-
m,
440+
mid,
441+
&module_graph,
418442
&compilation.module_static_cache_artifact,
419443
&compilation.options.context,
420444
)
@@ -454,11 +478,9 @@ impl ModuleConcatenationPlugin {
454478
let names: Vec<_> = non_esm_connections
455479
.iter()
456480
.map(|(origin_module, connections)| {
457-
let module = module_graph
458-
.module_by_identifier(origin_module)
459-
.expect("should have module");
460481
let readable_identifier = get_cached_readable_identifier(
461-
module,
482+
origin_module,
483+
&module_graph,
462484
&compilation.module_static_cache_artifact,
463485
&compilation.options.context,
464486
);
@@ -533,11 +555,9 @@ impl ModuleConcatenationPlugin {
533555
other_runtime_connections
534556
.iter()
535557
.map(|(origin_module, runtime_condition)| {
536-
let module = module_graph
537-
.module_by_identifier(origin_module)
538-
.expect("should have module");
539558
let readable_identifier = get_cached_readable_identifier(
540-
module,
559+
origin_module,
560+
&module_graph,
541561
&compilation.module_static_cache_artifact,
542562
&compilation.options.context,
543563
);
@@ -637,7 +657,8 @@ impl ModuleConcatenationPlugin {
637657
let root_module_ctxt = RootModuleContext {
638658
id: root_module_id,
639659
readable_identifier: get_cached_readable_identifier(
640-
box_module,
660+
&root_module_id,
661+
&module_graph,
641662
&compilation.module_static_cache_artifact,
642663
&compilation.options.context,
643664
),
@@ -681,7 +702,8 @@ impl ModuleConcatenationPlugin {
681702
Some(compilation),
682703
),
683704
shorten_id: get_cached_readable_identifier(
684-
module,
705+
id,
706+
&module_graph,
685707
&compilation.module_static_cache_artifact,
686708
&compilation.options.context,
687709
),
@@ -1005,8 +1027,12 @@ impl ModuleConcatenationPlugin {
10051027
runtime.extend(r);
10061028
}
10071029

1008-
let _ =
1009-
get_cached_readable_identifier(module, module_static_cache_artifact, compilation_context);
1030+
let _ = get_cached_readable_identifier(
1031+
module_id,
1032+
&module_graph,
1033+
module_static_cache_artifact,
1034+
compilation_context,
1035+
);
10101036

10111037
let connections = module
10121038
.get_dependencies()
@@ -1035,13 +1061,21 @@ impl ModuleConcatenationPlugin {
10351061
.collect::<Vec<_>>();
10361062

10371063
let incomings = module_graph.get_incoming_connections_by_origin_module(module_id);
1064+
let mut active_incomings = HashMap::default();
1065+
for connection in incomings.values().flatten() {
1066+
active_incomings.insert(
1067+
connection.dependency_id,
1068+
connection.is_active(&module_graph, Some(&runtime), module_graph_cache),
1069+
);
1070+
}
10381071
(
10391072
*module_id,
10401073
NoRuntimeModuleCache {
10411074
runtime,
10421075
provided_names,
10431076
connections,
10441077
incomings,
1078+
active_incomings,
10451079
},
10461080
)
10471081
})
@@ -1344,6 +1378,7 @@ pub struct NoRuntimeModuleCache {
13441378
provided_names: bool,
13451379
connections: Vec<(ModuleGraphConnection, (bool, bool))>,
13461380
incomings: HashMap<Option<ModuleIdentifier>, Vec<ModuleGraphConnection>>,
1381+
active_incomings: HashMap<DependencyId, bool>,
13471382
}
13481383

13491384
async fn create_concatenated_module(
@@ -1361,7 +1396,8 @@ async fn create_concatenated_module(
13611396
let root_module_ctxt = RootModuleContext {
13621397
id: root_module_id,
13631398
readable_identifier: get_cached_readable_identifier(
1364-
box_module,
1399+
&root_module_id,
1400+
&module_graph,
13651401
&compilation.module_static_cache_artifact,
13661402
&compilation.options.context,
13671403
),
@@ -1405,7 +1441,8 @@ async fn create_concatenated_module(
14051441
Some(compilation),
14061442
),
14071443
shorten_id: get_cached_readable_identifier(
1408-
module,
1444+
id,
1445+
&module_graph,
14091446
&compilation.module_static_cache_artifact,
14101447
&compilation.options.context,
14111448
),
@@ -1665,3 +1702,31 @@ fn add_concatenated_module(
16651702
module_graph.add_module(new_module.boxed());
16661703
compilation.chunk_graph = chunk_graph;
16671704
}
1705+
1706+
fn is_connection_active_in_runtime(
1707+
connection: &ModuleGraphConnection,
1708+
runtime: Option<&RuntimeSpec>,
1709+
cached_active_incomings: &HashMap<DependencyId, bool>,
1710+
cached_runtime: &RuntimeSpec,
1711+
mg: &ModuleGraph,
1712+
mg_cache: &ModuleGraphCacheArtifact,
1713+
) -> bool {
1714+
if let (Some(cached_active), Some(runtime)) = (
1715+
cached_active_incomings.get(&connection.dependency_id),
1716+
runtime,
1717+
) {
1718+
if runtime == cached_runtime {
1719+
return *cached_active;
1720+
}
1721+
1722+
if *cached_active && cached_runtime.is_subset(runtime) {
1723+
return true;
1724+
}
1725+
1726+
if !*cached_active && cached_runtime.is_superset(runtime) {
1727+
return false;
1728+
}
1729+
}
1730+
1731+
connection.is_active(mg, runtime, mg_cache)
1732+
}

0 commit comments

Comments
 (0)