Unverified Commit b5c64045 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor: Use `shrink-to-fit` crate instead (#75777)

### What?

Use `shrink-to-fit` crate instead of inline trait in turbo-tasks.

### Why?

It's a separate crate, so SWC AST implement the `ShrinkToFit` trait.
parent a18f3ed5
...@@ -2964,7 +2964,7 @@ dependencies = [ ...@@ -2964,7 +2964,7 @@ dependencies = [
"httpdate", "httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
"socket2 0.4.9", "socket2 0.5.8",
"tokio", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
...@@ -6612,18 +6612,21 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" ...@@ -6612,18 +6612,21 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "shrink-to-fit" name = "shrink-to-fit"
version = "0.2.1" version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "040f84743f19cf4a481c2580f4d9d12beab78f401773314ff18835ef78c930ea" checksum = "939a4c696178684fc5fc1426625b882805418bbb56e056d21f9d4946a9d6ff51"
dependencies = [ dependencies = [
"indexmap 2.7.1",
"serde_json",
"shrink-to-fit-macro", "shrink-to-fit-macro",
"smallvec",
] ]
[[package]] [[package]]
name = "shrink-to-fit-macro" name = "shrink-to-fit-macro"
version = "0.2.1" version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e9bca0d4a99ce6c7296fea5972a9327a5e70c062b73f38e11c7894cf7ec72c" checksum = "16d9bafdb4ba0cafd45a5aea7e8bc35b0f6280a603795c2ba9a823ca6afaba73"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
...@@ -9119,6 +9122,7 @@ dependencies = [ ...@@ -9119,6 +9122,7 @@ dependencies = [
"criterion", "criterion",
"new_debug_unreachable", "new_debug_unreachable",
"serde", "serde",
"shrink-to-fit",
"triomphe 0.1.12", "triomphe 0.1.12",
"turbo-tasks-hash", "turbo-tasks-hash",
] ]
...@@ -9169,6 +9173,7 @@ dependencies = [ ...@@ -9169,6 +9173,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_regex", "serde_regex",
"shrink-to-fit",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-util", "tokio-util",
......
...@@ -202,6 +202,7 @@ smallvec = { version = "1.13.1", features = [ ...@@ -202,6 +202,7 @@ smallvec = { version = "1.13.1", features = [
] } ] }
sourcemap = "9.0.0" sourcemap = "9.0.0"
strsim = "0.11.1" strsim = "0.11.1"
shrink-to-fit = "0.2.10"
swc-rustc-hash = { package = "rustc-hash", version = "1.1.0" } # used with swc swc-rustc-hash = { package = "rustc-hash", version = "1.1.0" } # used with swc
syn = "1.0.107" syn = "1.0.107"
tempfile = "3.3.0" tempfile = "3.3.0"
......
...@@ -13,6 +13,7 @@ triomphe = { workspace = true } ...@@ -13,6 +13,7 @@ triomphe = { workspace = true }
turbo-tasks-hash = { workspace = true } turbo-tasks-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
new_debug_unreachable = "1.0.6" new_debug_unreachable = "1.0.6"
shrink-to-fit = { workspace = true }
[dev-dependencies] [dev-dependencies]
criterion = { workspace = true } criterion = { workspace = true }
......
...@@ -11,6 +11,7 @@ use std::{ ...@@ -11,6 +11,7 @@ use std::{
use debug_unreachable::debug_unreachable; use debug_unreachable::debug_unreachable;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use shrink_to_fit::ShrinkToFit;
use triomphe::Arc; use triomphe::Arc;
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher}; use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
...@@ -297,6 +298,12 @@ impl Drop for RcStr { ...@@ -297,6 +298,12 @@ impl Drop for RcStr {
} }
} }
/// noop
impl ShrinkToFit for RcStr {
#[inline(always)]
fn shrink_to_fit(&mut self) {}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
......
...@@ -2,7 +2,6 @@ mod deterministic_hash_macro; ...@@ -2,7 +2,6 @@ mod deterministic_hash_macro;
mod key_value_pair_macro; mod key_value_pair_macro;
mod non_local_value_macro; mod non_local_value_macro;
mod operation_value_macro; mod operation_value_macro;
mod shrink_to_fit_macro;
mod task_input_macro; mod task_input_macro;
mod trace_raw_vcs_macro; mod trace_raw_vcs_macro;
mod value_debug_format_macro; mod value_debug_format_macro;
...@@ -12,7 +11,6 @@ pub use deterministic_hash_macro::derive_deterministic_hash; ...@@ -12,7 +11,6 @@ pub use deterministic_hash_macro::derive_deterministic_hash;
pub use key_value_pair_macro::derive_key_value_pair; pub use key_value_pair_macro::derive_key_value_pair;
pub use non_local_value_macro::derive_non_local_value; pub use non_local_value_macro::derive_non_local_value;
pub use operation_value_macro::derive_operation_value; pub use operation_value_macro::derive_operation_value;
pub use shrink_to_fit_macro::derive_shrink_to_fit;
use syn::{spanned::Spanned, Attribute, Meta, MetaList, NestedMeta}; use syn::{spanned::Spanned, Attribute, Meta, MetaList, NestedMeta};
pub use task_input_macro::derive_task_input; pub use task_input_macro::derive_task_input;
pub use trace_raw_vcs_macro::derive_trace_raw_vcs; pub use trace_raw_vcs_macro::derive_trace_raw_vcs;
......
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, FieldsNamed, FieldsUnnamed};
use turbo_tasks_macros_shared::{generate_exhaustive_destructuring, match_expansion};
pub fn derive_shrink_to_fit(input: TokenStream) -> TokenStream {
let derive_input = parse_macro_input!(input as DeriveInput);
let ident = &derive_input.ident;
let (impl_generics, ty_generics, where_clause) = derive_input.generics.split_for_impl();
let shrink_items = match_expansion(&derive_input, &shrink_named, &shrink_unnamed, &shrink_unit);
quote! {
impl #impl_generics turbo_tasks::ShrinkToFit for #ident #ty_generics #where_clause {
fn shrink_to_fit(&mut self) {
#shrink_items
}
}
}
.into()
}
fn shrink_named(_ident: TokenStream2, fields: &FieldsNamed) -> (TokenStream2, TokenStream2) {
let (captures, fields_idents) = generate_exhaustive_destructuring(fields.named.iter());
(
captures,
quote! {
{#(
turbo_tasks::macro_helpers::ShrinkToFitDerefSpecialization::new(
#fields_idents,
).shrink_to_fit();
)*}
},
)
}
fn shrink_unnamed(_ident: TokenStream2, fields: &FieldsUnnamed) -> (TokenStream2, TokenStream2) {
let (captures, fields_idents) = generate_exhaustive_destructuring(fields.unnamed.iter());
(
captures,
quote! {
{#(
turbo_tasks::macro_helpers::ShrinkToFitDerefSpecialization::new(
#fields_idents,
).shrink_to_fit();
)*}
},
)
}
fn shrink_unit(_ident: TokenStream2) -> TokenStream2 {
quote! { { } }
}
...@@ -23,11 +23,6 @@ pub fn derive_trace_raw_vcs_attr(input: TokenStream) -> TokenStream { ...@@ -23,11 +23,6 @@ pub fn derive_trace_raw_vcs_attr(input: TokenStream) -> TokenStream {
derive::derive_trace_raw_vcs(input) derive::derive_trace_raw_vcs(input)
} }
#[proc_macro_derive(ShrinkToFit, attributes(turbo_tasks))]
pub fn derive_shrink_to_fit(input: TokenStream) -> TokenStream {
derive::derive_shrink_to_fit(input)
}
#[proc_macro_derive(NonLocalValue, attributes(turbo_tasks))] #[proc_macro_derive(NonLocalValue, attributes(turbo_tasks))]
pub fn derive_non_local_value_attr(input: TokenStream) -> TokenStream { pub fn derive_non_local_value_attr(input: TokenStream) -> TokenStream {
derive::derive_non_local_value(input) derive::derive_non_local_value(input)
......
...@@ -16,16 +16,6 @@ pub fn primitive(input: TokenStream) -> TokenStream { ...@@ -16,16 +16,6 @@ pub fn primitive(input: TokenStream) -> TokenStream {
.into(); .into();
}; };
let value_shrink_to_fit_impl = if input.manual_shrink_to_fit.is_none() {
Some(quote! {
impl turbo_tasks::ShrinkToFit for #ty {
fn shrink_to_fit(&mut self) {}
}
})
} else {
None
};
let value_debug_impl = quote! { let value_debug_impl = quote! {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl turbo_tasks::debug::ValueDebug for #ty { impl turbo_tasks::debug::ValueDebug for #ty {
...@@ -72,7 +62,6 @@ pub fn primitive(input: TokenStream) -> TokenStream { ...@@ -72,7 +62,6 @@ pub fn primitive(input: TokenStream) -> TokenStream {
#value_type_and_register #value_type_and_register
#value_debug_impl #value_debug_impl
#value_shrink_to_fit_impl
#value_default_impl #value_default_impl
} }
.into() .into()
......
...@@ -343,6 +343,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -343,6 +343,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
turbo_tasks::trace::TraceRawVcs, turbo_tasks::trace::TraceRawVcs,
turbo_tasks::NonLocalValue, turbo_tasks::NonLocalValue,
)] )]
#[shrink_to_fit(crate = "turbo_tasks::macro_helpers::shrink_to_fit")]
}]; }];
match serialization_mode { match serialization_mode {
SerializationMode::Auto | SerializationMode::AutoForInput => { SerializationMode::Auto | SerializationMode::AutoForInput => {
......
...@@ -40,6 +40,7 @@ rustc-hash = { workspace = true } ...@@ -40,6 +40,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc", "derive"] } serde = { workspace = true, features = ["rc", "derive"] }
serde_json = { workspace = true } serde_json = { workspace = true }
serde_regex = "1.1.0" serde_regex = "1.1.0"
shrink-to-fit = { workspace=true,features = ["indexmap", "serde_json", "smallvec", "nightly"] }
thiserror = { workspace = true } thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true } tokio-util = { workspace = true }
......
...@@ -69,7 +69,6 @@ mod read_ref; ...@@ -69,7 +69,6 @@ mod read_ref;
pub mod registry; pub mod registry;
mod scope; mod scope;
mod serialization_invalidation; mod serialization_invalidation;
mod shrink_to_fit;
pub mod small_duration; pub mod small_duration;
mod state; mod state;
pub mod task; pub mod task;
......
//! Runtime helpers for [turbo-tasks-macro]. //! Runtime helpers for [turbo-tasks-macro].
use std::ops::{Deref, DerefMut};
pub use async_trait::async_trait; pub use async_trait::async_trait;
pub use once_cell::sync::{Lazy, OnceCell}; pub use once_cell::sync::{Lazy, OnceCell};
pub use serde; pub use serde;
pub use shrink_to_fit;
pub use tracing; pub use tracing;
use crate::{ use crate::{
debug::ValueDebugFormatString, shrink_to_fit::ShrinkToFit, task::TaskOutput, NonLocalValue, debug::ValueDebugFormatString, task::TaskOutput, NonLocalValue, RawVc, TaskInput,
RawVc, TaskInput, TaskPersistence, Vc, TaskPersistence, Vc,
}; };
pub use crate::{ pub use crate::{
magic_any::MagicAny, magic_any::MagicAny,
...@@ -61,60 +61,3 @@ macro_rules! stringify_path { ...@@ -61,60 +61,3 @@ macro_rules! stringify_path {
stringify!($path) stringify!($path)
}; };
} }
/// A wrapper type that uses the [autoderef specialization hack][autoderef] to call
/// [`ShrinkToFit::shrink_to_fit`] on types that implement [`ShrinkToFit`].
///
/// This uses a a no-op method [`ShrinkToFitFallbackNoop::shrink_to_fit`] on types that do not
/// implement [`ShrinkToFit`].
///
/// This is used by the derive macro for [`ShrinkToFit`], which is called by the
/// [turbo_tasks::value][macro@crate::value] macro.
///
/// [autoderef]: http://lukaskalbertodt.github.io/2019/12/05/generalized-autoref-based-specialization.html
pub struct ShrinkToFitDerefSpecialization<'a, T> {
inner: ShrinkToFitFallbackNoop<'a, T>,
}
impl<'a, T> ShrinkToFitDerefSpecialization<'a, T> {
pub fn new(real: &'a mut T) -> Self {
Self {
inner: ShrinkToFitFallbackNoop { real },
}
}
}
impl<T> ShrinkToFitDerefSpecialization<'_, T>
where
T: ShrinkToFit,
{
pub fn shrink_to_fit(&mut self) {
// call the real `ShrinkToFit::shrink_to_fit` method
self.inner.real.shrink_to_fit()
}
}
impl<'a, T> Deref for ShrinkToFitDerefSpecialization<'a, T> {
type Target = ShrinkToFitFallbackNoop<'a, T>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for ShrinkToFitDerefSpecialization<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
// Implements `ShrinkToFit` using a no-op `ShrinkToFit::shrink_to_fit` method.
pub struct ShrinkToFitFallbackNoop<'a, T> {
real: &'a mut T,
}
impl<T> ShrinkToFitFallbackNoop<'_, T> {
/// A no-op function called as part of [`ShrinkToFitDerefSpecialization`] when `T` does not
/// implement [`ShrinkToFit`].
pub fn shrink_to_fit(&mut self) {}
}
use std::{
collections::{BinaryHeap, HashMap, VecDeque},
ffi::OsString,
hash::{BuildHasher, Hash},
path::PathBuf,
};
use indexmap::{IndexMap, IndexSet};
pub use turbo_tasks_macros::ShrinkToFit;
/// A type that might have memory capacity that can be shrunk. See [`Vec::shrink_to_fit`] as an
/// example.
///
/// This method may be a no-op. Due to limitaitons of Rust's macro system, it is derived for every
/// [`VcValueType`][crate::VcValueType], even if that type contains no shrinkable collections.
pub trait ShrinkToFit {
fn shrink_to_fit(&mut self);
}
impl ShrinkToFit for String {
fn shrink_to_fit(&mut self) {
String::shrink_to_fit(self);
}
}
impl ShrinkToFit for OsString {
fn shrink_to_fit(&mut self) {
OsString::shrink_to_fit(self);
}
}
impl ShrinkToFit for PathBuf {
fn shrink_to_fit(&mut self) {
PathBuf::shrink_to_fit(self);
}
}
impl<T> ShrinkToFit for Vec<T> {
// NOTE: without real specialization (not the autoderef specialization hack that works in
// macros, but not generics) or negative impls, we cannot call `shrink_to_fit` on nested
// collections inside `T`, so we have to settle with just shrinking the outermost collection.
fn shrink_to_fit(&mut self) {
Vec::shrink_to_fit(self);
}
}
impl<T> ShrinkToFit for VecDeque<T> {
fn shrink_to_fit(&mut self) {
VecDeque::shrink_to_fit(self);
}
}
impl<K, V, S> ShrinkToFit for HashMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
fn shrink_to_fit(&mut self) {
HashMap::shrink_to_fit(self);
}
}
impl<T> ShrinkToFit for BinaryHeap<T> {
fn shrink_to_fit(&mut self) {
BinaryHeap::shrink_to_fit(self);
}
}
impl<K, V, S> ShrinkToFit for IndexMap<K, V, S> {
fn shrink_to_fit(&mut self) {
IndexMap::shrink_to_fit(self);
}
}
impl<T, S> ShrinkToFit for IndexSet<T, S> {
fn shrink_to_fit(&mut self) {
IndexSet::shrink_to_fit(self);
}
}
...@@ -18,6 +18,7 @@ use std::{ ...@@ -18,6 +18,7 @@ use std::{
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use shrink_to_fit::ShrinkToFit;
pub use self::{ pub use self::{
cast::{VcCast, VcValueTraitCast, VcValueTypeCast}, cast::{VcCast, VcValueTraitCast, VcValueTypeCast},
...@@ -33,7 +34,7 @@ use crate::{ ...@@ -33,7 +34,7 @@ use crate::{
debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString}, debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString},
registry, registry,
trace::{TraceRawVcs, TraceRawVcsContext}, trace::{TraceRawVcs, TraceRawVcsContext},
CellId, RawVc, ResolveTypeError, ShrinkToFit, CellId, RawVc, ResolveTypeError,
}; };
/// A "Value Cell" (`Vc` for short) is a reference to a memoized computation result stored on the /// A "Value Cell" (`Vc` for short) is a reference to a memoized computation result stored on the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment