Skip to content

Commit

Permalink
chore: 🤖 use hashmap based condition store
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Sep 17, 2023
1 parent e7d103c commit f263f81
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ impl ExportInfo {
.get_max_target()
.values()
.map(|item| UnResolvedExportInfoTarget {
connection: item.connection.clone(),
connection: item.connection,
exports: item.exports.clone(),
})
.clone();
Expand Down
15 changes: 8 additions & 7 deletions crates/rspack_core/src/module_graph/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl From<usize> for ConnectionId {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct ModuleGraphConnection {
/// The referencing module identifier
pub original_module_identifier: Option<ModuleIdentifier>,
Expand All @@ -31,7 +31,6 @@ pub struct ModuleGraphConnection {
pub dependency_id: DependencyId,
active: bool,
conditional: bool,
condition: Option<DependencyCondition>,
}

/// implementing hash by hand because condition maybe a function, which can't be hash
Expand Down Expand Up @@ -65,17 +64,15 @@ impl ModuleGraphConnection {
original_module_identifier: Option<ModuleIdentifier>,
dependency_id: DependencyId,
module_identifier: ModuleIdentifier,
condition: Option<DependencyCondition>,
active: bool,
conditional: bool,
) -> Self {
let active = !matches!(condition, Some(DependencyCondition::False));
let conditional = condition.is_some();
Self {
original_module_identifier,
module_identifier,
dependency_id,
active,
conditional,
condition,
}
}

Expand Down Expand Up @@ -123,7 +120,11 @@ impl ModuleGraphConnection {
module_graph: &ModuleGraph,
runtime: Option<&RuntimeSpec>,
) -> ConnectionState {
match self.condition.as_ref().expect("should have condition") {
match module_graph
.connection_to_condition
.get(&self)
.expect("should have condition")
{
DependencyCondition::False => ConnectionState::Bool(false),
DependencyCondition::Fn(f) => f(self, runtime, module_graph),
}
Expand Down
25 changes: 13 additions & 12 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub struct ModuleGraph {

/// Module graph connections table index for `ConnectionId`
connections_map: HashMap<ModuleGraphConnection, ConnectionId>,
connection_to_condition: HashMap<ConnectionId, DependencyCondition>,

import_var_map: IdentifierMap<ImportVarMap>,
pub exports_info_map: HashMap<ExportsInfoId, ExportsInfo>,
pub export_info_map: HashMap<ExportInfoId, ExportInfo>,
connection_to_condition: HashMap<ModuleGraphConnection, DependencyCondition>,
}

impl ModuleGraph {
Expand Down Expand Up @@ -82,13 +82,6 @@ impl ModuleGraph {
self.dependencies.insert(*dependency.id(), dependency);
}

pub fn get_condition_by_connection_id(
&self,
connection_id: &ConnectionId,
) -> Option<&DependencyCondition> {
self.connection_to_condition.get(connection_id)
}

pub fn dependency_by_id(&self, dependency_id: &DependencyId) -> Option<&BoxDependency> {
self.dependencies.get(dependency_id)
}
Expand Down Expand Up @@ -122,7 +115,7 @@ impl ModuleGraph {
dependency: BoxDependency,
module_identifier: ModuleIdentifier,
) -> Result<()> {
let module_dependency = dependency.as_module_dependency().is_some();
let is_module_dependency = dependency.as_module_dependency().is_some();
let dependency_id = *dependency.id();
let condition = dependency
.as_module_dependency()
Expand All @@ -131,28 +124,36 @@ impl ModuleGraph {
self
.dependency_id_to_module_identifier
.insert(dependency_id, module_identifier);
if !module_dependency {
if !is_module_dependency {
return Ok(());
}

let active = !matches!(condition, Some(DependencyCondition::False));
let conditional = condition.is_some();
// TODO: just a placeholder here, finish this when we have basic `getCondition` logic
let new_connection = ModuleGraphConnection::new(
original_module_identifier,
dependency_id,
module_identifier,
None,
active,
conditional,
);

let connection_id = if let Some(connection_id) = self.connections_map.get(&new_connection) {
*connection_id
} else {
let new_connection_id = ConnectionId::from(self.connections.len());
self.connections.push(Some(new_connection.clone()));
self.connections.push(Some(new_connection));
self
.connections_map
.insert(new_connection, new_connection_id);
new_connection_id
};
if let Some(condition) = condition {
self
.connection_to_condition
.insert(new_connection, condition);
}

self
.dependency_id_to_connection_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,22 @@ impl ModuleDependency for HarmonyImportDependency {
vec![]
}

// It's from HarmonyImportSideEffectDependency.
// TODO: It's from HarmonyImportSideEffectDependency.
fn get_condition(&self) -> Option<DependencyCondition> {
let id = self.id;
Some(DependencyCondition::Fn(Box::new(
move |_, _, module_graph| {
if let Some(module) = module_graph
.parent_module_by_dependency_id(&id)
.and_then(|module_identifier| module_graph.module_by_identifier(&module_identifier))
{
module.get_side_effects_connection_state(module_graph, &mut HashSet::default())
} else {
ConnectionState::Bool(true)
}
},
)))
None
// let id = self.id;
// Some(DependencyCondition::Fn(Box::new(
// move |_, _, module_graph| {
// if let Some(module) = module_graph
// .parent_module_by_dependency_id(&id)
// .and_then(|module_identifier| module_graph.module_by_identifier(&module_identifier))
// {
// module.get_side_effects_connection_state(module_graph, &mut HashSet::default())
// } else {
// ConnectionState::Bool(true)
// }
// },
// )))
}

// It's from HarmonyImportSideEffectDependency.
Expand Down

0 comments on commit f263f81

Please sign in to comment.