Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor optimizations to the codegen of TaskFnInputFunction #8304

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 69 additions & 50 deletions crates/turbo-tasks/src/task/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,44 @@ macro_rules! task_inputs_impl {
}
}

macro_rules! as_concrete_task_input {
( $arg:ident ) => {
ConcreteTaskInput
};
}

macro_rules! task_fn_impl {
( $async_fn_trait:ident , $( $arg:ident )* ) => {
( $helper_module:ident $async_fn_trait:ident $arg_len:literal $( $arg:ident )* ) => {
mod $helper_module {
use super::*;

// this is a non-generic helper method to allow code-sharing across monomorphized
// instances of this function
pub fn get_args(
inputs: &[ConcreteTaskInput],
) -> Result<($(&as_concrete_task_input!($arg),)*)> {
get_args_iter(inputs.iter())
}

pub fn get_method_args(
inputs: &[ConcreteTaskInput],
) -> Result<(&ConcreteTaskInput, ($(&as_concrete_task_input!($arg),)*))> {
let mut iter = inputs.iter();
let recv = iter.next().context("task is missing receiver")?;
Ok((recv, get_args_iter(iter)?))
}

fn get_args_iter(
mut iter: std::slice::Iter<'_, ConcreteTaskInput>,
) -> Result<($(&as_concrete_task_input!($arg),)*)> {
let args = ($(next_arg(&mut iter, stringify!($arg))?,)*);
if iter.next().is_some() {
bail!("task was called with too many arguments");
}
Ok(args)
}
}

impl<F, Output, $($arg,)*> TaskFnInputFunction<FunctionMode, ($($arg,)*)> for F
where
$($arg: TaskInput + 'static,)*
Expand All @@ -119,17 +155,9 @@ macro_rules! task_fn_impl {
{
#[allow(non_snake_case)]
fn functor(&self, inputs: &[ConcreteTaskInput]) -> Result<NativeTaskFn> {
let task_fn = self.clone();
let mut iter = inputs.iter();

$(
let $arg = iter.next().context(format!("task is missing argument {}", stringify!($arg)))?;
)*

if iter.next().is_some() {
bail!("task was called with too many arguments");
}
let ($($arg,)*) = $helper_module::get_args(inputs)?;

let task_fn = self.clone();
$(
let $arg = $arg::try_from_concrete($arg)?;
)*
Expand All @@ -156,17 +184,9 @@ macro_rules! task_fn_impl {
{
#[allow(non_snake_case)]
fn functor(&self, inputs: &[ConcreteTaskInput]) -> Result<NativeTaskFn> {
let task_fn = self.clone();
let mut iter = inputs.iter();

$(
let $arg = iter.next().context(format!("task is missing argument {}", stringify!($arg)))?;
)*

if iter.next().is_some() {
bail!("task was called with too many arguments");
}
let ($($arg,)*) = $helper_module::get_args(inputs)?;

let task_fn = self.clone();
$(
let $arg = $arg::try_from_concrete($arg)?;
)*
Expand All @@ -193,18 +213,9 @@ macro_rules! task_fn_impl {
{
#[allow(non_snake_case)]
fn functor(&self, inputs: &[ConcreteTaskInput]) -> Result<NativeTaskFn> {
let task_fn = self.clone();
let mut iter = inputs.iter();

let recv = iter.next().context("task is missing receiver")?;
$(
let $arg = iter.next().context(format!("task is missing argument {}", stringify!($arg)))?;
)*

if iter.next().is_some() {
bail!("task was called with too many arguments");
}
let (recv, ($($arg,)*)) = $helper_module::get_method_args(inputs)?;

let task_fn = self.clone();
let recv = Vc::<Recv>::try_from_concrete(recv)?;
$(
let $arg = $arg::try_from_concrete($arg)?;
Expand Down Expand Up @@ -254,7 +265,7 @@ macro_rules! task_fn_impl {

let recv = iter.next().context("task is missing receiver")?;
$(
let $arg = iter.next().context(format!("task is missing argument {}", stringify!($arg)))?;
let $arg = next_arg(&mut iter, stringify!($arg))?;
)*

if iter.next().is_some() {
Expand Down Expand Up @@ -284,23 +295,23 @@ macro_rules! task_fn_impl {
};
}

task_fn_impl! { AsyncFn0, }
task_fn_impl! { AsyncFn1, A1 }
task_fn_impl! { AsyncFn2, A1 A2 }
task_fn_impl! { AsyncFn3, A1 A2 A3 }
task_fn_impl! { AsyncFn4, A1 A2 A3 A4 }
task_fn_impl! { AsyncFn5, A1 A2 A3 A4 A5 }
task_fn_impl! { AsyncFn6, A1 A2 A3 A4 A5 A6 }
task_fn_impl! { AsyncFn7, A1 A2 A3 A4 A5 A6 A7 }
task_fn_impl! { AsyncFn8, A1 A2 A3 A4 A5 A6 A7 A8 }
task_fn_impl! { AsyncFn9, A1 A2 A3 A4 A5 A6 A7 A8 A9 }
task_fn_impl! { AsyncFn10, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 }
task_fn_impl! { AsyncFn11, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 }
task_fn_impl! { AsyncFn12, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 }
task_fn_impl! { AsyncFn13, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 }
task_fn_impl! { AsyncFn14, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 }
task_fn_impl! { AsyncFn15, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 }
task_fn_impl! { AsyncFn16, A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 }
task_fn_impl! { async_fn_0 AsyncFn0 0 }
task_fn_impl! { async_fn_1 AsyncFn1 1 A1 }
task_fn_impl! { async_fn_2 AsyncFn2 2 A1 A2 }
task_fn_impl! { async_fn_3 AsyncFn3 3 A1 A2 A3 }
task_fn_impl! { async_fn_4 AsyncFn4 4 A1 A2 A3 A4 }
task_fn_impl! { async_fn_5 AsyncFn5 5 A1 A2 A3 A4 A5 }
task_fn_impl! { async_fn_6 AsyncFn6 6 A1 A2 A3 A4 A5 A6 }
task_fn_impl! { async_fn_7 AsyncFn7 7 A1 A2 A3 A4 A5 A6 A7 }
task_fn_impl! { async_fn_8 AsyncFn8 8 A1 A2 A3 A4 A5 A6 A7 A8 }
task_fn_impl! { async_fn_9 AsyncFn9 9 A1 A2 A3 A4 A5 A6 A7 A8 A9 }
task_fn_impl! { async_fn_10 AsyncFn10 10 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 }
task_fn_impl! { async_fn_11 AsyncFn11 11 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 }
task_fn_impl! { async_fn_12 AsyncFn12 12 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 }
task_fn_impl! { async_fn_13 AsyncFn13 13 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 }
task_fn_impl! { async_fn_14 AsyncFn14 14 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 }
task_fn_impl! { async_fn_15 AsyncFn15 15 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 }
task_fn_impl! { async_fn_16 AsyncFn16 16 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 }

// There needs to be one more implementation than task_fn_impl to account for
// the receiver.
Expand All @@ -323,6 +334,14 @@ task_inputs_impl! { A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 }
task_inputs_impl! { A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 }
task_inputs_impl! { A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 }

fn next_arg<'a>(
iter: &mut std::slice::Iter<'a, ConcreteTaskInput>,
arg_name: &'static str,
) -> Result<&'a ConcreteTaskInput> {
iter.next()
.with_context(move || format!("task is missing argument {}", arg_name))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading