Skip to content

Commit

Permalink
refactor: Use shrink-to-fit crate instead (#75777)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
kdy1 authored Feb 8, 2025
1 parent a18f3ed commit b5c6404
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 217 deletions.
15 changes: 10 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ smallvec = { version = "1.13.1", features = [
] }
sourcemap = "9.0.0"
strsim = "0.11.1"
shrink-to-fit = "0.2.10"
swc-rustc-hash = { package = "rustc-hash", version = "1.1.0" } # used with swc
syn = "1.0.107"
tempfile = "3.3.0"
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-rcstr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ triomphe = { workspace = true }
turbo-tasks-hash = { workspace = true }
serde = { workspace = true }
new_debug_unreachable = "1.0.6"
shrink-to-fit = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
7 changes: 7 additions & 0 deletions turbopack/crates/turbo-rcstr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{

use debug_unreachable::debug_unreachable;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use shrink_to_fit::ShrinkToFit;
use triomphe::Arc;
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};

Expand Down Expand Up @@ -297,6 +298,12 @@ impl Drop for RcStr {
}
}

/// noop
impl ShrinkToFit for RcStr {
#[inline(always)]
fn shrink_to_fit(&mut self) {}
}

#[cfg(test)]
mod tests {
use std::mem::ManuallyDrop;
Expand Down
2 changes: 0 additions & 2 deletions turbopack/crates/turbo-tasks-macros/src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod deterministic_hash_macro;
mod key_value_pair_macro;
mod non_local_value_macro;
mod operation_value_macro;
mod shrink_to_fit_macro;
mod task_input_macro;
mod trace_raw_vcs_macro;
mod value_debug_format_macro;
Expand All @@ -12,7 +11,6 @@ pub use deterministic_hash_macro::derive_deterministic_hash;
pub use key_value_pair_macro::derive_key_value_pair;
pub use non_local_value_macro::derive_non_local_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};
pub use task_input_macro::derive_task_input;
pub use trace_raw_vcs_macro::derive_trace_raw_vcs;
Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions turbopack/crates/turbo-tasks-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ pub fn derive_trace_raw_vcs_attr(input: TokenStream) -> TokenStream {
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))]
pub fn derive_non_local_value_attr(input: TokenStream) -> TokenStream {
derive::derive_non_local_value(input)
Expand Down
11 changes: 0 additions & 11 deletions turbopack/crates/turbo-tasks-macros/src/primitive_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ pub fn primitive(input: TokenStream) -> TokenStream {
.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! {
#[turbo_tasks::value_impl]
impl turbo_tasks::debug::ValueDebug for #ty {
Expand Down Expand Up @@ -72,7 +62,6 @@ pub fn primitive(input: TokenStream) -> TokenStream {
#value_type_and_register

#value_debug_impl
#value_shrink_to_fit_impl
#value_default_impl
}
.into()
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-macros/src/value_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
turbo_tasks::trace::TraceRawVcs,
turbo_tasks::NonLocalValue,
)]
#[shrink_to_fit(crate = "turbo_tasks::macro_helpers::shrink_to_fit")]
}];
match serialization_mode {
SerializationMode::Auto | SerializationMode::AutoForInput => {
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc", "derive"] }
serde_json = { workspace = true }
serde_regex = "1.1.0"
shrink-to-fit = { workspace=true,features = ["indexmap", "serde_json", "smallvec", "nightly"] }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion turbopack/crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ mod read_ref;
pub mod registry;
mod scope;
mod serialization_invalidation;
mod shrink_to_fit;
pub mod small_duration;
mod state;
pub mod task;
Expand Down
63 changes: 3 additions & 60 deletions turbopack/crates/turbo-tasks/src/macro_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Runtime helpers for [turbo-tasks-macro].
use std::ops::{Deref, DerefMut};
pub use async_trait::async_trait;
pub use once_cell::sync::{Lazy, OnceCell};
pub use serde;
pub use shrink_to_fit;
pub use tracing;

use crate::{
debug::ValueDebugFormatString, shrink_to_fit::ShrinkToFit, task::TaskOutput, NonLocalValue,
RawVc, TaskInput, TaskPersistence, Vc,
debug::ValueDebugFormatString, task::TaskOutput, NonLocalValue, RawVc, TaskInput,
TaskPersistence, Vc,
};
pub use crate::{
magic_any::MagicAny,
Expand Down Expand Up @@ -61,60 +61,3 @@ macro_rules! 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) {}
}
79 changes: 0 additions & 79 deletions turbopack/crates/turbo-tasks/src/shrink_to_fit.rs

This file was deleted.

3 changes: 2 additions & 1 deletion turbopack/crates/turbo-tasks/src/vc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{

use anyhow::Result;
use serde::{Deserialize, Serialize};
use shrink_to_fit::ShrinkToFit;

pub use self::{
cast::{VcCast, VcValueTraitCast, VcValueTypeCast},
Expand All @@ -33,7 +34,7 @@ use crate::{
debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString},
registry,
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
Expand Down

0 comments on commit b5c6404

Please sign in to comment.