Skip to content

feat: rspack_cacheable support as attr#8521

Merged
jerrykingxyz merged 2 commits intomainfrom
jerry/cacheable
Nov 25, 2024
Merged

feat: rspack_cacheable support as attr#8521
jerrykingxyz merged 2 commits intomainfrom
jerry/cacheable

Conversation

@jerrykingxyz
Copy link
Copy Markdown
Contributor

@jerrykingxyz jerrykingxyz commented Nov 25, 2024

Summary

This PR will:

  1. add Inline wrapper type, which can convert the cacheable data reference. Note that since the Inline does not implement the deserialize method, it cannot be used with from_bytes.
use rspack_cacheable::{cacheable, to_bytes, with::Inline};

#[cacheable]
struct Data {
    block1: String
}

#[cacheable]
struct DataRef<'a> {
    #[cacheable(with=Inline)]
    block1: &'a String
}

let data = Data {
    block1: "block1".into()
}
let data_ref = DataRef {
    block1: &data.block1
}
assert_eq!(to_bytes(&data, &()), to_bytes(&data_ref, &()));
  1. add as attr in #[cacheable] when use at struct, and it is used to config the struct that deserialize converts to, requiring its memory layout to match the current structure.
use rspack_cacheable::{
  cacheable, from_bytes, to_bytes,
  with::{AsTuple2, Inline},
};

#[cacheable]
#[derive(Debug, PartialEq, Eq)]
struct Person {
  name: String,
  content: (String, String),
}

#[cacheable(as=Person)]
struct PersonRef<'a> {
  #[rkyv(with=Inline)]
  name: &'a String,
  #[rkyv(with=AsTuple2<Inline, Inline>)]
  content: (&'a String, &'a String),
}

#[test]
fn as_attr() {
  let a = Person {
    name: "abc".into(),
    content: ("a".into(), "b".into()),
  };
  let a_ref = PersonRef {
    name: &a.name,
    content: (&a.content.0, &a.content.1),
  };
  // use a_ref to serialize
  let bytes = to_bytes(&a_ref, &()).unwrap();
  // deserialize to Person
  let deserialize_a: Person = from_bytes(&bytes, &()).unwrap();
  assert_eq!(a, deserialize_a);
}

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added team The issue/pr is created by the member of Rspack. release: feature release: feature related release(mr only) labels Nov 25, 2024
@netlify
Copy link
Copy Markdown

netlify bot commented Nov 25, 2024

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit b0ab1c7
🔍 Latest deploy log https://app.netlify.com/sites/rspack/deploys/67442cc74cd71e0008b4607a

@jerrykingxyz jerrykingxyz changed the title feat(cacheable): cacheable support as attr feat: rspack_cacheable support as attr Nov 25, 2024
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Nov 25, 2024

CodSpeed Performance Report

Merging #8521 will not alter performance

Comparing jerry/cacheable (b0ab1c7) with main (eb12150)

Summary

✅ 1 untouched benchmarks

@jerrykingxyz jerrykingxyz merged commit f6763a9 into main Nov 25, 2024
@jerrykingxyz jerrykingxyz deleted the jerry/cacheable branch November 25, 2024 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants