Skip to content

Commit

Permalink
feat-inc-named-module-od
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Dec 2, 2024
1 parent eb71fd3 commit ff6e6ce
Show file tree
Hide file tree
Showing 37 changed files with 182 additions and 200 deletions.
2 changes: 1 addition & 1 deletion crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl Chunk {
.chunk_graph
.get_chunk_entry_modules_with_chunk_group_iterable(&self.ukey)
{
compilation.chunk_graph.get_module_id(*module).hash(hasher);
ChunkGraph::get_module_id(&compilation.module_ids, *module).hash(hasher);
if let Some(chunk_group) = compilation.chunk_group_by_ukey.get(chunk_group) {
chunk_group.id(compilation).hash(hasher);
}
Expand Down
24 changes: 13 additions & 11 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::hash::{Hash, Hasher};

use rspack_collections::{IdentifierSet, UkeySet};
use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet};
use rspack_hash::RspackHashDigest;
use rspack_util::ext::DynHash;
use rustc_hash::FxHasher;
Expand All @@ -17,7 +17,6 @@ use crate::{ChunkGraph, Module};

#[derive(Debug, Clone, Default)]
pub struct ChunkGraphModule {
pub id: Option<String>,
pub(super) entry_in_chunks: UkeySet<ChunkUkey>,
pub chunks: UkeySet<ChunkUkey>,
pub(super) runtime_in_chunks: UkeySet<ChunkUkey>,
Expand All @@ -26,7 +25,6 @@ pub struct ChunkGraphModule {
impl ChunkGraphModule {
pub fn new() -> Self {
Self {
id: None,
entry_in_chunks: Default::default(),
chunks: Default::default(),
runtime_in_chunks: Default::default(),
Expand Down Expand Up @@ -147,14 +145,19 @@ impl ChunkGraph {
runtimes
}

pub fn get_module_id(&self, module_identifier: ModuleIdentifier) -> Option<&str> {
let cgm = self.expect_chunk_graph_module(module_identifier);
cgm.id.as_deref()
pub fn get_module_id(
module_ids: &IdentifierMap<String>,
module_identifier: ModuleIdentifier,
) -> Option<&str> {
module_ids.get(&module_identifier).map(|id| id.as_str())
}

pub fn set_module_id(&mut self, module_identifier: ModuleIdentifier, id: String) {
let cgm = self.expect_chunk_graph_module_mut(module_identifier);
cgm.id = Some(id);
pub fn set_module_id(
module_ids: &mut IdentifierMap<String>,
module_identifier: ModuleIdentifier,
id: String,
) -> Option<String> {
module_ids.insert(module_identifier, id)
}

pub fn get_block_chunk_group<'a>(
Expand Down Expand Up @@ -245,8 +248,7 @@ impl ChunkGraph {
let mut hasher = FxHasher::default();
let mg = compilation.get_module_graph();
let module_identifier = module.identifier();
let cgm = self.expect_chunk_graph_module(module_identifier);
cgm.id.as_ref().dyn_hash(&mut hasher);
Self::get_module_id(&compilation.module_ids, module_identifier).dyn_hash(&mut hasher);
module.source_types().dyn_hash(&mut hasher);
ModuleGraph::is_async(compilation, &module_identifier).dyn_hash(&mut hasher);
mg.get_exports_info(&module_identifier)
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub struct Compilation {
pub async_modules: IdentifierSet,
// artifact for collect_dependencies_diagnostics
pub dependencies_diagnostics: IdentifierMap<Vec<Diagnostic>>,
// artifact for module_ids
pub module_ids: IdentifierMap<String>,
// artifact for code_generation
pub code_generation_results: CodeGenerationResults,
// artifact for create_module_hashes
Expand Down Expand Up @@ -281,6 +283,7 @@ impl Compilation {

async_modules: Default::default(),
dependencies_diagnostics: Default::default(),
module_ids: Default::default(),
code_generation_results: Default::default(),
cgm_hash_results: Default::default(),
cgm_runtime_requirements_results: Default::default(),
Expand Down
13 changes: 7 additions & 6 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::path::PathBuf;

use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rayon::iter::{ParallelBridge, ParallelIterator};
use rspack_collections::{Identifier, IdentifierMap};
use rspack_error::Result;
use rspack_hash::RspackHashDigest;
use rspack_sources::Source;
use rustc_hash::FxHashSet as HashSet;

use crate::{
fast_set, incremental::IncrementalPasses, ChunkKind, Compilation, Compiler, ModuleExecutor,
RuntimeSpec,
fast_set, incremental::IncrementalPasses, ChunkGraph, ChunkKind, Compilation, Compiler,
ModuleExecutor, RuntimeSpec,
};

impl Compiler {
Expand Down Expand Up @@ -192,9 +192,10 @@ pub fn collect_changed_modules(compilation: &Compilation) -> Result<ChangedModul
let modules_map = compilation
.chunk_graph
.chunk_graph_module_by_module_identifier
.par_iter()
.filter_map(|(identifier, cgm)| {
let cid = cgm.id.as_deref();
.keys()
.par_bridge()
.filter_map(|identifier| {
let cid = ChunkGraph::get_module_id(&compilation.module_ids, *identifier);
// TODO: Determine how to calc module hash if module related to multiple runtime code
// gen
if let Some(code_generation_result) = compilation.code_generation_results.get_one(identifier)
Expand Down
15 changes: 4 additions & 11 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,10 @@ impl Task<MakeTaskContext> for ExecuteTask {
compilation.chunk_group_by_ukey.add(entrypoint);

// Assign ids to modules and modules to the chunk
let module_graph = compilation.get_module_graph();
for m in &modules {
let module = module_graph
.module_by_identifier(m)
.expect("should have module");

let id = module.identifier();

chunk_graph.add_module(id);
chunk_graph.set_module_id(*m, id.to_string());
chunk_graph.connect_chunk_and_module(chunk_ukey, *m);
for &m in &modules {
chunk_graph.add_module(m);
ChunkGraph::set_module_id(&mut compilation.module_ids, m, m.to_string());
chunk_graph.connect_chunk_and_module(chunk_ukey, m);
}

// Webpack uses this trick to make sure process_runtime_requirements access
Expand Down
31 changes: 18 additions & 13 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ use crate::{
reserved_names::RESERVED_NAMES, returning_function, runtime_condition_expression,
subtract_runtime_condition, to_identifier, AsyncDependenciesBlockIdentifier, BoxDependency,
BuildContext, BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, BuildResult,
ChunkInitFragments, CodeGenerationDataTopLevelDeclarations, CodeGenerationExportsFinalNames,
CodeGenerationResult, Compilation, ConcatenatedModuleIdent, ConcatenationScope, ConnectionState,
Context, DependenciesBlock, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
ExportInfo, ExportInfoProvided, ExportsArgument, ExportsType, FactoryMeta, IdentCollector,
LibIdentOptions, Module, ModuleDependency, ModuleGraph, ModuleGraphConnection, ModuleIdentifier,
ModuleLayer, ModuleType, Resolve, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType,
SpanExt, Template, UsageState, UsedName, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
ChunkGraph, ChunkInitFragments, CodeGenerationDataTopLevelDeclarations,
CodeGenerationExportsFinalNames, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
ConcatenationScope, ConnectionState, Context, DependenciesBlock, DependencyId,
DependencyTemplate, DependencyType, ErrorSpan, ExportInfo, ExportInfoProvided, ExportsArgument,
ExportsType, FactoryMeta, IdentCollector, LibIdentOptions, Module, ModuleDependency, ModuleGraph,
ModuleGraphConnection, ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition,
RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template, UsageState, UsedName, DEFAULT_EXPORT,
NAMESPACE_OBJECT_EXPORT,
};

type ExportsDefinitionArgs = Vec<(String, String)>;
Expand Down Expand Up @@ -1226,8 +1227,11 @@ impl Module for ConcatenatedModule {
"var {} = {}({});",
info.name.as_ref().expect("should have name"),
RuntimeGlobals::REQUIRE,
serde_json::to_string(&compilation.chunk_graph.get_module_id(info.module))
.expect("should have module id")
serde_json::to_string(
ChunkGraph::get_module_id(&compilation.module_ids, info.module)
.expect("should have module id")
)
.expect("should json stringify module id")
)));

name = info.name.clone();
Expand Down Expand Up @@ -1335,10 +1339,11 @@ impl Module for ConcatenatedModule {
.expect("should have module")
.update_hash(hasher, compilation, generation_runtime)?,
ConcatenationEntry::External(e) => {
compilation
.chunk_graph
.get_module_id(e.module(&compilation.get_module_graph()))
.dyn_hash(hasher);
ChunkGraph::get_module_id(
&compilation.module_ids,
e.module(&compilation.get_module_graph()),
)
.dyn_hash(hasher);
}
};
}
Expand Down
34 changes: 14 additions & 20 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cow_utils::CowUtils;
use derivative::Derivative;
use indoc::formatdoc;
use itertools::Itertools;
use rspack_collections::{Identifiable, Identifier};
use rspack_collections::{Identifiable, Identifier, IdentifierMap};
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_macros::impl_source_map_config;
use rspack_paths::Utf8PathBuf;
Expand Down Expand Up @@ -183,11 +183,8 @@ impl ContextModule {
}
}

pub fn id<'chunk_graph>(&self, chunk_graph: &'chunk_graph ChunkGraph) -> &'chunk_graph str {
chunk_graph
.get_module_id(self.identifier)
.as_ref()
.expect("module id not found")
pub fn get_module_id<'a>(&self, module_ids: &'a IdentifierMap<String>) -> &'a str {
ChunkGraph::get_module_id(module_ids, self.identifier).expect("module id not found")
}

fn get_fake_map(
Expand All @@ -209,10 +206,7 @@ impl ContextModule {
.map(|m| (m, dep_id))
})
.filter_map(|(m, dep)| {
compilation
.chunk_graph
.get_module_id(*m)
.map(|id| (id.to_string(), dep))
ChunkGraph::get_module_id(&compilation.module_ids, *m).map(|id| (id.to_string(), dep))
})
.sorted_unstable_by_key(|(module_id, _)| module_id.to_string());
for (module_id, dep) in sorted_modules {
Expand Down Expand Up @@ -307,7 +301,7 @@ impl ContextModule {
});
let module_id = module_graph
.module_identifier_by_dependency_id(dep_id)
.and_then(|module| compilation.chunk_graph.get_module_id(*module))
.and_then(|module| ChunkGraph::get_module_id(&compilation.module_ids, *module))
.map(|s| s.to_string());
// module_id could be None in weak mode
dep.map(|dep| (dep, module_id))
Expand All @@ -333,7 +327,7 @@ impl ContextModule {
module.exports = webpackEmptyAsyncContext;
"#,
keys = returning_function(&compilation.options.output.environment, "[]", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
})
.boxed()
}
Expand All @@ -351,7 +345,7 @@ impl ContextModule {
module.exports = webpackEmptyContext;
"#,
keys = returning_function(&compilation.options.output.environment, "[]", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
})
.boxed()
}
Expand Down Expand Up @@ -448,7 +442,7 @@ impl ContextModule {
})?;
let module_id = module_graph
.module_identifier_by_dependency_id(d)
.and_then(|m| compilation.chunk_graph.get_module_id(*m))?;
.and_then(|m| ChunkGraph::get_module_id(&compilation.module_ids, *m))?;
Some((chunks, user_request, module_id.to_string()))
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -556,7 +550,7 @@ impl ContextModule {
"#,
map = json_stringify(&map),
keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
}));
source.boxed()
}
Expand Down Expand Up @@ -618,7 +612,7 @@ impl ContextModule {
fake_map_init_statement = self.get_fake_map_init_statement(&fake_map),
has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY,
keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
};
RawSource::from(source).boxed()
}
Expand Down Expand Up @@ -664,7 +658,7 @@ impl ContextModule {
module_factories = RuntimeGlobals::MODULE_FACTORIES,
has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY,
keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
};
RawSource::from(source).boxed()
}
Expand Down Expand Up @@ -705,7 +699,7 @@ impl ContextModule {
module_factories = RuntimeGlobals::MODULE_FACTORIES,
has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY,
keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
};
RawSource::from(source).boxed()
}
Expand Down Expand Up @@ -756,7 +750,7 @@ impl ContextModule {
fake_map_init_statement = self.get_fake_map_init_statement(&fake_map),
has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY,
keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""),
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
};
RawSource::from(source).boxed()
}
Expand Down Expand Up @@ -793,7 +787,7 @@ impl ContextModule {
map = json_stringify(&map),
fake_map_init_statement = self.get_fake_map_init_statement(&fake_map),
has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY,
id = json_stringify(self.id(&compilation.chunk_graph))
id = json_stringify(self.get_module_id(&compilation.module_ids))
};
RawSource::from(source).boxed()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub fn module_id(
if let Some(module_identifier) = compilation
.get_module_graph()
.module_identifier_by_dependency_id(id)
&& let Some(module_id) = compilation.chunk_graph.get_module_id(*module_identifier)
&& let Some(module_id) = ChunkGraph::get_module_id(&compilation.module_ids, *module_identifier)
{
module_id_expr(&compilation.options, request, module_id)
} else if weak {
Expand Down Expand Up @@ -645,7 +645,7 @@ pub fn module_raw(
if let Some(module_identifier) = compilation
.get_module_graph()
.module_identifier_by_dependency_id(id)
&& let Some(module_id) = compilation.chunk_graph.get_module_id(*module_identifier)
&& let Some(module_id) = ChunkGraph::get_module_id(&compilation.module_ids, *module_identifier)
{
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
format!(
Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,8 @@ impl ExternalModule {
}
}
"amd" | "amd-require" | "umd" | "umd2" | "system" | "jsonp" => {
let id = compilation
.get_module_graph()
.module_graph_module_by_identifier(&self.identifier())
.map(|m| m.id(&compilation.chunk_graph))
.unwrap_or_default();
let id = ChunkGraph::get_module_id(&compilation.module_ids, self.identifier())
.expect("should have module id");
format!(
"{} = __WEBPACK_EXTERNAL_MODULE_{}__;",
get_namespace_object_export(concatenation_scope, supports_const),
Expand Down
7 changes: 1 addition & 6 deletions crates/rspack_core/src/module_graph/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_hash::FxHashSet as HashSet;

use crate::ExportsInfo;
use crate::{ChunkGraph, DependencyId, ModuleIdentifier, ModuleIssuer, ModuleProfile};
use crate::{DependencyId, ModuleIdentifier, ModuleIssuer, ModuleProfile};

#[derive(Debug, Clone)]
pub struct ModuleGraphModule {
Expand Down Expand Up @@ -42,11 +42,6 @@ impl ModuleGraphModule {
}
}

pub fn id<'chunk_graph>(&self, chunk_graph: &'chunk_graph ChunkGraph) -> &'chunk_graph str {
let c = chunk_graph.get_module_id(self.module_identifier);
c.unwrap_or_else(|| panic!("{} module id not found", self.module_identifier))
}

pub fn add_incoming_connection(&mut self, dependency_id: DependencyId) {
self.incoming_connections.insert(dependency_id);
}
Expand Down
Loading

0 comments on commit ff6e6ce

Please sign in to comment.