Skip to content

Commit

Permalink
refactor: phases level switcher for incremental options (#7954)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Sep 24, 2024
1 parent ce775cc commit e833f20
Show file tree
Hide file tree
Showing 21 changed files with 398 additions and 131 deletions.
13 changes: 12 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ export interface RawEvalDevToolModulePluginOptions {
export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
incremental?: RawIncremental
rspackFuture: RawRspackFuture
}

Expand Down Expand Up @@ -1391,6 +1392,16 @@ export interface RawIgnorePluginOptions {
checkResource?: (resource: string, context: string) => boolean
}

export interface RawIncremental {
make: boolean
emitAssets: boolean
inferAsyncModules: boolean
providedExports: boolean
moduleHashes: boolean
moduleCodegen: boolean
moduleRuntimeRequirements: boolean
}

export interface RawInfo {
immutable?: boolean
minimized?: boolean
Expand Down Expand Up @@ -1748,7 +1759,7 @@ export interface RawResolveTsconfigOptions {
}

export interface RawRspackFuture {
newIncremental: boolean

}

export interface RawRuleSetCondition {
Expand Down
24 changes: 16 additions & 8 deletions crates/rspack_binding_options/src/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use napi_derive::napi;
use rspack_core::{
CacheOptions, CompilerOptions, Context, Experiments, IncrementalRebuild,
IncrementalRebuildMakeState, ModuleOptions, OutputOptions, References, Target,
CacheOptions, CompilerOptions, Context, Experiments, Incremental, ModuleOptions, OutputOptions,
References, Target,
};

mod raw_builtins;
Expand Down Expand Up @@ -72,13 +72,21 @@ impl TryFrom<RawOptions> for CompilerOptions {
let target = Target::new(&value.target)?;
let cache = value.cache.into();
let experiments = Experiments {
incremental_rebuild: IncrementalRebuild {
make: if matches!(cache, CacheOptions::Disabled) {
None
} else {
Some(IncrementalRebuildMakeState::default())
incremental: match value.experiments.incremental {
Some(value) => Incremental::Enabled {
make: if matches!(cache, CacheOptions::Disabled) {
false
} else {
value.make
},
emit_assets: value.emit_assets,
infer_async_modules: value.infer_async_modules,
provided_exports: value.provided_exports,
module_hashes: value.module_hashes,
module_codegen: value.module_codegen,
module_runtime_requirements: value.module_runtime_requirements,
},
emit_asset: true,
None => Incremental::Disabled,
},
layers: value.experiments.layers,
top_level_await: value.experiments.top_level_await,
Expand Down
47 changes: 35 additions & 12 deletions crates/rspack_binding_options/src/options/raw_experiments.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
use napi_derive::napi;
use rspack_core::RspackFuture;

#[allow(clippy::empty_structs_with_brackets)]
#[derive(Debug, Default)]
#[napi(object)]
pub struct RawRspackFuture {
pub new_incremental: bool,
}
use rspack_core::{Incremental, RspackFuture};

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperiments {
pub layers: bool,
pub top_level_await: bool,
pub incremental: Option<RawIncremental>,
pub rspack_future: RawRspackFuture,
}

impl From<RawRspackFuture> for RspackFuture {
fn from(value: RawRspackFuture) -> Self {
Self {
new_incremental: value.new_incremental,
#[derive(Debug, Default)]
#[napi(object)]
pub struct RawIncremental {
pub make: bool,
pub emit_assets: bool,
pub infer_async_modules: bool,
pub provided_exports: bool,
pub module_hashes: bool,
pub module_codegen: bool,
pub module_runtime_requirements: bool,
}

impl From<RawIncremental> for Incremental {
fn from(value: RawIncremental) -> Self {
Self::Enabled {
make: value.make,
emit_assets: value.emit_assets,
infer_async_modules: value.infer_async_modules,
provided_exports: value.provided_exports,
module_hashes: value.module_hashes,
module_codegen: value.module_codegen,
module_runtime_requirements: value.module_runtime_requirements,
}
}
}

#[allow(clippy::empty_structs_with_brackets)]
#[derive(Debug, Default)]
#[napi(object)]
pub struct RawRspackFuture {}

impl From<RawRspackFuture> for RspackFuture {
fn from(_value: RawRspackFuture) -> Self {
Self {}
}
}
58 changes: 38 additions & 20 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Compilation {
removed_files: HashSet<PathBuf>,
input_filesystem: Arc<dyn ReadableFileSystem>,
) -> Self {
let mutations = options.new_incremental_enabled().then(Mutations::default);
let mutations = options.incremental().enabled().then(Mutations::default);
Self {
id: CompilationId::new(),
hot_index: 0,
Expand Down Expand Up @@ -1068,7 +1068,8 @@ impl Compilation {
)],
)?;

if self.options.new_incremental_enabled() {
let incremental = self.options.incremental();
if incremental.infer_async_modules_enabled() || incremental.provided_exports_enabled() {
self
.unaffected_modules_cache
.compute_affected_modules_with_module_graph(self);
Expand Down Expand Up @@ -1137,10 +1138,6 @@ impl Compilation {
) {}
logger.time_end(start);

// if self.options.is_new_tree_shaking() {
// // let filter = |item: &str| ["config-provider"].iter().any(|pat| item.contains(pat));
// // debug_all_exports_info!(&self.module_graph, filter);
// }
let start = logger.time("create chunks");
use_code_splitting_cache(self, |compilation| async {
build_chunk_graph(compilation)?;
Expand Down Expand Up @@ -1194,31 +1191,52 @@ impl Compilation {

self.assign_runtime_ids();

if self.options.new_incremental_enabled() {
let incremental = self.options.incremental();
let module_hashes_modules;
let module_codegen_modules;
let module_runtime_requirements_modules;
let all_modules: Option<IdentifierSet> = if incremental.module_hashes_enabled()
&& incremental.module_codegen_enabled()
&& incremental.module_runtime_requirements_enabled()
{
None
} else {
Some(self.get_module_graph().modules().keys().copied().collect())
};
if (incremental.module_hashes_enabled()
|| incremental.module_codegen_enabled()
|| incremental.module_runtime_requirements_enabled())
&& let Some(mutations) = &self.mutations
{
self
.unaffected_modules_cache
.compute_affected_modules_with_chunk_graph(self);
}

let modules = if self.options.new_incremental_enabled()
&& let Some(mutations) = &self.mutations
{
let mut modules = self
let mut affected_modules: IdentifierSet = self
.unaffected_modules_cache
.get_affected_modules_with_chunk_graph()
.lock()
.expect("should lock")
.clone();
modules.extend(mutations.iter().filter_map(|mutation| match mutation {
Mutation::ModuleGraphModuleSetAsync { module } => Some(module),
affected_modules.extend(mutations.iter().filter_map(|mutation| match mutation {
Mutation::ModuleSetAsync { module } => Some(module),
_ => None,
}));
modules
let create_task_modules = |enabled: bool| {
enabled
.then(|| affected_modules.clone())
.unwrap_or_else(|| all_modules.clone().expect("failed"))
};
module_hashes_modules = create_task_modules(incremental.module_hashes_enabled());
module_codegen_modules = create_task_modules(incremental.module_codegen_enabled());
module_runtime_requirements_modules =
create_task_modules(incremental.module_runtime_requirements_enabled());
} else {
self.get_module_graph().modules().keys().copied().collect()
module_hashes_modules = all_modules.clone().expect("failed");
module_codegen_modules = all_modules.clone().expect("failed");
module_runtime_requirements_modules = all_modules.clone().expect("failed");
};

self.create_module_hashes(modules.clone())?;
self.create_module_hashes(module_hashes_modules)?;

let start = logger.time("optimize code generation");
plugin_driver
Expand All @@ -1228,13 +1246,13 @@ impl Compilation {
logger.time_end(start);

let start = logger.time("code generation");
self.code_generation(modules.clone())?;
self.code_generation(module_codegen_modules)?;
logger.time_end(start);

let start = logger.time("runtime requirements");
self
.process_runtime_requirements(
modules,
module_runtime_requirements_modules,
self
.chunk_by_ukey
.keys()
Expand Down
17 changes: 9 additions & 8 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ impl Compiler {
self.input_filesystem.clone(),
);

if let Some(state) = self.options.get_incremental_rebuild_make_state() {
state.set_is_not_first();
}

new_compilation.hot_index = self.compilation.hot_index + 1;

let is_incremental_rebuild_make = self.options.is_incremental_rebuild_make_enabled();
if is_incremental_rebuild_make {
let incremental = self.options.incremental();
if incremental.make_enabled() {
// copy field from old compilation
// make stage used
self
Expand All @@ -97,12 +93,17 @@ impl Compiler {
// reuse module executor
new_compilation.module_executor = std::mem::take(&mut self.compilation.module_executor);
}

if self.options.new_incremental_enabled() {
if incremental.infer_async_modules_enabled() {
new_compilation.async_modules = std::mem::take(&mut self.compilation.async_modules);
}
if incremental.module_hashes_enabled() {
new_compilation.cgm_hash_results = std::mem::take(&mut self.compilation.cgm_hash_results);
}
if incremental.module_codegen_enabled() {
new_compilation.code_generation_results =
std::mem::take(&mut self.compilation.code_generation_results);
}
if incremental.module_runtime_requirements_enabled() {
new_compilation.cgm_runtime_requirements_results =
std::mem::take(&mut self.compilation.cgm_runtime_requirements_results);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Compiler {
.iter()
.filter_map(|(filename, asset)| {
// collect version info to new_emitted_asset_versions
if self.options.is_incremental_rebuild_emit_asset_enabled() {
if self.options.incremental().emit_assets_enabled() {
new_emitted_asset_versions.insert(filename.to_string(), asset.info.version.clone());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
T: Fn(&'a mut Compilation) -> F,
F: Future<Output = Result<&'a mut Compilation>>,
{
let is_incremental_rebuild = compilation.options.is_incremental_rebuild_make_enabled();
let is_incremental_rebuild = compilation.options.incremental().make_enabled();
if !is_incremental_rebuild {
task(compilation).await?;
return Ok(());
Expand Down
22 changes: 5 additions & 17 deletions crates/rspack_core/src/options/compiler_options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Incremental;
use crate::{
CacheOptions, Context, DevServerOptions, Experiments, IncrementalRebuildMakeState, Mode,
ModuleOptions, NodeOption, Optimization, OutputOptions, Resolve, SnapshotOptions, StatsOptions,
Target,
CacheOptions, Context, DevServerOptions, Experiments, Mode, ModuleOptions, NodeOption,
Optimization, OutputOptions, Resolve, SnapshotOptions, StatsOptions, Target,
};

#[derive(Debug)]
Expand Down Expand Up @@ -29,19 +29,7 @@ pub struct CompilerOptions {
pub type References = serde_json::Map<String, serde_json::Value>;

impl CompilerOptions {
pub fn is_incremental_rebuild_make_enabled(&self) -> bool {
self.experiments.incremental_rebuild.make.is_some()
}

pub fn get_incremental_rebuild_make_state(&self) -> Option<&IncrementalRebuildMakeState> {
self.experiments.incremental_rebuild.make.as_ref()
}

pub fn is_incremental_rebuild_emit_asset_enabled(&self) -> bool {
self.experiments.incremental_rebuild.emit_asset
}

pub fn new_incremental_enabled(&self) -> bool {
self.experiments.rspack_future.new_incremental
pub fn incremental(&self) -> &Incremental {
&self.experiments.incremental
}
}
Loading

2 comments on commit e833f20

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-09-24 ce775cc) Current Change
10000_development-mode + exec 2.22 s ± 47 ms 2.28 s ± 34 ms +2.34 %
10000_development-mode_hmr + exec 687 ms ± 7.8 ms 717 ms ± 11 ms +4.37 %
10000_production-mode + exec 2.8 s ± 38 ms 2.91 s ± 25 ms +3.87 %
arco-pro_development-mode + exec 1.84 s ± 83 ms 1.85 s ± 72 ms +0.64 %
arco-pro_development-mode_hmr + exec 434 ms ± 1.9 ms 438 ms ± 3.7 ms +1.01 %
arco-pro_production-mode + exec 3.25 s ± 61 ms 3.32 s ± 66 ms +2.13 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.32 s ± 67 ms 3.39 s ± 79 ms +2.27 %
threejs_development-mode_10x + exec 1.68 s ± 21 ms 1.72 s ± 12 ms +1.94 %
threejs_development-mode_10x_hmr + exec 781 ms ± 7.9 ms 802 ms ± 4.9 ms +2.67 %
threejs_production-mode_10x + exec 5.11 s ± 31 ms 5.28 s ± 23 ms +3.25 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success

Please sign in to comment.