Skip to content

Commit

Permalink
feat: rspack_cacheable support rspack_sources::BoxSource (#8527)
Browse files Browse the repository at this point in the history
* feat: rspack_cacheable support rspack_sources::BoxSource

* fix: ci
  • Loading branch information
jerrykingxyz authored Nov 25, 2024
1 parent 8c3a2b0 commit b09e6b9
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 216 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/rspack_cacheable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub use rspack_macros::{cacheable, cacheable_dyn};
#[cfg(feature = "noop")]
pub use rspack_macros::{disable_cacheable as cacheable, disable_cacheable_dyn as cacheable_dyn};
pub mod r#dyn;
pub mod utils;
pub mod with;

mod context;
Expand Down
3 changes: 0 additions & 3 deletions crates/rspack_cacheable/src/utils/mod.rs

This file was deleted.

108 changes: 0 additions & 108 deletions crates/rspack_cacheable/src/utils/type_wrapper.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/rspack_cacheable/src/with/as_preset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod camino;
mod json;
mod lightningcss;
mod rspack_resolver;
mod rspack_source;
mod rspack_sources;
mod serde_json;
mod swc;
mod ustr;
Expand Down
103 changes: 0 additions & 103 deletions crates/rspack_cacheable/src/with/as_preset/rspack_source/mod.rs

This file was deleted.

86 changes: 86 additions & 0 deletions crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use rkyv::{
rancor::Fallible,
ser::{Allocator, Writer},
with::{ArchiveWith, DeserializeWith, SerializeWith},
Archive, Archived, Deserialize, Place, Resolver, Serialize,
};
use rspack_sources::{
BoxSource, RawSource, Source, SourceExt, SourceMap, SourceMapSource, WithoutOriginalOptions,
};

use super::AsPreset;
use crate::{cacheable, DeserializeError, SerializeError};

#[cacheable(crate=crate)]
pub struct CacheableSource {
buffer: Vec<u8>,
map: Option<String>,
}

pub struct InnerResolver {
source: CacheableSource,
resolver: Resolver<CacheableSource>,
}

impl ArchiveWith<BoxSource> for AsPreset {
type Archived = Archived<CacheableSource>;
type Resolver = InnerResolver;

#[inline]
fn resolve_with(_field: &BoxSource, resolver: Self::Resolver, out: Place<Self::Archived>) {
let InnerResolver { source, resolver } = resolver;
source.resolve(resolver, out)
}
}

impl<S> SerializeWith<BoxSource, S> for AsPreset
where
S: Fallible<Error = SerializeError> + Allocator + Writer,
{
fn serialize_with(
field: &BoxSource,
serializer: &mut S,
) -> Result<Self::Resolver, SerializeError> {
let map = match field.map(&Default::default()) {
Some(map) => Some(
map
.to_json()
.map_err(|_| SerializeError::MessageError("source map to json failed"))?,
),
None => None,
};
let source = CacheableSource {
buffer: field.buffer().to_vec(),
map,
};
Ok(InnerResolver {
resolver: source.serialize(serializer)?,
source,
})
}
}

impl<D> DeserializeWith<Archived<CacheableSource>, BoxSource, D> for AsPreset
where
D: Fallible<Error = DeserializeError>,
{
fn deserialize_with(
field: &Archived<CacheableSource>,
deserializer: &mut D,
) -> Result<BoxSource, DeserializeError> {
let CacheableSource { buffer, map } = field.deserialize(deserializer)?;
if let Some(map) = &map {
if let Ok(source_map) = SourceMap::from_json(map) {
return Ok(
SourceMapSource::new(WithoutOriginalOptions {
value: String::from_utf8_lossy(&buffer),
name: "persistent-cache",
source_map,
})
.boxed(),
);
}
}
Ok(RawSource::from(buffer).boxed())
}
}
1 change: 1 addition & 0 deletions crates/rspack_cacheable_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lightningcss = { workspace = true }
once_cell = { workspace = true }
rspack_cacheable = { path = "../rspack_cacheable" }
rspack_resolver = { workspace = true }
rspack_sources = { workspace = true }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
swc_core = { workspace = true, features = ["ecma_ast"] }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_cacheable_test/tests/with/as_preset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod camino;
mod json;
mod lightningcss;
mod rspack_resolver;
mod rspack_sources;
mod serde_json;
mod swc;
mod ustr;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rspack_cacheable::{cacheable, from_bytes, to_bytes, with::AsPreset};
use rspack_sources::{BoxSource, RawSource, SourceExt};

#[cacheable]
#[derive(Debug)]
struct Data(#[cacheable(with=AsPreset)] BoxSource);

#[test]
fn test_rspack_source() {
fn test_data(data: Data) {
let bytes = to_bytes(&data, &()).unwrap();
let new_data: Data = from_bytes(&bytes, &()).unwrap();
assert_eq!(data.0.buffer(), new_data.0.buffer());
assert_eq!(
data.0.map(&Default::default()),
new_data.0.map(&Default::default())
);
}

test_data(Data(RawSource::from("123".as_bytes()).boxed()));
test_data(Data(RawSource::from("123").boxed()));
}

2 comments on commit b09e6b9

@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-11-25 6dfaba3) Current Change
10000_big_production-mode_disable-minimize + exec 40 s ± 1.05 s 39.8 s ± 1.04 s -0.57 %
10000_development-mode + exec 1.8 s ± 35 ms 1.78 s ± 32 ms -0.85 %
10000_development-mode_hmr + exec 643 ms ± 14 ms 638 ms ± 4.8 ms -0.81 %
10000_production-mode + exec 2.38 s ± 32 ms 2.38 s ± 31 ms -0.16 %
arco-pro_development-mode + exec 1.76 s ± 59 ms 1.75 s ± 79 ms -0.95 %
arco-pro_development-mode_hmr + exec 428 ms ± 0.73 ms 430 ms ± 0.79 ms +0.32 %
arco-pro_production-mode + exec 3.1 s ± 105 ms 3.09 s ± 61 ms -0.21 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.15 s ± 47 ms 3.15 s ± 54 ms -0.12 %
threejs_development-mode_10x + exec 1.62 s ± 18 ms 1.62 s ± 13 ms -0.05 %
threejs_development-mode_10x_hmr + exec 806 ms ± 7 ms 810 ms ± 9.4 ms +0.50 %
threejs_production-mode_10x + exec 4.91 s ± 29 ms 4.9 s ± 41 ms -0.12 %
10000_big_production-mode_disable-minimize + rss memory 12487 MiB ± 63.7 MiB 12729 MiB ± 158 MiB +1.94 %
10000_development-mode + rss memory 762 MiB ± 54.1 MiB 748 MiB ± 16.9 MiB -1.86 %
10000_development-mode_hmr + rss memory 1572 MiB ± 426 MiB 1667 MiB ± 228 MiB +6.05 %
10000_production-mode + rss memory 665 MiB ± 33.2 MiB 661 MiB ± 22.9 MiB -0.69 %
arco-pro_development-mode + rss memory 696 MiB ± 23.1 MiB 688 MiB ± 48.1 MiB -1.16 %
arco-pro_development-mode_hmr + rss memory 888 MiB ± 94.6 MiB 882 MiB ± 37.7 MiB -0.73 %
arco-pro_production-mode + rss memory 871 MiB ± 59.8 MiB 857 MiB ± 41.8 MiB -1.61 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 862 MiB ± 60.1 MiB 860 MiB ± 69.2 MiB -0.15 %
threejs_development-mode_10x + rss memory 784 MiB ± 60.8 MiB 816 MiB ± 50.6 MiB +4.03 %
threejs_development-mode_10x_hmr + rss memory 1496 MiB ± 268 MiB 1401 MiB ± 454 MiB -6.35 %
threejs_production-mode_10x + rss memory 1018 MiB ± 46.3 MiB 1065 MiB ± 64.8 MiB +4.54 %

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

Please sign in to comment.