Skip to content

Commit

Permalink
feat(rspack_cacheable): as attr support use with dyn trait (#8535)
Browse files Browse the repository at this point in the history
* feat(rspack_cacheable): `as` attr support use with dyn trait

* fix: ci
  • Loading branch information
jerrykingxyz authored Nov 26, 2024
1 parent 27e8d88 commit c1e8f11
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 68 deletions.
8 changes: 4 additions & 4 deletions crates/rspack_cacheable/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ impl<'a> ContextGuard<'a> {
Self { context }
}

pub fn add_to_sharing<S: Sharing<SerializeError>>(
pub fn add_to_sharing<S: Sharing<SerializeError> + ?Sized>(
&self,
sharing: &mut S,
) -> Result<(), SerializeError> {
sharing.start_sharing(CONTEXT_ADDR);
sharing.finish_sharing(CONTEXT_ADDR, self as *const _ as usize)
}

pub fn sharing_context<S: Sharing<SerializeError>>(
pub fn sharing_context<S: Sharing<SerializeError> + ?Sized>(
sharing: &'a mut S,
) -> Result<&'a dyn Any, SerializeError> {
match sharing.start_sharing(CONTEXT_ADDR) {
Expand All @@ -40,7 +40,7 @@ impl<'a> ContextGuard<'a> {
}
}

pub fn add_to_pooling<P: Pooling<DeserializeError>>(
pub fn add_to_pooling<P: Pooling<DeserializeError> + ?Sized>(
&self,
pooling: &mut P,
) -> Result<(), DeserializeError> {
Expand All @@ -51,7 +51,7 @@ impl<'a> ContextGuard<'a> {
}
}

pub fn pooling_context<P: Pooling<DeserializeError>>(
pub fn pooling_context<P: Pooling<DeserializeError> + ?Sized>(
pooling: &'a mut P,
) -> Result<&'a dyn Any, DeserializeError> {
match pooling.start_pooling(CONTEXT_ADDR) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_cacheable/src/with/as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
impl<T, A, S> SerializeWith<T, S> for As<A>
where
A: AsConverter<T> + Archive + Serialize<S>,
S: Fallible<Error = SerializeError> + Sharing,
S: Fallible<Error = SerializeError> + Sharing + ?Sized,
{
#[inline]
fn serialize_with(field: &T, serializer: &mut S) -> Result<Self::Resolver, SerializeError> {
Expand All @@ -60,7 +60,7 @@ impl<T, A, D> DeserializeWith<Archived<A>, T, D> for As<A>
where
A: AsConverter<T> + Archive,
A::Archived: Deserialize<A, D>,
D: Fallible<Error = DeserializeError> + Pooling,
D: Fallible<Error = DeserializeError> + Pooling + ?Sized,
{
#[inline]
fn deserialize_with(field: &Archived<A>, de: &mut D) -> Result<T, DeserializeError> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_cacheable/src/with/as_cacheable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T: Archive> ArchiveWith<T> for AsCacheable {
impl<T, S> SerializeWith<T, S> for AsCacheable
where
T: Archive + Serialize<S>,
S: ?Sized + Fallible,
S: Fallible + ?Sized,
{
#[inline]
fn serialize_with(field: &T, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Expand All @@ -32,7 +32,7 @@ impl<T, D> DeserializeWith<Archived<T>, T, D> for AsCacheable
where
T: Archive,
T::Archived: Deserialize<T, D>,
D: ?Sized + Fallible,
D: Fallible + ?Sized,
{
#[inline]
fn deserialize_with(field: &Archived<T>, de: &mut D) -> Result<T, D::Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_cacheable/src/with/as_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T, A, O, D> DeserializeWith<A::Archived, T, D> for AsInner<A>
where
T: AsInnerConverter<Inner = O>,
A: ArchiveWith<O> + DeserializeWith<A::Archived, O, D>,
D: ?Sized + Fallible,
D: Fallible + ?Sized,
{
fn deserialize_with(field: &A::Archived, d: &mut D) -> Result<T, D::Error> {
Ok(T::from_inner(A::deserialize_with(field, d)?))
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_cacheable/src/with/as_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ where
}
}

impl<WK, WV, K, V, S: Fallible + ?Sized> Serialize<S> for Entry<&'_ K, &'_ V, WK, WV>
impl<WK, WV, K, V, S> Serialize<S> for Entry<&'_ K, &'_ V, WK, WV>
where
WK: SerializeWith<K, S>,
WV: SerializeWith<V, S>,
S: Fallible + ?Sized,
{
#[inline]
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Expand Down Expand Up @@ -90,7 +91,7 @@ where
T: AsMapConverter<Key = K, Value = V>,
WK: ArchiveWith<K>,
WV: ArchiveWith<V>,
S: Fallible + ?Sized + Allocator + Writer,
S: Fallible + Allocator + Writer + ?Sized,
for<'a> Entry<&'a K, &'a V, WK, WV>: Serialize<S>,
{
fn serialize_with(field: &T, s: &mut S) -> Result<Self::Resolver, S::Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_cacheable/src/with/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
impl<T, S> SerializeWith<T, S> for AsRefStr
where
T: AsRefStrConverter,
S: ?Sized + Fallible + Writer,
S: Fallible + Writer + ?Sized,
S::Error: Source,
{
#[inline]
Expand All @@ -43,7 +43,7 @@ where
impl<T, D> DeserializeWith<ArchivedString, T, D> for AsRefStr
where
T: AsRefStrConverter,
D: ?Sized + Fallible,
D: Fallible + ?Sized,
{
#[inline]
fn deserialize_with(field: &ArchivedString, _: &mut D) -> Result<T, D::Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_cacheable/src/with/as_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
impl<T, S> SerializeWith<T, S> for AsString
where
T: AsStringConverter,
S: Fallible<Error = SerializeError> + Writer,
S: Fallible<Error = SerializeError> + Writer + ?Sized,
{
#[inline]
fn serialize_with(field: &T, serializer: &mut S) -> Result<Self::Resolver, SerializeError> {
Expand All @@ -52,7 +52,7 @@ where
impl<T, D> DeserializeWith<ArchivedString, T, D> for AsString
where
T: AsStringConverter,
D: Fallible<Error = DeserializeError>,
D: Fallible<Error = DeserializeError> + ?Sized,
{
#[inline]
fn deserialize_with(field: &ArchivedString, _: &mut D) -> Result<T, DeserializeError> {
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_cacheable/src/with/as_tuple2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ where
}
}

impl<A, B, K, V, S: Fallible + ?Sized> SerializeWith<(K, V), S> for AsTuple2<A, B>
impl<A, B, K, V, S> SerializeWith<(K, V), S> for AsTuple2<A, B>
where
A: SerializeWith<K, S>,
B: SerializeWith<V, S>,
S: Fallible + ?Sized,
{
#[inline]
fn serialize_with(field: &(K, V), serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Expand All @@ -49,7 +50,7 @@ impl<A, B, K, V, D> DeserializeWith<ArchivedTuple2<A::Archived, B::Archived>, (K
where
A: ArchiveWith<K> + DeserializeWith<A::Archived, K, D>,
B: ArchiveWith<V> + DeserializeWith<B::Archived, V, D>,
D: ?Sized + Fallible,
D: Fallible + ?Sized,
{
fn deserialize_with(
field: &ArchivedTuple2<A::Archived, B::Archived>,
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_cacheable/src/with/as_tuple3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ where
}
}

impl<A, B, C, K, V, H, S: Fallible + ?Sized> SerializeWith<(K, V, H), S> for AsTuple3<A, B, C>
impl<A, B, C, K, V, H, S> SerializeWith<(K, V, H), S> for AsTuple3<A, B, C>
where
A: SerializeWith<K, S>,
B: SerializeWith<V, S>,
C: SerializeWith<H, S>,
S: Fallible + ?Sized,
{
#[inline]
fn serialize_with(field: &(K, V, H), serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Expand All @@ -57,7 +58,7 @@ where
A: ArchiveWith<K> + DeserializeWith<A::Archived, K, D>,
B: ArchiveWith<V> + DeserializeWith<B::Archived, V, D>,
C: ArchiveWith<H> + DeserializeWith<C::Archived, H, D>,
D: ?Sized + Fallible,
D: Fallible + ?Sized,
{
fn deserialize_with(
field: &ArchivedTuple3<A::Archived, B::Archived, C::Archived>,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_cacheable/src/with/as_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
impl<T, A, O, D> DeserializeWith<ArchivedVec<A::Archived>, T, D> for AsVec<A>
where
T: AsVecConverter<Item = O>,
D: Fallible<Error = DeserializeError>,
D: Fallible<Error = DeserializeError> + ?Sized,
A: ArchiveWith<O> + DeserializeWith<A::Archived, O, D>,
{
fn deserialize_with(field: &ArchivedVec<A::Archived>, d: &mut D) -> Result<T, DeserializeError> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_cacheable/src/with/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
impl<'a, T, F, S> SerializeWith<&'a F, S> for Inline<T>
where
T: SerializeWith<F, S>,
S: ?Sized + Fallible,
S: Fallible + ?Sized,
{
#[inline]
fn serialize_with(field: &&F, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_cacheable/src/with/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<F> ArchiveWith<F> for Unsupported {

impl<F, S> SerializeWith<F, S> for Unsupported
where
S: Fallible<Error = SerializeError>,
S: Fallible<Error = SerializeError> + ?Sized,
{
fn serialize_with(_: &F, _: &mut S) -> Result<(), SerializeError> {
Err(SerializeError::UnsupportedField)
Expand All @@ -26,7 +26,7 @@ where

impl<F, D> DeserializeWith<(), F, D> for Unsupported
where
D: Fallible<Error = DeserializeError>,
D: Fallible<Error = DeserializeError> + ?Sized,
{
fn deserialize_with(_: &(), _: &mut D) -> Result<F, DeserializeError> {
Err(DeserializeError::UnsupportedField)
Expand Down
70 changes: 49 additions & 21 deletions crates/rspack_cacheable_test/tests/macro/cacheable/as_attr.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
use rspack_cacheable::{
cacheable, from_bytes, to_bytes,
with::{AsTuple2, Inline},
cacheable, cacheable_dyn, from_bytes, to_bytes,
with::{AsOption, AsTuple2, AsVec, Inline},
};

#[cacheable_dyn]
trait Module {}

#[cacheable]
struct NormalModule {
inner: String,
}

#[cacheable_dyn]
impl Module for NormalModule {}

#[cacheable]
#[derive(Debug, PartialEq, Eq)]
struct Person {
name: String,
content: (String, String),
struct Data {
block1: String,
block2: Vec<(String, Option<String>)>,
block3: Box<dyn Module>,
}

#[cacheable(as=Person)]
struct PersonRef<'a> {
#[rkyv(with=Inline)]
name: &'a String,
#[rkyv(with=AsTuple2<Inline, Inline>)]
content: (&'a String, &'a String),
#[cacheable(as=Data)]
struct DataRef<'a> {
#[cacheable(with=Inline)]
block1: &'a String,
#[cacheable(with=AsVec<AsTuple2<Inline, AsOption<Inline>>>)]
block2: Vec<(&'a String, Option<&'a String>)>,
#[allow(clippy::borrowed_box)]
#[cacheable(with=Inline)]
block3: &'a Box<dyn Module>,
}

#[test]
#[cfg_attr(miri, ignore)]
fn as_attr() {
let a = Person {
name: "abc".into(),
content: ("a".into(), "b".into()),
let a = Data {
block1: "abc".into(),
block2: vec![
("key1".into(), None),
("key2".into(), Some("value2".into())),
("key3".into(), Some("value3".into())),
],
block3: Box::new(NormalModule {
inner: "inner".into(),
}),
};
let a_ref = PersonRef {
name: &a.name,
content: (&a.content.0, &a.content.1),
let a_ref = DataRef {
block1: &a.block1,
block2: a
.block2
.iter()
.map(|(key, value)| (key, value.as_ref()))
.collect(),
block3: &a.block3,
};
let bytes = to_bytes(&a_ref, &()).unwrap();
let deserialize_a: Person = from_bytes(&bytes, &()).unwrap();
assert_eq!(a, deserialize_a);
let bytes = to_bytes(&a, &()).unwrap();
let bytes_ref = to_bytes(&a_ref, &()).unwrap();
assert_eq!(bytes, bytes_ref);
from_bytes::<Data, ()>(&bytes, &()).unwrap();
}
Loading

2 comments on commit c1e8f11

@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-26 b09e6b9) Current Change
10000_big_production-mode_disable-minimize + exec 40 s ± 958 ms 40.2 s ± 638 ms +0.46 %
10000_development-mode + exec 1.79 s ± 42 ms 1.81 s ± 45 ms +1.23 %
10000_development-mode_hmr + exec 644 ms ± 8.5 ms 640 ms ± 4.6 ms -0.49 %
10000_production-mode + exec 2.38 s ± 26 ms 2.4 s ± 35 ms +0.84 %
arco-pro_development-mode + exec 1.77 s ± 66 ms 1.8 s ± 93 ms +1.68 %
arco-pro_development-mode_hmr + exec 429 ms ± 1.7 ms 430 ms ± 2 ms +0.14 %
arco-pro_production-mode + exec 3.11 s ± 75 ms 3.08 s ± 76 ms -0.77 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.14 s ± 116 ms 3.15 s ± 95 ms +0.35 %
threejs_development-mode_10x + exec 1.61 s ± 12 ms 1.63 s ± 23 ms +1.27 %
threejs_development-mode_10x_hmr + exec 814 ms ± 11 ms 811 ms ± 14 ms -0.37 %
threejs_production-mode_10x + exec 4.89 s ± 30 ms 4.91 s ± 30 ms +0.38 %
10000_big_production-mode_disable-minimize + rss memory 12526 MiB ± 165 MiB 12658 MiB ± 83.3 MiB +1.05 %
10000_development-mode + rss memory 750 MiB ± 21.4 MiB 741 MiB ± 29.1 MiB -1.18 %
10000_development-mode_hmr + rss memory 1411 MiB ± 423 MiB 1615 MiB ± 283 MiB +14.46 %
10000_production-mode + rss memory 657 MiB ± 43.4 MiB 648 MiB ± 28 MiB -1.44 %
arco-pro_development-mode + rss memory 705 MiB ± 20.4 MiB 700 MiB ± 45.5 MiB -0.72 %
arco-pro_development-mode_hmr + rss memory 891 MiB ± 63.6 MiB 860 MiB ± 70.1 MiB -3.50 %
arco-pro_production-mode + rss memory 856 MiB ± 34.6 MiB 867 MiB ± 40.4 MiB +1.31 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 878 MiB ± 51.7 MiB 884 MiB ± 62.2 MiB +0.63 %
threejs_development-mode_10x + rss memory 784 MiB ± 49.3 MiB 803 MiB ± 49.7 MiB +2.51 %
threejs_development-mode_10x_hmr + rss memory 1588 MiB ± 139 MiB 1384 MiB ± 842 MiB -12.89 %
threejs_production-mode_10x + rss memory 1047 MiB ± 66.3 MiB 1036 MiB ± 64.9 MiB -1.08 %

@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.