Unverified Commit 5ca1aa7f authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: perf: Fix unused argument filtering optimization in turbo-tasks for…

Turbopack: perf: Fix unused argument filtering optimization in turbo-tasks for `self` arguments (#86231)

Should fix https://github.com/vercel/next.js/pull/86203

## Validation

I ran:

```
rm -rf .next && NEXT_TURBOPACK_TASK_STATISTICS=~/stats-with-fix.json pnpm next build --turbopack --experimental-build-mode=compile
```

over vercel-site and used `jq '[.[].cache_miss]|add'
~/stats-with-fix.json` and `jq '[.[].cache_hit]|add'
~/stats-with-fix.json` to measure the total cache hits/misses. These
numbers are pretty stable from run-to-run (`cache_miss` shows zero
variance, `cache_hit` shows a minor variance) because Turbopack is
(mostly) deterministic.

**Canary:**
```
cache_miss: 8603613
cache_hit: 34824116
```

**With This Fix:**
```
cache_miss: 8602660
cache_hit: 34825148
```

The difference is surprisingly small (much less than 1%), but it's still
an improvement.
parent 3f4db01d
...@@ -9383,6 +9383,7 @@ dependencies = [ ...@@ -9383,6 +9383,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
"rstest",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"syn 2.0.104", "syn 2.0.104",
] ]
......
...@@ -50,6 +50,7 @@ impl Adder { ...@@ -50,6 +50,7 @@ impl Adder {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn add_method(&self, arg1: ResolvedVc<u16>, arg2: ResolvedVc<u32>) -> Result<Vc<u64>> { async fn add_method(&self, arg1: ResolvedVc<u16>, arg2: ResolvedVc<u32>) -> Result<Vc<u64>> {
let _ = self; // Make sure unused argument filtering doesn't remove the arg
Ok(Vc::cell(u64::from(*arg1.await?) + u64::from(*arg2.await?))) Ok(Vc::cell(u64::from(*arg1.await?) + u64::from(*arg2.await?)))
} }
} }
......
...@@ -20,3 +20,6 @@ quote = { workspace = true } ...@@ -20,3 +20,6 @@ quote = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
syn = { workspace = true, features = ["full", "extra-traits", "visit", "visit-mut"] } syn = { workspace = true, features = ["full", "extra-traits", "visit", "visit-mut"] }
[dev-dependencies]
rstest = { workspace = true }
...@@ -27,6 +27,7 @@ pub struct TurboFn<'a> { ...@@ -27,6 +27,7 @@ pub struct TurboFn<'a> {
output: Type, output: Type,
this: Option<Input>, this: Option<Input>,
is_self_used: bool,
exposed_inputs: Vec<Input>, exposed_inputs: Vec<Input>,
/// Should we return `OperationVc` and require that all arguments are `NonLocalValue`s? /// Should we return `OperationVc` and require that all arguments are `NonLocalValue`s?
operation: bool, operation: bool,
...@@ -43,6 +44,7 @@ impl TurboFn<'_> { ...@@ -43,6 +44,7 @@ impl TurboFn<'_> {
orig_signature: &Signature, orig_signature: &Signature,
definition_context: DefinitionContext, definition_context: DefinitionContext,
args: FunctionArguments, args: FunctionArguments,
is_self_used: bool,
) -> Option<TurboFn<'_>> { ) -> Option<TurboFn<'_>> {
if !orig_signature.generics.params.is_empty() { if !orig_signature.generics.params.is_empty() {
orig_signature orig_signature
...@@ -202,6 +204,7 @@ impl TurboFn<'_> { ...@@ -202,6 +204,7 @@ impl TurboFn<'_> {
ident: orig_ident, ident: orig_ident,
output, output,
this, this,
is_self_used,
exposed_inputs, exposed_inputs,
operation: args.operation.is_some(), operation: args.operation.is_some(),
inline_ident, inline_ident,
...@@ -271,7 +274,6 @@ impl TurboFn<'_> { ...@@ -271,7 +274,6 @@ impl TurboFn<'_> {
pub fn inline_signature_and_block<'a>( pub fn inline_signature_and_block<'a>(
&self, &self,
orig_block: &'a Block, orig_block: &'a Block,
is_self_used: bool,
) -> (Signature, Cow<'a, Block>) { ) -> (Signature, Cow<'a, Block>) {
let mut shadow_self = None; let mut shadow_self = None;
let (inputs, transform_stmts): (Punctuated<_, _>, Vec<Option<_>>) = self let (inputs, transform_stmts): (Punctuated<_, _>, Vec<Option<_>>) = self
...@@ -280,7 +282,7 @@ impl TurboFn<'_> { ...@@ -280,7 +282,7 @@ impl TurboFn<'_> {
.iter() .iter()
.filter(|arg| { .filter(|arg| {
let FnArg::Typed(pat_type) = arg else { let FnArg::Typed(pat_type) = arg else {
return is_self_used; return self.is_self_used;
}; };
let Pat::Ident(pat_id) = &*pat_type.pat else { let Pat::Ident(pat_id) = &*pat_type.pat else {
return true; return true;
...@@ -543,8 +545,7 @@ impl TurboFn<'_> { ...@@ -543,8 +545,7 @@ impl TurboFn<'_> {
} }
} }
/// The block of the exposed function for a dynamic dispatch call to the /// The block of the exposed function for a dynamic dispatch call to the given trait.
/// given trait.
pub fn dynamic_block(&self, trait_type_ident: &Ident) -> Block { pub fn dynamic_block(&self, trait_type_ident: &Ident) -> Block {
let Some(converted_this) = self.converted_this() else { let Some(converted_this) = self.converted_this() else {
return parse_quote! { return parse_quote! {
...@@ -579,13 +580,14 @@ impl TurboFn<'_> { ...@@ -579,13 +580,14 @@ impl TurboFn<'_> {
} }
} }
/// The block of the exposed function for a static dispatch call to the /// The block of the exposed function for a static dispatch call to the given native function.
/// given native function.
pub fn static_block(&self, native_function_ident: &Ident) -> Block { pub fn static_block(&self, native_function_ident: &Ident) -> Block {
let output = &self.output; let output = &self.output;
let inputs = self.inline_input_idents(); let inputs = self.inline_input_idents();
let assertions = self.get_assertions(); let assertions = self.get_assertions();
let mut block = if let Some(converted_this) = self.converted_this() { let mut block = if self.is_self_used
&& let Some(converted_this) = self.converted_this()
{
let persistence = self.persistence_with_this(); let persistence = self.persistence_with_this();
parse_quote! { parse_quote! {
{ {
......
...@@ -43,7 +43,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -43,7 +43,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
.unwrap_or_default(); .unwrap_or_default();
let is_self_used = args.operation.is_some() || is_self_used(&block); let is_self_used = args.operation.is_some() || is_self_used(&block);
let Some(turbo_fn) = TurboFn::new(&sig, DefinitionContext::NakedFn, args) else { let Some(turbo_fn) = TurboFn::new(&sig, DefinitionContext::NakedFn, args, is_self_used) else {
return quote! { return quote! {
// An error occurred while parsing the function signature. // An error occurred while parsing the function signature.
} }
...@@ -53,8 +53,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -53,8 +53,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
let ident = &sig.ident; let ident = &sig.ident;
let inline_function_ident = turbo_fn.inline_ident(); let inline_function_ident = turbo_fn.inline_ident();
let (inline_signature, inline_block) = let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(&block);
turbo_fn.inline_signature_and_block(&block, is_self_used);
let inline_attrs = filter_inline_attributes(&attrs[..]); let inline_attrs = filter_inline_attributes(&attrs[..]);
let function_path_string = ident.to_string(); let function_path_string = ident.to_string();
......
...@@ -79,3 +79,38 @@ fn contains_self_token(tok: &TokenTree) -> bool { ...@@ -79,3 +79,38 @@ fn contains_self_token(tok: &TokenTree) -> bool {
TokenTree::Punct(..) | TokenTree::Literal(..) => false, TokenTree::Punct(..) | TokenTree::Literal(..) => false,
} }
} }
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case::no_self_usage(r#"{ let x = 42; println!("hello"); }"#, false)]
#[case::simple_self_usage(r#"{ self.foo(); }"#, true)]
#[case::self_field_access(r#"{ let x = self.field; }"#, true)]
#[case::self_in_nested_block(r#"{ let x = 1; { self.method(); } }"#, true)]
#[case::self_in_impl_block_not_detected(
r#"{ impl Foo { fn bar(&self) { self.baz(); } } }"#,
false
)]
#[case::self_before_impl_block(
r#"{ self.foo(); impl Bar { fn baz(&self) { self.qux(); } } }"#,
true
)]
#[case::self_in_closure(r#"{ let f = || { self.method(); }; }"#, true)]
#[case::self_in_if_condition(r#"{ if self.check() { println!("true"); } }"#, true)]
#[case::self_in_match_arm(r#"{ match x { Some(_) => self.handle(), None => {}, } }"#, true)]
#[case::self_in_macro(r#"{ println!("{:?}", self); }"#, true)]
#[case::self_in_complex_macro(r#"{ format!("value: {}", self.field); }"#, true)]
#[case::no_self_with_similar_idents(r#"{ let myself = 42; let selfish = true; }"#, false)]
#[case::empty_block(r#"{}"#, false)]
#[case::self_in_return_statement(r#"{ return self.value; }"#, true)]
#[case::self_as_function_argument(r#"{ some_function(self); }"#, true)]
fn test_is_self_used(#[case] code: &str, #[case] expected: bool) {
let block: syn::Block = syn::parse_str(code).unwrap();
let result = is_self_used(&block);
assert_eq!(result, expected);
}
}
...@@ -73,14 +73,17 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -73,14 +73,17 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
let mut errors = Vec::new(); let mut errors = Vec::new();
for item in items.iter() { for item in items.iter() {
if let ImplItem::Fn(ImplItemFn { let ImplItem::Fn(ImplItemFn {
attrs, attrs,
vis, vis,
defaultness: _, defaultness: _,
sig, sig,
block, block,
}) = item }) = item
{ else {
continue;
};
let ident = &sig.ident; let ident = &sig.ident;
let (func_args, attrs) = split_function_attributes(attrs); let (func_args, attrs) = split_function_attributes(attrs);
let func_args = match func_args { let func_args = match func_args {
...@@ -99,17 +102,19 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -99,17 +102,19 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
}; };
let is_self_used = func_args.operation.is_some() || is_self_used(block); let is_self_used = func_args.operation.is_some() || is_self_used(block);
let Some(turbo_fn) = let Some(turbo_fn) = TurboFn::new(
TurboFn::new(sig, DefinitionContext::ValueInherentImpl, func_args) sig,
else { DefinitionContext::ValueInherentImpl,
func_args,
is_self_used,
) else {
return quote! { return quote! {
// An error occurred while parsing the function signature. // An error occurred while parsing the function signature.
}; };
}; };
let inline_function_ident = turbo_fn.inline_ident(); let inline_function_ident = turbo_fn.inline_ident();
let (inline_signature, inline_block) = let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block);
turbo_fn.inline_signature_and_block(block, is_self_used);
let inline_attrs = filter_inline_attributes(attrs.iter().copied()); let inline_attrs = filter_inline_attributes(attrs.iter().copied());
let function_path_string = format!("{ty}::{ident}", ty = ty.to_token_stream()); let function_path_string = format!("{ty}::{ident}", ty = ty.to_token_stream());
let native_fn = NativeFn { let native_fn = NativeFn {
...@@ -154,7 +159,6 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -154,7 +159,6 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
} }
}) })
} }
}
quote! { quote! {
impl #ty { impl #ty {
...@@ -207,11 +211,15 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -207,11 +211,15 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
continue; continue;
} }
}; };
// operations are not currently compatible with methods
let is_self_used = func_args.operation.is_some() || is_self_used(block); let is_self_used = func_args.operation.is_some() || is_self_used(block);
let Some(turbo_fn) = let Some(turbo_fn) = TurboFn::new(
TurboFn::new(sig, DefinitionContext::ValueTraitImpl, func_args) sig,
else { DefinitionContext::ValueTraitImpl,
func_args,
is_self_used,
) else {
return quote! { return quote! {
// An error occurred while parsing the function signature. // An error occurred while parsing the function signature.
}; };
...@@ -222,8 +230,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -222,8 +230,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
&format!("{ty_ident}_{trait_ident}_{ident}_inline"), &format!("{ty_ident}_{trait_ident}_{ident}_inline"),
ident.span(), ident.span(),
); );
let (inline_signature, inline_block) = let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block);
turbo_fn.inline_signature_and_block(block, is_self_used);
let inline_attrs = filter_inline_attributes(attrs.iter().copied()); let inline_attrs = filter_inline_attributes(attrs.iter().copied());
let native_fn = NativeFn { let native_fn = NativeFn {
// This global name breaks the pattern. It isn't clear if it is intentional // This global name breaks the pattern. It isn't clear if it is intentional
......
...@@ -176,10 +176,12 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -176,10 +176,12 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
.emit(); .emit();
} }
let is_self_used = default.as_ref().map(is_self_used).unwrap_or(false);
let Some(turbo_fn) = TurboFn::new( let Some(turbo_fn) = TurboFn::new(
sig, sig,
DefinitionContext::ValueTrait, DefinitionContext::ValueTrait,
FunctionArguments::default(), FunctionArguments::default(),
is_self_used,
) else { ) else {
return quote! { return quote! {
// An error occurred while parsing the function signature. // An error occurred while parsing the function signature.
...@@ -194,12 +196,10 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -194,12 +196,10 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
}); });
let default = if let Some(default) = default { let default = if let Some(default) = default {
let is_self_used = is_self_used(default);
let inline_function_ident = turbo_fn.inline_ident(); let inline_function_ident = turbo_fn.inline_ident();
let inline_extension_trait_ident = let inline_extension_trait_ident =
Ident::new(&format!("{trait_ident}_{ident}_inline"), ident.span()); Ident::new(&format!("{trait_ident}_{ident}_inline"), ident.span());
let (inline_signature, inline_block) = let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(default);
turbo_fn.inline_signature_and_block(default, is_self_used);
let inline_attrs = filter_inline_attributes(attrs.iter().copied()); let inline_attrs = filter_inline_attributes(attrs.iter().copied());
let function_path_string = format!("{trait_ident}::{ident}"); let function_path_string = format!("{trait_ident}::{ident}");
......
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