Skip to content

Commit

Permalink
feat: improve cache storage (#8675)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Dec 12, 2024
1 parent c8fa8e8 commit 15d948e
Show file tree
Hide file tree
Showing 20 changed files with 648 additions and 310 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/rspack_fs/src/memory_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ impl ReadStream for MemoryReadStream {
async fn read_until(&mut self, byte: u8) -> Result<Vec<u8>> {
let mut buf = vec![];
self.0.read_until(byte, &mut buf).map_err(Error::from)?;
buf.pop();
if buf.last().is_some_and(|b| b == &byte) {
buf.pop();
}
Ok(buf)
}
async fn read_to_end(&mut self) -> Result<Vec<u8>> {
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_fs/src/native_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ impl ReadStream for NativeReadStream {
async fn read_until(&mut self, byte: u8) -> Result<Vec<u8>> {
let mut buf = vec![];
self.0.read_until(byte, &mut buf).map_err(Error::from)?;
buf.pop();
if buf.last().is_some_and(|b| b == &byte) {
buf.pop();
}
Ok(buf)
}
async fn read_to_end(&mut self) -> Result<Vec<u8>> {
Expand Down
34 changes: 28 additions & 6 deletions crates/rspack_storage/src/pack/data/meta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::{SystemTime, UNIX_EPOCH};

use rspack_paths::{Utf8Path, Utf8PathBuf};
use rustc_hash::FxHashSet as HashSet;

use super::options::PackOptions;

Expand All @@ -12,14 +13,39 @@ pub struct PackFileMeta {
pub wrote: bool,
}

#[derive(Debug, Default, Clone)]
pub struct RootMeta {
pub last_modified: u64,
pub scopes: HashSet<String>,
}

impl RootMeta {
pub fn new(scopes: HashSet<String>) -> Self {
Self {
scopes,
last_modified: current_time(),
}
}
pub fn get_path(dir: &Utf8Path) -> Utf8PathBuf {
dir.join("storage_meta")
}
}

pub fn current_time() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("should get current time")
.as_millis() as u64
}

#[derive(Debug, Default)]
pub struct ScopeMeta {
pub path: Utf8PathBuf,
pub bucket_size: usize,
pub pack_size: usize,
pub last_modified: u64,
pub packs: Vec<Vec<PackFileMeta>>,
}

impl ScopeMeta {
pub fn new(dir: &Utf8Path, options: &PackOptions) -> Self {
let mut packs = vec![];
Expand All @@ -30,15 +56,11 @@ impl ScopeMeta {
path: Self::get_path(dir),
bucket_size: options.bucket_size,
pack_size: options.pack_size,
last_modified: SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("should get current time")
.as_millis() as u64,
packs,
}
}

pub fn get_path(dir: &Utf8Path) -> Utf8PathBuf {
dir.join("cache_meta")
dir.join("scope_meta")
}
}
6 changes: 3 additions & 3 deletions crates/rspack_storage/src/pack/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod options;
mod pack;
mod scope;

pub use meta::{PackFileMeta, ScopeMeta};
pub use options::PackOptions;
pub use meta::{current_time, PackFileMeta, RootMeta, ScopeMeta};
pub use options::{PackOptions, RootOptions};
pub use pack::{Pack, PackContents, PackKeys};
pub use scope::PackScope;
pub use scope::{PackScope, RootMetaState};
16 changes: 6 additions & 10 deletions crates/rspack_storage/src/pack/data/options.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use std::time::{SystemTime, UNIX_EPOCH};
use rspack_paths::Utf8PathBuf;

#[derive(Debug)]
pub struct PackOptions {
pub bucket_size: usize,
pub pack_size: usize,
pub expire: u64,
}

impl PackOptions {
pub fn is_expired(&self, last_modified: &u64) -> bool {
let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("get current time failed")
.as_millis() as u64;
current_time - last_modified > self.expire
}
#[derive(Debug)]
pub struct RootOptions {
pub root: Utf8PathBuf,
pub expire: u64,
pub clean: bool,
}
18 changes: 17 additions & 1 deletion crates/rspack_storage/src/pack/data/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@ use itertools::Itertools;
use rspack_paths::Utf8PathBuf;
use rustc_hash::FxHashSet as HashSet;

use super::{Pack, PackOptions, ScopeMeta};
use super::{Pack, PackOptions, RootMeta, ScopeMeta};
use crate::StorageContent;

#[derive(Debug, Default)]
pub enum RootMetaState {
#[default]
Pending,
Value(Option<RootMeta>),
}

impl RootMetaState {
pub fn expect_value(&self) -> &Option<RootMeta> {
match self {
RootMetaState::Value(v) => v,
RootMetaState::Pending => panic!("should have scope meta"),
}
}
}

#[derive(Debug, Default)]
pub enum ScopeMetaState {
#[default]
Expand Down
Loading

2 comments on commit 15d948e

@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-12-12 bd04c60) Current Change
10000_big_production-mode_disable-minimize + exec 37.6 s ± 508 ms 37.3 s ± 422 ms -0.69 %
10000_development-mode + exec 1.81 s ± 43 ms 1.8 s ± 40 ms -0.36 %
10000_development-mode_hmr + exec 664 ms ± 20 ms 649 ms ± 15 ms -2.34 %
10000_production-mode + exec 2.36 s ± 57 ms 2.36 s ± 37 ms -0.30 %
arco-pro_development-mode + exec 1.78 s ± 81 ms 1.73 s ± 80 ms -2.51 %
arco-pro_development-mode_hmr + exec 426 ms ± 9.5 ms 378 ms ± 0.61 ms -11.18 %
arco-pro_production-mode + exec 3.17 s ± 81 ms 3.1 s ± 72 ms -2.06 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.16 s ± 62 ms 3.15 s ± 84 ms -0.25 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.17 s ± 74 ms 3.11 s ± 76 ms -1.93 %
threejs_development-mode_10x + exec 1.62 s ± 15 ms 1.62 s ± 21 ms -0.01 %
threejs_development-mode_10x_hmr + exec 807 ms ± 23 ms 782 ms ± 13 ms -3.08 %
threejs_production-mode_10x + exec 5.17 s ± 112 ms 4.89 s ± 29 ms -5.32 %
10000_big_production-mode_disable-minimize + rss memory 9583 MiB ± 89.8 MiB 9773 MiB ± 77.6 MiB +1.98 %
10000_development-mode + rss memory 794 MiB ± 14.3 MiB 828 MiB ± 27.4 MiB +4.22 %
10000_development-mode_hmr + rss memory 1821 MiB ± 230 MiB 1978 MiB ± 428 MiB +8.58 %
10000_production-mode + rss memory 690 MiB ± 41 MiB 720 MiB ± 50.9 MiB +4.31 %
arco-pro_development-mode + rss memory 704 MiB ± 24.9 MiB 728 MiB ± 37.6 MiB +3.36 %
arco-pro_development-mode_hmr + rss memory 916 MiB ± 59.6 MiB 761 MiB ± 55.6 MiB -16.94 %
arco-pro_production-mode + rss memory 806 MiB ± 38.3 MiB 814 MiB ± 40.1 MiB +0.88 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 808 MiB ± 46 MiB 840 MiB ± 52.4 MiB +3.96 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 801 MiB ± 43.9 MiB 827 MiB ± 53.4 MiB +3.17 %
threejs_development-mode_10x + rss memory 769 MiB ± 37.9 MiB 755 MiB ± 42.2 MiB -1.78 %
threejs_development-mode_10x_hmr + rss memory 1548 MiB ± 311 MiB 1698 MiB ± 284 MiB +9.70 %
threejs_production-mode_10x + rss memory 1095 MiB ± 61.1 MiB 1092 MiB ± 46.8 MiB -0.21 %

@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
rsdoctor ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.