Skip to content

Commit

Permalink
refactor(turbo-tasks): Merge dynamic_call/native_call with dynamic_th…
Browse files Browse the repository at this point in the history
…is_call/native_this_call
  • Loading branch information
bgw committed Jan 24, 2025
1 parent 0f70ca4 commit 8439258
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 151 deletions.
5 changes: 3 additions & 2 deletions turbopack/crates/turbo-tasks-macros/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn turbo_tasks::MagicAny>,
persistence,
)
Expand All @@ -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<dyn turbo_tasks::MagicAny>,
persistence,
)
Expand Down
25 changes: 3 additions & 22 deletions turbopack/crates/turbo-tasks-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,16 @@ impl TurboTasksCallApi for VcStorage {
fn dynamic_call(
&self,
func: turbo_tasks::FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
_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<dyn MagicAny>,
_persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, Some(this_arg), arg)
}

fn native_call(
&self,
_func: turbo_tasks::FunctionId,
_arg: Box<dyn MagicAny>,
_persistence: TaskPersistence,
) -> RawVc {
unreachable!()
}

fn this_call(
&self,
_func: turbo_tasks::FunctionId,
_this: RawVc,
_this: Option<RawVc>,
_arg: Box<dyn MagicAny>,
_persistence: TaskPersistence,
) -> RawVc {
Expand Down
9 changes: 4 additions & 5 deletions turbopack/crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
135 changes: 19 additions & 116 deletions turbopack/crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,7 @@ pub trait TurboTasksCallApi: Sync + Send {
fn dynamic_call(
&self,
func: FunctionId,
arg: Box<dyn MagicAny>,
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<RawVc>,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc;
Expand All @@ -75,15 +67,7 @@ pub trait TurboTasksCallApi: Sync + Send {
fn native_call(
&self,
func: FunctionId,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc;
/// Call a native function with arguments.
/// All inputs must be resolved.
fn this_call(
&self,
func: FunctionId,
this: RawVc,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc;
Expand Down Expand Up @@ -606,48 +590,12 @@ impl<B: Backend + 'static> TurboTasks<B> {

pub(crate) fn native_call(
&self,
func: FunctionId,
fn_type: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
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<dyn MagicAny>,
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");
Expand All @@ -671,36 +619,17 @@ impl<B: Backend + 'static> TurboTasks<B> {

pub fn dynamic_call(
&self,
func: FunctionId,
fn_type: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
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<dyn MagicAny>,
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)
}

Expand All @@ -718,7 +647,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
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;
Expand Down Expand Up @@ -1240,36 +1169,20 @@ impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> {
fn dynamic_call(
&self,
func: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, arg, persistence)
}
fn dynamic_this_call(
&self,
func: FunctionId,
this: RawVc,
arg: Box<dyn MagicAny>,
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<RawVc>,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
self.native_call(func, arg, persistence)
}
fn this_call(
&self,
func: FunctionId,
this: RawVc,
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
self.this_call(func, this, arg, persistence)
self.native_call(func, this, arg, persistence)
}
fn trait_call(
&self,
Expand Down Expand Up @@ -1723,21 +1636,11 @@ pub async fn run_once_with_reason<T: Send + 'static>(
/// Calls [`TurboTasks::dynamic_call`] for the current turbo tasks instance.
pub fn dynamic_call(
func: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
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<dyn MagicAny>,
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.
Expand Down
8 changes: 2 additions & 6 deletions turbopack/crates/turbo-tasks/src/task/local_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B: Backend + 'static>(
Expand All @@ -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(
Expand Down

0 comments on commit 8439258

Please sign in to comment.