Skip to content

Commit

Permalink
feat: support invalidate persistent cache using config.mode and `co…
Browse files Browse the repository at this point in the history
…nfig.name` (#8920)

* feat: support invalidate persistent cache using `mode` and `name`

* test: add test case
  • Loading branch information
jerrykingxyz authored Jan 2, 2025
1 parent aa8732c commit 96084cc
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ export interface RawOptimizationOptions {
}

export interface RawOptions {
name?: string
mode?: undefined | 'production' | 'development' | 'none'
context: string
output: RawOutputOptions
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/raw_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use crate::raw_resolve::*;
#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawOptions {
pub name: Option<String>,
#[napi(ts_type = "undefined | 'production' | 'development' | 'none'")]
pub mode: Option<RawMode>,
pub context: String,
Expand Down Expand Up @@ -80,6 +81,7 @@ impl TryFrom<RawOptions> for CompilerOptions {
let node = value.node.map(|n| n.into());

Ok(CompilerOptions {
name: value.name,
context,
mode,
module,
Expand Down
10 changes: 8 additions & 2 deletions crates/rspack_core/src/cache/persistent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod occasion;
pub mod snapshot;
pub mod storage;
mod version;
use std::{path::PathBuf, sync::Arc};
use std::{hash::Hash, path::PathBuf, sync::Arc};

pub use cacheable_context::{CacheableContext, FromContext};
use occasion::MakeOccasion;
Expand Down Expand Up @@ -49,7 +49,13 @@ impl PersistentCache {
let version = version::get_version(
input_filesystem.clone(),
&option.build_dependencies,
vec![compiler_path, &option.version, rspack_version!()],
|hasher| {
compiler_path.hash(hasher);
option.version.hash(hasher);
rspack_version!().hash(hasher);
compiler_options.name.hash(hasher);
compiler_options.mode.hash(hasher);
},
);
let storage = create_storage(option.storage.clone(), version, intermediate_filesystem);
let context = Arc::new(CacheableContext {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/cache/persistent/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rspack_paths::AssertUtf8;
pub fn get_version(
fs: Arc<dyn ReadableFileSystem>,
dependencies: &Vec<PathBuf>,
salt: Vec<&str>,
add_salt: impl FnOnce(&mut DefaultHasher),
) -> String {
let mut hasher = DefaultHasher::new();
for dep in dependencies {
Expand All @@ -24,6 +24,6 @@ pub fn get_version(
.unwrap_or_else(|_| panic!("Failed to read buildDependency({path}) content."));
bytes.hash(&mut hasher);
}
salt.hash(&mut hasher);
add_salt(&mut hasher);
hex::encode(hasher.finish().to_ne_bytes())
}
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/compiler_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{

#[derive(Debug)]
pub struct CompilerOptions {
pub name: Option<String>,
pub context: Context,
pub output: OutputOptions,
pub mode: Mode,
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/options/compiler_options_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro_rules! f {

#[derive(Debug, Default)]
pub struct CompilerOptionsBuilder {
name: Option<String>,
target: Option<Target>,
entry: IndexMap<String, EntryDescription>,
context: Option<Context>,
Expand All @@ -50,6 +51,11 @@ impl CompilerOptions {
}

impl CompilerOptionsBuilder {
pub fn name(&mut self, name: String) -> &mut Self {
self.name = Some(name);
self
}

pub fn target(&mut self, target: Target) -> &mut Self {
self.target = Some(target);
self
Expand Down Expand Up @@ -108,6 +114,7 @@ impl CompilerOptionsBuilder {
}

pub fn build(&mut self) -> CompilerOptions {
let name = self.name.take();
let context = self.context.take().unwrap_or_else(|| {
std::env::current_dir()
.expect("`current_dir` should be available")
Expand Down Expand Up @@ -160,6 +167,7 @@ impl CompilerOptionsBuilder {
);

CompilerOptions {
name,
context,
output,
mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/options/mode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
pub enum Mode {
Development,
Production,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default 1;
---
export default 2;
---
export default 3;
---
export default 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import value from "./file";

it("should invalidation using config.mode work", async () => {
if (COMPILER_INDEX == 0) {
expect(value).toBe(1);
await NEXT_HMR();
expect(value).toBe(2);
await NEXT_START();
}
if (COMPILER_INDEX == 1) {
expect(value).toBe(3);
await NEXT_HMR();
expect(value).toBe(4);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require("path");

let index = 1;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
experiments: {
cache: {
type: "persistent",
snapshot: {
immutablePaths: [path.join(__dirname, "./file.js")]
}
}
},
plugins: [
{
apply(compiler) {
compiler.hooks.beforeCompile.tap("Test Plugin", function () {
if (index === 1) {
compiler.options.mode = "development";
} else {
compiler.options.mode = "production";
}
index++;
});
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default 1;
---
export default 2;
---
export default 3;
---
export default 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import value from "./file";

it("should invalidation using config.name work", async () => {
if (COMPILER_INDEX == 0) {
expect(value).toBe(1);
await NEXT_HMR();
expect(value).toBe(2);
await NEXT_START();
}
if (COMPILER_INDEX == 1) {
expect(value).toBe(3);
await NEXT_HMR();
expect(value).toBe(4);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require("path");

let index = 1;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
experiments: {
cache: {
type: "persistent",
snapshot: {
immutablePaths: [path.join(__dirname, "./file.js")]
}
}
},
plugins: [
{
apply(compiler) {
compiler.hooks.beforeCompile.tap("Test Plugin", function () {
compiler.options.name = String(index);
index++;
});
}
}
]
};
1 change: 1 addition & 0 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const getRawOptions = (
const mode = options.mode;
const experiments = options.experiments as Required<ExperimentsNormalized>;
return {
name: options.name,
mode,
context: options.context!,
output: options.output as Required<OutputNormalized>,
Expand Down

1 comment on commit 96084cc

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 96084cc Jan 2, 2025

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 (2025-01-02 9a00d39) Current Change
10000_big_production-mode_disable-minimize + exec 37.3 s ± 234 ms 37.5 s ± 394 ms +0.47 %
10000_development-mode + exec 1.88 s ± 30 ms 1.83 s ± 26 ms -2.89 %
10000_development-mode_hmr + exec 680 ms ± 6.9 ms 684 ms ± 16 ms +0.65 %
10000_production-mode + exec 2.5 s ± 44 ms 2.47 s ± 71 ms -0.95 %
arco-pro_development-mode + exec 1.73 s ± 89 ms 1.73 s ± 80 ms +0.56 %
arco-pro_development-mode_hmr + exec 377 ms ± 2.7 ms 378 ms ± 5 ms +0.20 %
arco-pro_production-mode + exec 3.64 s ± 92 ms 3.55 s ± 109 ms -2.37 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.67 s ± 155 ms 3.58 s ± 71 ms -2.19 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.66 s ± 128 ms 3.58 s ± 72 ms -2.40 %
threejs_development-mode_10x + exec 1.52 s ± 22 ms 1.49 s ± 22 ms -1.48 %
threejs_development-mode_10x_hmr + exec 787 ms ± 34 ms 771 ms ± 42 ms -1.97 %
threejs_production-mode_10x + exec 5.37 s ± 95 ms 5.37 s ± 153 ms -0.04 %
10000_big_production-mode_disable-minimize + rss memory 9465 MiB ± 98.4 MiB 9523 MiB ± 30.8 MiB +0.62 %
10000_development-mode + rss memory 659 MiB ± 13.5 MiB 701 MiB ± 27.7 MiB +6.28 %
10000_development-mode_hmr + rss memory 1426 MiB ± 352 MiB 1563 MiB ± 136 MiB +9.59 %
10000_production-mode + rss memory 629 MiB ± 21.3 MiB 678 MiB ± 31.9 MiB +7.78 %
arco-pro_development-mode + rss memory 576 MiB ± 25.9 MiB 579 MiB ± 21.1 MiB +0.58 %
arco-pro_development-mode_hmr + rss memory 624 MiB ± 42 MiB 602 MiB ± 67 MiB -3.64 %
arco-pro_production-mode + rss memory 743 MiB ± 64.5 MiB 733 MiB ± 48.6 MiB -1.31 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 743 MiB ± 39.9 MiB 734 MiB ± 34 MiB -1.12 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 751 MiB ± 51.1 MiB 731 MiB ± 63.1 MiB -2.64 %
threejs_development-mode_10x + rss memory 613 MiB ± 23.4 MiB 621 MiB ± 32.9 MiB +1.27 %
threejs_development-mode_10x_hmr + rss memory 1175 MiB ± 68.7 MiB 1131 MiB ± 171 MiB -3.72 %
threejs_production-mode_10x + rss memory 857 MiB ± 44.9 MiB 906 MiB ± 54.9 MiB +5.70 %

Please sign in to comment.