Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Jan 14, 2025
2 parents 4deec10 + 995abc6 commit 2ccee82
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ pub async fn recovery_module_graph(
let mut need_check_dep = vec![];
let mut partial = ModuleGraphPartial::default();
let mut mg = ModuleGraph::new(vec![], Some(&mut partial));
for (_, v) in storage.load(SCOPE).await? {
let mut node: Node =
from_bytes(&v, context).expect("unexpected module graph deserialize failed");
let nodes: Vec<_> = storage
.load(SCOPE)
.await?
.into_par_iter()
.map(|(_, v)| {
from_bytes::<Node, CacheableContext>(&v, context)
.expect("unexpected module graph deserialize failed")
})
.collect();
for mut node in nodes {
for (index_in_block, (dep, parent_block)) in node.dependencies.into_iter().enumerate() {
mg.set_parents(
*dep.id(),
Expand Down
88 changes: 50 additions & 38 deletions crates/rspack_core/src/cache/persistent/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{path::Path, sync::Arc};
use rspack_cacheable::{from_bytes, to_bytes};
use rspack_error::Result;
use rspack_fs::ReadableFileSystem;
use rspack_futures::FuturesResults;
use rspack_paths::{ArcPath, AssertUtf8};
use rustc_hash::FxHashSet as HashSet;

Expand Down Expand Up @@ -42,37 +43,38 @@ impl Snapshot {
#[tracing::instrument("Cache::Snapshot::add", skip_all)]
pub async fn add(&self, paths: impl Iterator<Item = &Path>) {
let default_strategy = StrategyHelper::compile_time();
let mut helper = StrategyHelper::new(self.fs.clone());
// TODO use multi thread
let helper = StrategyHelper::new(self.fs.clone());
// TODO merge package version file
for path in paths {
let utf8_path = path.assert_utf8();
// check path exists
if self.fs.metadata(utf8_path).is_err() {
continue;
}
// TODO directory path should check all sub file
let path_str = utf8_path.as_str();
if self.options.is_immutable_path(path_str) {
continue;
}
if self.options.is_managed_path(path_str) {
if let Some(v) = helper.package_version(path).await {
self.storage.set(
SCOPE,
path.as_os_str().as_encoded_bytes().to_vec(),
to_bytes::<_, ()>(&v, &()).expect("should to bytes success"),
);
continue;
let _ = paths
.map(|path| async {
let utf8_path = path.assert_utf8();
// check path exists
if self.fs.metadata(utf8_path).is_err() {
return;
}
}
// compiler time
self.storage.set(
SCOPE,
path.as_os_str().as_encoded_bytes().to_vec(),
to_bytes::<_, ()>(&default_strategy, &()).expect("should to bytes success"),
);
}
// TODO directory path should check all sub file
let path_str = utf8_path.as_str();
if self.options.is_immutable_path(path_str) {
return;
}
if self.options.is_managed_path(path_str) {
if let Some(v) = helper.package_version(path).await {
self.storage.set(
SCOPE,
path.as_os_str().as_encoded_bytes().to_vec(),
to_bytes::<_, ()>(&v, &()).expect("should to bytes success"),
);
return;
}
}
// compiler time
self.storage.set(
SCOPE,
path.as_os_str().as_encoded_bytes().to_vec(),
to_bytes::<_, ()>(&default_strategy, &()).expect("should to bytes success"),
);
})
.collect::<FuturesResults<_>>();
}

pub fn remove(&self, paths: impl Iterator<Item = &Path>) {
Expand All @@ -85,16 +87,26 @@ impl Snapshot {

#[tracing::instrument("Cache::Snapshot::calc_modified_path", skip_all)]
pub async fn calc_modified_paths(&self) -> Result<(HashSet<ArcPath>, HashSet<ArcPath>)> {
let mut helper = StrategyHelper::new(self.fs.clone());
let helper = StrategyHelper::new(self.fs.clone());

let results = self
.storage
.load(SCOPE)
.await?
.iter()
.map(|(key, value)| async {
let path: ArcPath = Path::new(&*String::from_utf8_lossy(key)).into();
let strategy: Strategy =
from_bytes::<Strategy, ()>(value, &()).expect("should from bytes success");
let validate = helper.validate(&path, &strategy).await;
(path, validate)
})
.collect::<FuturesResults<_>>();

let mut modified_path = HashSet::default();
let mut deleted_path = HashSet::default();

// TODO use multi thread
for (key, value) in self.storage.load(SCOPE).await? {
let path: ArcPath = Path::new(&*String::from_utf8_lossy(&key)).into();
let strategy: Strategy =
from_bytes::<Strategy, ()>(&value, &()).expect("should from bytes success");
match helper.validate(&path, &strategy).await {
for (path, validate) in results.into_inner() {
match validate {
ValidateResult::Modified => {
modified_path.insert(path);
}
Expand Down Expand Up @@ -123,7 +135,7 @@ mod tests {
};
}

#[tokio::test]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn should_snapshot_work() {
let fs = Arc::new(MemoryFileSystem::default());
let storage = Arc::new(MemoryStorage::default());
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_core/src/cache/persistent/snapshot/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use dashmap::DashMap;
use rspack_cacheable::cacheable;
use rspack_fs::ReadableFileSystem;
use rspack_paths::{ArcPath, AssertUtf8};
use rustc_hash::FxHashMap as HashMap;

/// Snapshot check strategy
#[cacheable]
Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum ValidateResult {

pub struct StrategyHelper {
fs: Arc<dyn ReadableFileSystem>,
package_version_cache: HashMap<ArcPath, Option<String>>,
package_version_cache: DashMap<ArcPath, Option<String>>,
}

impl StrategyHelper {
Expand All @@ -60,7 +60,7 @@ impl StrategyHelper {

/// get path file version in package.json
#[async_recursion::async_recursion]
async fn package_version_with_cache(&mut self, path: &Path) -> Option<String> {
async fn package_version_with_cache(&self, path: &Path) -> Option<String> {
if let Some(version) = self.package_version_cache.get(path) {
return version.clone();
}
Expand Down Expand Up @@ -99,15 +99,15 @@ impl StrategyHelper {
Strategy::CompileTime(now)
}
/// get path file package version strategy
pub async fn package_version(&mut self, path: &Path) -> Option<Strategy> {
pub async fn package_version(&self, path: &Path) -> Option<Strategy> {
self
.package_version_with_cache(path)
.await
.map(Strategy::PackageVersion)
}

/// validate path file by target strategy
pub async fn validate(&mut self, path: &Path, strategy: &Strategy) -> ValidateResult {
pub async fn validate(&self, path: &Path, strategy: &Strategy) -> ValidateResult {
match strategy {
Strategy::PackageVersion(version) => {
if let Some(ref cur_version) = self.package_version_with_cache(path).await {
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {
};
assert!(time1 < time2);

let mut helper = StrategyHelper::new(fs.clone());
let helper = StrategyHelper::new(fs.clone());
// modified_time
assert_eq!(
helper.modified_time(Path::new("/file1")).await,
Expand Down
10 changes: 8 additions & 2 deletions packages/rspack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"@discoveryjs/json-ext": "^0.5.7",
"@rspack/dev-server": "1.0.10",
"@rspack/tracing": "workspace:*",
"colorette": "2.0.19",
"exit-hook": "^4.0.0",
"interpret": "^3.1.1",
Expand All @@ -46,6 +45,7 @@
"devDependencies": {
"@rslib/core": "0.2.2",
"@rspack/core": "workspace:*",
"@rspack/tracing": "workspace:*",
"@types/interpret": "^1.1.3",
"@types/rechoir": "^0.6.1",
"@types/webpack-bundle-analyzer": "^4.6.0",
Expand All @@ -57,7 +57,13 @@
"typescript": "^5.7.2"
},
"peerDependencies": {
"@rspack/core": "^1.0.0-alpha || ^1.x"
"@rspack/core": "^1.0.0-alpha || ^1.x",
"@rspack/tracing": "^1.x"
},
"peerDependenciesMeta": {
"@rspack/tracing": {
"optional": true
}
},
"publishConfig": {
"access": "public",
Expand Down
7 changes: 5 additions & 2 deletions packages/rspack-tracing/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@rspack/tracing",
"version": "1.0.12",
"private": true,
"version": "1.2.0-beta.0",
"type": "module",
"description": "Internal tracing package for rspack",
"homepage": "https://rspack.dev",
Expand All @@ -11,6 +10,10 @@
"url": "https://github.com/web-infra-dev/rspack",
"directory": "packages/rspack-tracing"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"license": "MIT",
"main": "./index.js",
"types": "./index.d.ts",
Expand Down
Loading

0 comments on commit 2ccee82

Please sign in to comment.