-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(turbo-tasks): Call
.shrink_to_fit()
on common collection types…
… when constructing a cell
- Loading branch information
Showing
16 changed files
with
351 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../turbo-tasks-testing/tests/shrink_to_fit.rs |
35 changes: 33 additions & 2 deletions
35
turbopack/crates/turbo-tasks-macros-shared/src/primitive_input.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
use proc_macro2::Span; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
Result, Type, | ||
punctuated::Punctuated, | ||
spanned::Spanned, | ||
Meta, Result, Token, Type, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct PrimitiveInput { | ||
pub ty: Type, | ||
pub manual_shrink_to_fit: Option<Span>, | ||
} | ||
|
||
impl Parse for PrimitiveInput { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let ty: Type = input.parse()?; | ||
Ok(PrimitiveInput { ty }) | ||
let mut parsed_input = PrimitiveInput { | ||
ty, | ||
manual_shrink_to_fit: None, | ||
}; | ||
if input.parse::<Option<Token![,]>>()?.is_some() { | ||
let punctuated: Punctuated<Meta, Token![,]> = input.parse_terminated(Meta::parse)?; | ||
for meta in punctuated { | ||
match ( | ||
meta.path() | ||
.get_ident() | ||
.map(ToString::to_string) | ||
.as_deref() | ||
.unwrap_or_default(), | ||
&meta, | ||
) { | ||
("manual_shrink_to_fit", Meta::Path(_)) => { | ||
parsed_input.manual_shrink_to_fit = Some(meta.span()) | ||
} | ||
(_, meta) => { | ||
return Err(syn::Error::new_spanned( | ||
meta, | ||
"unexpected token, expected: \"manual_shrink_to_fit\"", | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
Ok(parsed_input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
turbopack/crates/turbo-tasks-macros/src/derive/shrink_to_fit_macro.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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! { { } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../turbo-tasks-testing/tests/shrink_to_fit.rs |
26 changes: 26 additions & 0 deletions
26
turbopack/crates/turbo-tasks-testing/tests/shrink_to_fit.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![feature(arbitrary_self_types)] | ||
#![feature(arbitrary_self_types_pointers)] | ||
|
||
use anyhow::Result; | ||
use turbo_tasks::Vc; | ||
use turbo_tasks_testing::{register, run, Registration}; | ||
|
||
static REGISTRATION: Registration = register!(); | ||
|
||
#[turbo_tasks::value(transparent)] | ||
struct Wrapper(Vec<u32>); | ||
|
||
#[tokio::test] | ||
async fn test_shrink_to_fit() -> Result<()> { | ||
run(®ISTRATION, || async { | ||
// `Vec::shrink_to_fit` is implicitly called when a cell is constructed. | ||
let a: Vc<Wrapper> = Vc::cell(Vec::with_capacity(100)); | ||
assert_eq!(a.await?.capacity(), 0); | ||
|
||
let b: Vc<Wrapper> = Vc::local_cell(Vec::with_capacity(100)); | ||
assert_eq!(b.await?.capacity(), 0); | ||
|
||
Ok(()) | ||
}) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.