From 84392580c28765c7c8e1c15f532d81b919449453 Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Thu, 23 Jan 2025 14:48:55 -0800 Subject: [PATCH] refactor(turbo-tasks): Merge dynamic_call/native_call with dynamic_this_call/native_this_call --- .../crates/turbo-tasks-macros/src/func.rs | 5 +- .../crates/turbo-tasks-testing/src/lib.rs | 25 +--- turbopack/crates/turbo-tasks/src/lib.rs | 9 +- turbopack/crates/turbo-tasks/src/manager.rs | 135 +++--------------- .../crates/turbo-tasks/src/task/local_task.rs | 8 +- 5 files changed, 31 insertions(+), 151 deletions(-) diff --git a/turbopack/crates/turbo-tasks-macros/src/func.rs b/turbopack/crates/turbo-tasks-macros/src/func.rs index 3578749092d3c9..52d0e24f3d44d5 100644 --- a/turbopack/crates/turbo-tasks-macros/src/func.rs +++ b/turbopack/crates/turbo-tasks-macros/src/func.rs @@ -593,9 +593,9 @@ impl TurboFn<'_> { let this = #converted_this; let persistence = #persistence; <#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc( - turbo_tasks::dynamic_this_call( + turbo_tasks::dynamic_call( *#native_function_id_ident, - this, + Some(this), inputs as std::boxed::Box, persistence, ) @@ -612,6 +612,7 @@ impl TurboFn<'_> { <#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc( turbo_tasks::dynamic_call( *#native_function_id_ident, + None, inputs as std::boxed::Box, persistence, ) diff --git a/turbopack/crates/turbo-tasks-testing/src/lib.rs b/turbopack/crates/turbo-tasks-testing/src/lib.rs index 1ea96cf8f63292..56cb55b068c2c2 100644 --- a/turbopack/crates/turbo-tasks-testing/src/lib.rs +++ b/turbopack/crates/turbo-tasks-testing/src/lib.rs @@ -91,35 +91,16 @@ impl TurboTasksCallApi for VcStorage { fn dynamic_call( &self, func: turbo_tasks::FunctionId, + this: Option, arg: Box, _persistence: TaskPersistence, ) -> RawVc { - self.dynamic_call(func, None, arg) + self.dynamic_call(func, this, arg) } - - fn dynamic_this_call( - &self, - func: turbo_tasks::FunctionId, - this_arg: RawVc, - arg: Box, - _persistence: TaskPersistence, - ) -> RawVc { - self.dynamic_call(func, Some(this_arg), arg) - } - fn native_call( &self, _func: turbo_tasks::FunctionId, - _arg: Box, - _persistence: TaskPersistence, - ) -> RawVc { - unreachable!() - } - - fn this_call( - &self, - _func: turbo_tasks::FunctionId, - _this: RawVc, + _this: Option, _arg: Box, _persistence: TaskPersistence, ) -> RawVc { diff --git a/turbopack/crates/turbo-tasks/src/lib.rs b/turbopack/crates/turbo-tasks/src/lib.rs index ccad66dc437cdc..8bcc910a6544ec 100644 --- a/turbopack/crates/turbo-tasks/src/lib.rs +++ b/turbopack/crates/turbo-tasks/src/lib.rs @@ -102,11 +102,10 @@ pub use join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt}; pub use key_value_pair::KeyValuePair; pub use magic_any::MagicAny; pub use manager::{ - dynamic_call, dynamic_this_call, emit, mark_finished, mark_session_dependent, mark_stateful, - prevent_gc, run_once, run_once_with_reason, spawn_blocking, spawn_thread, trait_call, - turbo_tasks, turbo_tasks_scope, CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks, - TurboTasksApi, TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, - UpdateInfo, + dynamic_call, emit, mark_finished, mark_session_dependent, mark_stateful, prevent_gc, run_once, + run_once_with_reason, spawn_blocking, spawn_thread, trait_call, turbo_tasks, turbo_tasks_scope, + CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks, TurboTasksApi, + TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, UpdateInfo, }; pub use native_function::{FunctionMeta, NativeFunction}; pub use output::OutputContent; diff --git a/turbopack/crates/turbo-tasks/src/manager.rs b/turbopack/crates/turbo-tasks/src/manager.rs index 161462b787d468..039fd8c04aa24c 100644 --- a/turbopack/crates/turbo-tasks/src/manager.rs +++ b/turbopack/crates/turbo-tasks/src/manager.rs @@ -58,15 +58,7 @@ pub trait TurboTasksCallApi: Sync + Send { fn dynamic_call( &self, func: FunctionId, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc; - /// Calls a native function with arguments. Resolves arguments when needed - /// with a wrapper task. - fn dynamic_this_call( - &self, - func: FunctionId, - this: RawVc, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc; @@ -75,15 +67,7 @@ pub trait TurboTasksCallApi: Sync + Send { fn native_call( &self, func: FunctionId, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc; - /// Call a native function with arguments. - /// All inputs must be resolved. - fn this_call( - &self, - func: FunctionId, - this: RawVc, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc; @@ -606,48 +590,12 @@ impl TurboTasks { pub(crate) fn native_call( &self, - func: FunctionId, + fn_type: FunctionId, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc { - let task_type = CachedTaskType { - fn_type: func, - this: None, - arg, - }; - match persistence { - TaskPersistence::LocalCells => { - todo!("bgw: local tasks"); - } - TaskPersistence::Transient => { - RawVc::TaskOutput(self.backend.get_or_create_transient_task( - task_type, - current_task("turbo_function calls"), - self, - )) - } - TaskPersistence::Persistent => { - RawVc::TaskOutput(self.backend.get_or_create_persistent_task( - task_type, - current_task("turbo_function calls"), - self, - )) - } - } - } - - pub(crate) fn this_call( - &self, - func: FunctionId, - this: RawVc, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc { - let task_type = CachedTaskType { - fn_type: func, - this: Some(this), - arg, - }; + let task_type = CachedTaskType { fn_type, this, arg }; match persistence { TaskPersistence::LocalCells => { todo!("bgw: local tasks"); @@ -671,36 +619,17 @@ impl TurboTasks { pub fn dynamic_call( &self, - func: FunctionId, + fn_type: FunctionId, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc { - if registry::get_function(func).arg_meta.is_resolved(&*arg) { - return self.native_call(func, arg, persistence); - } - let task_type = LocalTaskType::ResolveNative { - fn_type: func, - this: None, - arg, - }; - self.schedule_local_task(task_type, persistence) - } - - pub fn dynamic_this_call( - &self, - func: FunctionId, - this: RawVc, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc { - if this.is_resolved() && registry::get_function(func).arg_meta.is_resolved(&*arg) { - return self.this_call(func, this, arg, persistence); + if this.is_none_or(|this| this.is_resolved()) + && registry::get_function(fn_type).arg_meta.is_resolved(&*arg) + { + return self.native_call(fn_type, this, arg, persistence); } - let task_type = LocalTaskType::ResolveNative { - fn_type: func, - this: Some(this), - arg, - }; + let task_type = LocalTaskType::ResolveNative { fn_type, this, arg }; self.schedule_local_task(task_type, persistence) } @@ -718,7 +647,7 @@ impl TurboTasks { if let RawVc::TaskCell(_, CellId { type_id, .. }) = this { match get_trait_method(trait_type, type_id, trait_fn_name) { Ok(native_fn) => { - return self.dynamic_this_call(native_fn, this, arg, persistence); + return self.dynamic_call(native_fn, Some(this), arg, persistence); } Err(name) => { trait_fn_name = name; @@ -1240,36 +1169,20 @@ impl TurboTasksCallApi for TurboTasks { fn dynamic_call( &self, func: FunctionId, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc { - self.dynamic_call(func, arg, persistence) - } - fn dynamic_this_call( - &self, - func: FunctionId, - this: RawVc, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc { - self.dynamic_this_call(func, this, arg, persistence) + self.dynamic_call(func, this, arg, persistence) } fn native_call( &self, func: FunctionId, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc { - self.native_call(func, arg, persistence) - } - fn this_call( - &self, - func: FunctionId, - this: RawVc, - arg: Box, - persistence: TaskPersistence, - ) -> RawVc { - self.this_call(func, this, arg, persistence) + self.native_call(func, this, arg, persistence) } fn trait_call( &self, @@ -1723,21 +1636,11 @@ pub async fn run_once_with_reason( /// Calls [`TurboTasks::dynamic_call`] for the current turbo tasks instance. pub fn dynamic_call( func: FunctionId, + this: Option, arg: Box, persistence: TaskPersistence, ) -> RawVc { - with_turbo_tasks(|tt| tt.dynamic_call(func, arg, persistence)) -} - -/// Calls [`TurboTasks::dynamic_this_call`] for the current turbo tasks -/// instance. -pub fn dynamic_this_call( - func: FunctionId, - this: RawVc, - arg: Box, - persistence: TaskPersistence, -) -> RawVc { - with_turbo_tasks(|tt| tt.dynamic_this_call(func, this, arg, persistence)) + with_turbo_tasks(|tt| tt.dynamic_call(func, this, arg, persistence)) } /// Calls [`TurboTasks::trait_call`] for the current turbo tasks instance. diff --git a/turbopack/crates/turbo-tasks/src/task/local_task.rs b/turbopack/crates/turbo-tasks/src/task/local_task.rs index 87c1af1c048fb0..74d34291b2ce09 100644 --- a/turbopack/crates/turbo-tasks/src/task/local_task.rs +++ b/turbopack/crates/turbo-tasks/src/task/local_task.rs @@ -156,11 +156,7 @@ impl LocalTaskType { *this = this.resolve().await?; } let arg = registry::get_function(fn_id).arg_meta.resolve(arg).await?; - Ok(if let Some(this) = this { - turbo_tasks.this_call(fn_id, this, arg, persistence) - } else { - turbo_tasks.native_call(fn_id, arg, persistence) - }) + Ok(turbo_tasks.native_call(fn_id, this, arg, persistence)) } pub async fn run_resolve_trait( @@ -179,7 +175,7 @@ impl LocalTaskType { .arg_meta .resolve(arg) .await?; - Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, persistence)) + Ok(turbo_tasks.dynamic_call(native_fn, Some(this), arg, persistence)) } fn resolve_trait_method_from_value(