Skip to content

Commit

Permalink
remove ConcreteTaskInput in favor of the original rust datatypes (ver…
Browse files Browse the repository at this point in the history
…cel/turborepo#8737)

### Description

Instead of the ConcreteTaskInput conversion, we use a boxed any. This
allows to store arguments in plain rust structures and uses less memory.

We need to do some magic to make serialization possible.

Also removed Ord requirement from TaskInputs and make Vc not implement
Ord

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra committed Jul 18, 2024
1 parent 1d91263 commit 476d187
Show file tree
Hide file tree
Showing 70 changed files with 1,249 additions and 1,612 deletions.
27 changes: 3 additions & 24 deletions crates/turbo-tasks-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use turbo_tasks_macros_shared::{
get_impl_function_ident, get_native_function_ident, get_path_ident,
get_register_trait_methods_ident, get_register_value_type_ident,
get_trait_default_impl_function_ident, get_trait_impl_function_ident, get_trait_type_ident,
get_type_ident, GenericTypeInput, PrimitiveInput, ValueTraitArguments,
get_type_ident, GenericTypeInput, PrimitiveInput,
};

pub fn generate_register() {
Expand Down Expand Up @@ -361,10 +361,10 @@ impl<'a> RegisterContext<'a> {
}

fn process_trait_inner(&mut self, trait_item: &ItemTrait) -> Result<()> {
if let Some(attr) = trait_item
if trait_item
.attrs
.iter()
.find(|a| is_turbo_attribute(a, "value_trait"))
.any(|a| is_turbo_attribute(a, "value_trait"))
{
let trait_ident = &trait_item.ident;

Expand All @@ -388,16 +388,6 @@ impl<'a> RegisterContext<'a> {

let trait_type_ident = get_trait_type_ident(trait_ident);
self.register(trait_type_ident, self.get_global_name(&[trait_ident]))?;

let trait_args: ValueTraitArguments = parse_attr_args(attr)?.unwrap_or_default();
if trait_args.debug {
self.register_debug_impl(
&get_type_ident(&parse_quote! {
Box<dyn #trait_ident>
})
.unwrap(),
)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -610,14 +600,3 @@ fn is_cfg_attribute(attr: &Attribute) -> bool {
.get_ident()
.is_some_and(|ident| ident == "cfg" || ident == "cfg_attr")
}

fn parse_attr_args<T>(attr: &Attribute) -> syn::Result<Option<T>>
where
T: syn::parse::Parse,
{
if attr.tokens.is_empty() {
Ok(None)
} else {
Ok(Some(attr.parse_args_with(T::parse)?))
}
}
76 changes: 44 additions & 32 deletions crates/turbo-tasks-macros-shared/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use syn::{
/// These helpers should themselves call [generate_destructuring] to generate
/// the destructure necessary to access the fields of the value.
pub fn match_expansion<
EN: Fn(&Ident, &FieldsNamed) -> (TokenStream, TokenStream),
EU: Fn(&Ident, &FieldsUnnamed) -> (TokenStream, TokenStream),
U: Fn(&Ident) -> TokenStream,
EN: Fn(TokenStream, &FieldsNamed) -> (TokenStream, TokenStream),
EU: Fn(TokenStream, &FieldsUnnamed) -> (TokenStream, TokenStream),
U: Fn(TokenStream) -> TokenStream,
>(
derive_input: &DeriveInput,
expand_named: &EN,
Expand All @@ -33,36 +33,48 @@ pub fn match_expansion<
let expand_unit = move |ident| (TokenStream::new(), expand_unit(ident));
match &derive_input.data {
Data::Enum(DataEnum { variants, .. }) => {
let (variants_idents, (variants_fields_capture, expansion)): (
Vec<_>,
(Vec<_>, Vec<_>),
) = variants
.iter()
.map(|variant| {
(
&variant.ident,
expand_fields(
&variant.ident,
&variant.fields,
expand_named,
expand_unnamed,
expand_unit,
),
)
})
.unzip();
let (idents, (variants_fields_capture, expansion)): (Vec<_>, (Vec<_>, Vec<_>)) =
variants
.iter()
.map(|variant| {
let variants_idents = &variant.ident;
let ident = quote! { #ident::#variants_idents };
(
ident.clone(),
expand_fields(
ident,
&variant.fields,
expand_named,
expand_unnamed,
expand_unit,
),
)
})
.unzip();

quote! {
match self {
#(
#ident::#variants_idents #variants_fields_capture => #expansion,
)*
if idents.is_empty() {
let (_, expansion) = expand_unit(quote! { #ident });
quote! {
#expansion
}
} else {
quote! {
match self {
#(
#idents #variants_fields_capture => #expansion,
)*
}
}
}
}
Data::Struct(DataStruct { fields, .. }) => {
let (captures, expansion) =
expand_fields(ident, fields, expand_named, expand_unnamed, expand_unit);
let (captures, expansion) = expand_fields(
quote! { #ident },
fields,
expand_named,
expand_unnamed,
expand_unit,
);

if fields.is_empty() {
assert!(captures.is_empty());
Expand Down Expand Up @@ -100,12 +112,12 @@ pub fn match_expansion<
pub fn expand_fields<
'ident,
'fields,
EN: Fn(&'ident Ident, &'fields FieldsNamed) -> R,
EU: Fn(&'ident Ident, &'fields FieldsUnnamed) -> R,
U: Fn(&'ident Ident) -> R,
EN: Fn(TokenStream, &'fields FieldsNamed) -> R,
EU: Fn(TokenStream, &'fields FieldsUnnamed) -> R,
U: Fn(TokenStream) -> R,
R,
>(
ident: &'ident Ident,
ident: TokenStream,
fields: &'fields Fields,
expand_named: EN,
expand_unnamed: EU,
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks-macros-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dev-dependencies]
anyhow = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true }
turbo-tasks = { workspace = true }
turbo-tasks-memory = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/turbo-tasks-macros-tests/tests/task_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//! However, we keep one test here as an integration test between the derive
//! macro and the `#[turbo_tasks::function]` macro.

use serde::{Deserialize, Serialize};
use turbo_tasks::{Completion, TaskInput, Vc};
use turbo_tasks_testing::{register, run};

register!();

#[derive(Clone, TaskInput)]
#[derive(Clone, TaskInput, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct OneUnnamedField(u32);

#[turbo_tasks::function]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenStream as TokenStream2};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, FieldsNamed, FieldsUnnamed};
use turbo_tasks_macros_shared::{generate_exhaustive_destructuring, match_expansion};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn derive_deterministic_hash(input: TokenStream) -> TokenStream {

/// Hashes a struct or enum variant with named fields (e.g. `struct Foo {
/// bar: u32 }`, `Foo::Bar { baz: u32 }`).
fn hash_named(_ident: &Ident, fields: &FieldsNamed) -> (TokenStream2, TokenStream2) {
fn hash_named(_ident: TokenStream2, fields: &FieldsNamed) -> (TokenStream2, TokenStream2) {
let (captures, fields_idents) = generate_exhaustive_destructuring(fields.named.iter());
(
captures,
Expand All @@ -49,7 +49,7 @@ fn hash_named(_ident: &Ident, fields: &FieldsNamed) -> (TokenStream2, TokenStrea

/// Hashes a struct or enum variant with unnamed fields (e.g. `struct
/// Foo(u32)`, `Foo::Bar(u32)`).
fn hash_unnamed(_ident: &Ident, fields: &FieldsUnnamed) -> (TokenStream2, TokenStream2) {
fn hash_unnamed(_ident: TokenStream2, fields: &FieldsUnnamed) -> (TokenStream2, TokenStream2) {
let (captures, fields_idents) = generate_exhaustive_destructuring(fields.unnamed.iter());
(
captures,
Expand All @@ -62,6 +62,6 @@ fn hash_unnamed(_ident: &Ident, fields: &FieldsUnnamed) -> (TokenStream2, TokenS
}

/// Hashes a unit struct or enum variant (e.g. `struct Foo;`, `Foo::Bar`).
fn hash_unit(_ident: &Ident) -> TokenStream2 {
fn hash_unit(_ident: TokenStream2) -> TokenStream2 {
quote! { { } }
}
Loading

0 comments on commit 476d187

Please sign in to comment.