Skip to content

Commit

Permalink
feat(incremental): side effects optimization (#8799)
Browse files Browse the repository at this point in the history
* feat(incremental): incrementally side effects optimize

* wip

* fix

* fix

* fix
  • Loading branch information
ahabhgk authored Dec 25, 2024
1 parent 6bbe05b commit 93860a8
Show file tree
Hide file tree
Showing 97 changed files with 741 additions and 443 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ esbuild.cpuprofile
!packages/rspack-test-tools/tests/treeShakingCases/node_modules

# Binding artifacts
artifacts
/artifacts/

# Node Addons
npm/**/*.node
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,7 @@ export interface RawIncremental {
inferAsyncModules: boolean
providedExports: boolean
dependenciesDiagnostics: boolean
sideEffects: boolean
buildChunkGraph: boolean
moduleIds: boolean
chunkIds: boolean
Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_binding_values/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl JsChunk {
#[napi(getter)]
pub fn id(&self) -> napi::Result<Either<&str, ()>> {
let (compilation, chunk) = self.as_ref()?;
Ok(match chunk.id(&compilation.chunk_ids) {
Ok(match chunk.id(&compilation.chunk_ids_artifact) {
Some(id) => Either::A(id.as_str()),
None => Either::B(()),
})
Expand All @@ -52,7 +52,7 @@ impl JsChunk {
let (compilation, chunk) = self.as_ref()?;
Ok(
chunk
.id(&compilation.chunk_ids)
.id(&compilation.chunk_ids_artifact)
.map(|id| vec![id.as_str()])
.unwrap_or_default(),
)
Expand Down Expand Up @@ -107,7 +107,7 @@ impl JsChunk {
let (compilation, chunk) = self.as_ref()?;
Ok(
match chunk
.hash(&compilation.chunk_hashes_results)
.hash(&compilation.chunk_hashes_artifact)
.map(|d| d.encoded())
{
Some(hash) => Either::A(hash),
Expand All @@ -121,7 +121,7 @@ impl JsChunk {
let (compilation, chunk) = self.as_ref()?;
Ok(
chunk
.content_hash(&compilation.chunk_hashes_results)
.content_hash(&compilation.chunk_hashes_artifact)
.map(|content_hash| {
content_hash
.iter()
Expand All @@ -137,7 +137,7 @@ impl JsChunk {
let (compilation, chunk) = self.as_ref()?;
Ok(
match chunk.rendered_hash(
&compilation.chunk_hashes_results,
&compilation.chunk_hashes_artifact,
compilation.options.output.hash_digest_length,
) {
Some(hash) => Either::A(hash),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_values/src/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl JsChunkGraph {
pub fn get_module_id(&self, js_module: &JsModule) -> napi::Result<Option<&str>> {
let compilation = self.as_ref()?;
Ok(
ChunkGraph::get_module_id(&compilation.module_ids, js_module.identifier)
ChunkGraph::get_module_id(&compilation.module_ids_artifact, js_module.identifier)
.map(|module_id| module_id.as_str()),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct RawIncremental {
pub infer_async_modules: bool,
pub provided_exports: bool,
pub dependencies_diagnostics: bool,
pub side_effects: bool,
pub build_chunk_graph: bool,
pub module_ids: bool,
pub chunk_ids: bool,
Expand Down Expand Up @@ -35,6 +36,9 @@ impl From<RawIncremental> for IncrementalPasses {
if value.dependencies_diagnostics {
passes.insert(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS);
}
if value.side_effects {
passes.insert(IncrementalPasses::SIDE_EFFECTS);
}
if value.build_chunk_graph {
passes.insert(IncrementalPasses::BUILD_CHUNK_GRAPH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rspack_hash::RspackHashDigest;
use crate::{ModuleIdentifier, RuntimeSpec, RuntimeSpecMap};

#[derive(Debug, Default)]
pub struct CgmHashResults {
pub struct CgmHashArtifact {
module_to_hashes: IdentifierMap<RuntimeSpecMap<RspackHashDigest>>,
}

impl CgmHashResults {
impl CgmHashArtifact {
pub fn is_empty(&self) -> bool {
self.module_to_hashes.is_empty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use rspack_collections::IdentifierMap;
use crate::{ModuleIdentifier, RuntimeGlobals, RuntimeSpec, RuntimeSpecMap};

#[derive(Debug, Default)]
pub struct CgmRuntimeRequirementsResults {
pub struct CgmRuntimeRequirementsArtifact {
module_to_runtime_requirements: IdentifierMap<RuntimeSpecMap<RuntimeGlobals>>,
}

impl CgmRuntimeRequirementsResults {
impl CgmRuntimeRequirementsArtifact {
pub fn is_empty(&self) -> bool {
self.module_to_runtime_requirements.is_empty()
}
Expand Down
25 changes: 25 additions & 0 deletions crates/rspack_core/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rspack_collections::{IdentifierMap, IdentifierSet, UkeyMap};
use rspack_error::Diagnostic;

use crate::{
chunk_graph_chunk::ChunkId, ChunkHashesResult, ChunkRenderResult, ChunkUkey, ModuleId,
RuntimeGlobals,
};

mod cgm_hash_artifact;
mod cgm_runtime_requirement_artifact;
mod code_generation_results;
mod side_effects_do_optimize_artifact;

pub use cgm_hash_artifact::*;
pub use cgm_runtime_requirement_artifact::*;
pub use code_generation_results::*;
pub use side_effects_do_optimize_artifact::*;

pub type AsyncModulesArtifact = IdentifierSet;
pub type DependenciesDiagnosticsArtifact = IdentifierMap<Vec<Diagnostic>>;
pub type ModuleIdsArtifact = IdentifierMap<ModuleId>;
pub type ChunkIdsArtifact = UkeyMap<ChunkUkey, ChunkId>;
pub type CgcRuntimeRequirementsArtifact = UkeyMap<ChunkUkey, RuntimeGlobals>;
pub type ChunkHashesArtifact = UkeyMap<ChunkUkey, ChunkHashesResult>;
pub type ChunkRenderArtifact = UkeyMap<ChunkUkey, ChunkRenderResult>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rspack_collections::UkeyMap;
use rspack_util::atom::Atom;

use crate::{DependencyId, ExportInfo, ModuleIdentifier};

#[derive(Debug, Clone)]
pub struct SideEffectsDoOptimize {
pub ids: Vec<Atom>,
pub target_module: ModuleIdentifier,
pub need_move_target: Option<SideEffectsDoOptimizeMoveTarget>,
}

#[derive(Debug, Clone)]
pub struct SideEffectsDoOptimizeMoveTarget {
pub export_info: ExportInfo,
pub target_export: Option<Vec<Atom>>,
}

pub type SideEffectsOptimizeArtifact = UkeyMap<DependencyId, SideEffectsDoOptimize>;
22 changes: 9 additions & 13 deletions crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use crate::chunk_graph_chunk::ChunkId;
use crate::{
compare_chunk_group, merge_runtime, sort_group_by_index, ChunkGraph, ChunkGroupOrderKey,
RenderManifestEntry,
ChunkIdsArtifact, RenderManifestEntry,
};
use crate::{ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, SourceType};
use crate::{Compilation, EntryOptions, Filename, ModuleGraph, RuntimeSpec};
Expand Down Expand Up @@ -108,21 +108,17 @@ impl Chunk {
self.css_filename_template = filename_template;
}

pub fn id<'a>(&self, chunk_ids: &'a UkeyMap<ChunkUkey, ChunkId>) -> Option<&'a ChunkId> {
pub fn id<'a>(&self, chunk_ids: &'a ChunkIdsArtifact) -> Option<&'a ChunkId> {
ChunkGraph::get_chunk_id(chunk_ids, &self.ukey)
}

pub fn expect_id<'a>(&self, chunk_ids: &'a UkeyMap<ChunkUkey, ChunkId>) -> &'a ChunkId {
pub fn expect_id<'a>(&self, chunk_ids: &'a ChunkIdsArtifact) -> &'a ChunkId {
self
.id(chunk_ids)
.expect("Should set id before calling expect_id")
}

pub fn set_id(
&self,
chunk_ids: &mut UkeyMap<ChunkUkey, ChunkId>,
id: impl Into<ChunkId>,
) -> bool {
pub fn set_id(&self, chunk_ids: &mut ChunkIdsArtifact, id: impl Into<ChunkId>) -> bool {
let id = id.into();
ChunkGraph::set_chunk_id(chunk_ids, self.ukey, id)
}
Expand Down Expand Up @@ -594,7 +590,7 @@ impl Chunk {

pub fn name_for_filename_template<'a>(
&'a self,
chunk_ids: &'a UkeyMap<ChunkUkey, ChunkId>,
chunk_ids: &'a ChunkIdsArtifact,
) -> Option<&'a str> {
if self.name.is_some() {
self.name.as_deref()
Expand All @@ -612,7 +608,7 @@ impl Chunk {
}

pub fn update_hash(&self, hasher: &mut RspackHash, compilation: &Compilation) {
self.id(&compilation.chunk_ids).hash(hasher);
self.id(&compilation.chunk_ids_artifact).hash(hasher);
for module in compilation
.chunk_graph
.get_ordered_chunk_modules(&self.ukey, &compilation.get_module_graph())
Expand Down Expand Up @@ -645,7 +641,7 @@ impl Chunk {
.chunk_graph
.get_chunk_entry_modules_with_chunk_group_iterable(&self.ukey)
{
ChunkGraph::get_module_id(&compilation.module_ids, *module).hash(hasher);
ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module).hash(hasher);
if let Some(chunk_group) = compilation.chunk_group_by_ukey.get(chunk_group) {
chunk_group.id(compilation).hash(hasher);
}
Expand Down Expand Up @@ -737,7 +733,7 @@ impl Chunk {
compilation
.chunk_by_ukey
.expect_get(chunk_ukey)
.id(&compilation.chunk_ids)
.id(&compilation.chunk_ids_artifact)
.cloned()
})
})
Expand All @@ -764,7 +760,7 @@ impl Chunk {
) {
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
if let (Some(chunk_id), Some(child_chunk_ids)) = (
chunk.id(&compilation.chunk_ids).cloned(),
chunk.id(&compilation.chunk_ids_artifact).cloned(),
chunk.get_child_ids_by_order(order, compilation),
) {
result
Expand Down
20 changes: 9 additions & 11 deletions crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ use hashlink::LinkedHashMap;
use indexmap::IndexSet;
use itertools::Itertools;
use rspack_cacheable::cacheable;
use rspack_collections::{
DatabaseItem, IdentifierLinkedMap, IdentifierMap, IdentifierSet, UkeyMap,
};
use rspack_collections::{DatabaseItem, IdentifierLinkedMap, IdentifierMap, IdentifierSet};
use rustc_hash::{FxHashMap as HashMap, FxHashSet};
use serde::{Serialize, Serializer};

use crate::{
find_graph_roots, merge_runtime, BoxModule, Chunk, ChunkByUkey, ChunkGraphModule,
ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, Module, ModuleGraph, ModuleIdentifier,
RuntimeGlobals, RuntimeModule, SourceType,
ChunkGroupByUkey, ChunkGroupUkey, ChunkIdsArtifact, ChunkUkey, Module, ModuleGraph,
ModuleIdentifier, RuntimeGlobals, RuntimeModule, SourceType,
};
use crate::{ChunkGraph, Compilation};

Expand Down Expand Up @@ -512,7 +510,7 @@ impl ChunkGraph {
runtime_requirements: RuntimeGlobals,
) {
compilation
.cgc_runtime_requirements_results
.cgc_runtime_requirements_artifact
.insert(chunk_ukey, runtime_requirements);
}

Expand All @@ -529,15 +527,15 @@ impl ChunkGraph {
chunk_ukey: &ChunkUkey,
) -> &'a RuntimeGlobals {
compilation
.cgc_runtime_requirements_results
.cgc_runtime_requirements_artifact
.get(chunk_ukey)
.unwrap_or_else(|| {
let c = compilation.chunk_graph.expect_chunk_graph_chunk(chunk_ukey);
panic!(
"Chunk({:?} {:?}) should have runtime requirements, {:?}",
c,
chunk_ukey,
&compilation.cgc_runtime_requirements_results.keys()
&compilation.cgc_runtime_requirements_artifact.keys()
)
})
}
Expand Down Expand Up @@ -604,7 +602,7 @@ impl ChunkGraph {
{
let chunk = compilation.chunk_by_ukey.expect_get(c);
map.insert(
chunk.expect_id(&compilation.chunk_ids).to_string(),
chunk.expect_id(&compilation.chunk_ids_artifact).to_string(),
filter(c, compilation),
);
}
Expand Down Expand Up @@ -970,14 +968,14 @@ impl ChunkGraph {
}

pub fn get_chunk_id<'a>(
chunk_ids: &'a UkeyMap<ChunkUkey, ChunkId>,
chunk_ids: &'a ChunkIdsArtifact,
chunk_ukey: &ChunkUkey,
) -> Option<&'a ChunkId> {
chunk_ids.get(chunk_ukey)
}

pub fn set_chunk_id(
chunk_ids: &mut UkeyMap<ChunkUkey, ChunkId>,
chunk_ids: &mut ChunkIdsArtifact,
chunk_ukey: ChunkUkey,
id: ChunkId,
) -> bool {
Expand Down
20 changes: 10 additions & 10 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet};
use rspack_collections::{IdentifierSet, UkeySet};
use rspack_hash::RspackHashDigest;
use rspack_util::ext::DynHash;
use rustc_hash::FxHasher;
Expand All @@ -14,8 +14,8 @@ use tracing::instrument;

use crate::{
AsyncDependenciesBlockIdentifier, ChunkByUkey, ChunkGroup, ChunkGroupByUkey, ChunkGroupUkey,
ChunkUkey, Compilation, ModuleGraph, ModuleIdentifier, RuntimeGlobals, RuntimeSpec,
RuntimeSpecMap, RuntimeSpecSet,
ChunkUkey, Compilation, ModuleGraph, ModuleIdentifier, ModuleIdsArtifact, RuntimeGlobals,
RuntimeSpec, RuntimeSpecMap, RuntimeSpecSet,
};
use crate::{ChunkGraph, Module};

Expand Down Expand Up @@ -169,7 +169,7 @@ impl ChunkGraph {
map: RuntimeSpecMap<RuntimeGlobals>,
) {
compilation
.cgm_runtime_requirements_results
.cgm_runtime_requirements_artifact
.set_runtime_requirements(module_identifier, map);
}

Expand All @@ -179,7 +179,7 @@ impl ChunkGraph {
runtime: &RuntimeSpec,
) -> Option<&'c RuntimeGlobals> {
compilation
.cgm_runtime_requirements_results
.cgm_runtime_requirements_artifact
.get(&module_identifier, runtime)
}

Expand All @@ -198,14 +198,14 @@ impl ChunkGraph {
}

pub fn get_module_id(
module_ids: &IdentifierMap<ModuleId>,
module_ids: &ModuleIdsArtifact,
module_identifier: ModuleIdentifier,
) -> Option<&ModuleId> {
module_ids.get(&module_identifier)
}

pub fn set_module_id(
module_ids: &mut IdentifierMap<ModuleId>,
module_ids: &mut ModuleIdsArtifact,
module_identifier: ModuleIdentifier,
id: ModuleId,
) -> bool {
Expand Down Expand Up @@ -241,7 +241,7 @@ impl ChunkGraph {
runtime: &RuntimeSpec,
) -> Option<&'c RspackHashDigest> {
compilation
.cgm_hash_results
.cgm_hash_artifact
.get(&module_identifier, runtime)
}

Expand All @@ -251,7 +251,7 @@ impl ChunkGraph {
hashes: RuntimeSpecMap<RspackHashDigest>,
) {
compilation
.cgm_hash_results
.cgm_hash_artifact
.set_hashes(module_identifier, hashes);
}

Expand Down Expand Up @@ -303,7 +303,7 @@ impl ChunkGraph {
let mut hasher = FxHasher::default();
let mg = compilation.get_module_graph();
let module_identifier = module.identifier();
Self::get_module_id(&compilation.module_ids, module_identifier).dyn_hash(&mut hasher);
Self::get_module_id(&compilation.module_ids_artifact, 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
Loading

0 comments on commit 93860a8

Please sign in to comment.