Skip to content

Commit 66d3b9a

Browse files
authored
refactor(turbo-tasks): Merge dynamic_call/native_call with dynamic_this_call/native_this_call (#75253)
The `_this` argument variants of these functions are 99% of the same, so this deduplicates them.
1 parent aee9309 commit 66d3b9a

File tree

5 files changed

+31
-151
lines changed

5 files changed

+31
-151
lines changed

turbopack/crates/turbo-tasks-macros/src/func.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,9 @@ impl TurboFn<'_> {
593593
let this = #converted_this;
594594
let persistence = #persistence;
595595
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
596-
turbo_tasks::dynamic_this_call(
596+
turbo_tasks::dynamic_call(
597597
*#native_function_id_ident,
598-
this,
598+
Some(this),
599599
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
600600
persistence,
601601
)
@@ -612,6 +612,7 @@ impl TurboFn<'_> {
612612
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
613613
turbo_tasks::dynamic_call(
614614
*#native_function_id_ident,
615+
None,
615616
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
616617
persistence,
617618
)

turbopack/crates/turbo-tasks-testing/src/lib.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,16 @@ impl TurboTasksCallApi for VcStorage {
9191
fn dynamic_call(
9292
&self,
9393
func: turbo_tasks::FunctionId,
94+
this: Option<RawVc>,
9495
arg: Box<dyn MagicAny>,
9596
_persistence: TaskPersistence,
9697
) -> RawVc {
97-
self.dynamic_call(func, None, arg)
98+
self.dynamic_call(func, this, arg)
9899
}
99-
100-
fn dynamic_this_call(
101-
&self,
102-
func: turbo_tasks::FunctionId,
103-
this_arg: RawVc,
104-
arg: Box<dyn MagicAny>,
105-
_persistence: TaskPersistence,
106-
) -> RawVc {
107-
self.dynamic_call(func, Some(this_arg), arg)
108-
}
109-
110100
fn native_call(
111101
&self,
112102
_func: turbo_tasks::FunctionId,
113-
_arg: Box<dyn MagicAny>,
114-
_persistence: TaskPersistence,
115-
) -> RawVc {
116-
unreachable!()
117-
}
118-
119-
fn this_call(
120-
&self,
121-
_func: turbo_tasks::FunctionId,
122-
_this: RawVc,
103+
_this: Option<RawVc>,
123104
_arg: Box<dyn MagicAny>,
124105
_persistence: TaskPersistence,
125106
) -> RawVc {

turbopack/crates/turbo-tasks/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ pub use join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt};
102102
pub use key_value_pair::KeyValuePair;
103103
pub use magic_any::MagicAny;
104104
pub use manager::{
105-
dynamic_call, dynamic_this_call, emit, mark_finished, mark_session_dependent, mark_stateful,
106-
prevent_gc, run_once, run_once_with_reason, spawn_blocking, spawn_thread, trait_call,
107-
turbo_tasks, turbo_tasks_scope, CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks,
108-
TurboTasksApi, TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused,
109-
UpdateInfo,
105+
dynamic_call, emit, mark_finished, mark_session_dependent, mark_stateful, prevent_gc, run_once,
106+
run_once_with_reason, spawn_blocking, spawn_thread, trait_call, turbo_tasks, turbo_tasks_scope,
107+
CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks, TurboTasksApi,
108+
TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, UpdateInfo,
110109
};
111110
pub use native_function::{FunctionMeta, NativeFunction};
112111
pub use output::OutputContent;

turbopack/crates/turbo-tasks/src/manager.rs

Lines changed: 19 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ pub trait TurboTasksCallApi: Sync + Send {
5858
fn dynamic_call(
5959
&self,
6060
func: FunctionId,
61-
arg: Box<dyn MagicAny>,
62-
persistence: TaskPersistence,
63-
) -> RawVc;
64-
/// Calls a native function with arguments. Resolves arguments when needed
65-
/// with a wrapper task.
66-
fn dynamic_this_call(
67-
&self,
68-
func: FunctionId,
69-
this: RawVc,
61+
this: Option<RawVc>,
7062
arg: Box<dyn MagicAny>,
7163
persistence: TaskPersistence,
7264
) -> RawVc;
@@ -75,15 +67,7 @@ pub trait TurboTasksCallApi: Sync + Send {
7567
fn native_call(
7668
&self,
7769
func: FunctionId,
78-
arg: Box<dyn MagicAny>,
79-
persistence: TaskPersistence,
80-
) -> RawVc;
81-
/// Call a native function with arguments.
82-
/// All inputs must be resolved.
83-
fn this_call(
84-
&self,
85-
func: FunctionId,
86-
this: RawVc,
70+
this: Option<RawVc>,
8771
arg: Box<dyn MagicAny>,
8872
persistence: TaskPersistence,
8973
) -> RawVc;
@@ -606,48 +590,12 @@ impl<B: Backend + 'static> TurboTasks<B> {
606590

607591
pub(crate) fn native_call(
608592
&self,
609-
func: FunctionId,
593+
fn_type: FunctionId,
594+
this: Option<RawVc>,
610595
arg: Box<dyn MagicAny>,
611596
persistence: TaskPersistence,
612597
) -> RawVc {
613-
let task_type = CachedTaskType {
614-
fn_type: func,
615-
this: None,
616-
arg,
617-
};
618-
match persistence {
619-
TaskPersistence::LocalCells => {
620-
todo!("bgw: local tasks");
621-
}
622-
TaskPersistence::Transient => {
623-
RawVc::TaskOutput(self.backend.get_or_create_transient_task(
624-
task_type,
625-
current_task("turbo_function calls"),
626-
self,
627-
))
628-
}
629-
TaskPersistence::Persistent => {
630-
RawVc::TaskOutput(self.backend.get_or_create_persistent_task(
631-
task_type,
632-
current_task("turbo_function calls"),
633-
self,
634-
))
635-
}
636-
}
637-
}
638-
639-
pub(crate) fn this_call(
640-
&self,
641-
func: FunctionId,
642-
this: RawVc,
643-
arg: Box<dyn MagicAny>,
644-
persistence: TaskPersistence,
645-
) -> RawVc {
646-
let task_type = CachedTaskType {
647-
fn_type: func,
648-
this: Some(this),
649-
arg,
650-
};
598+
let task_type = CachedTaskType { fn_type, this, arg };
651599
match persistence {
652600
TaskPersistence::LocalCells => {
653601
todo!("bgw: local tasks");
@@ -671,36 +619,17 @@ impl<B: Backend + 'static> TurboTasks<B> {
671619

672620
pub fn dynamic_call(
673621
&self,
674-
func: FunctionId,
622+
fn_type: FunctionId,
623+
this: Option<RawVc>,
675624
arg: Box<dyn MagicAny>,
676625
persistence: TaskPersistence,
677626
) -> RawVc {
678-
if registry::get_function(func).arg_meta.is_resolved(&*arg) {
679-
return self.native_call(func, arg, persistence);
680-
}
681-
let task_type = LocalTaskType::ResolveNative {
682-
fn_type: func,
683-
this: None,
684-
arg,
685-
};
686-
self.schedule_local_task(task_type, persistence)
687-
}
688-
689-
pub fn dynamic_this_call(
690-
&self,
691-
func: FunctionId,
692-
this: RawVc,
693-
arg: Box<dyn MagicAny>,
694-
persistence: TaskPersistence,
695-
) -> RawVc {
696-
if this.is_resolved() && registry::get_function(func).arg_meta.is_resolved(&*arg) {
697-
return self.this_call(func, this, arg, persistence);
627+
if this.is_none_or(|this| this.is_resolved())
628+
&& registry::get_function(fn_type).arg_meta.is_resolved(&*arg)
629+
{
630+
return self.native_call(fn_type, this, arg, persistence);
698631
}
699-
let task_type = LocalTaskType::ResolveNative {
700-
fn_type: func,
701-
this: Some(this),
702-
arg,
703-
};
632+
let task_type = LocalTaskType::ResolveNative { fn_type, this, arg };
704633
self.schedule_local_task(task_type, persistence)
705634
}
706635

@@ -718,7 +647,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
718647
if let RawVc::TaskCell(_, CellId { type_id, .. }) = this {
719648
match get_trait_method(trait_type, type_id, trait_fn_name) {
720649
Ok(native_fn) => {
721-
return self.dynamic_this_call(native_fn, this, arg, persistence);
650+
return self.dynamic_call(native_fn, Some(this), arg, persistence);
722651
}
723652
Err(name) => {
724653
trait_fn_name = name;
@@ -1240,36 +1169,20 @@ impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> {
12401169
fn dynamic_call(
12411170
&self,
12421171
func: FunctionId,
1172+
this: Option<RawVc>,
12431173
arg: Box<dyn MagicAny>,
12441174
persistence: TaskPersistence,
12451175
) -> RawVc {
1246-
self.dynamic_call(func, arg, persistence)
1247-
}
1248-
fn dynamic_this_call(
1249-
&self,
1250-
func: FunctionId,
1251-
this: RawVc,
1252-
arg: Box<dyn MagicAny>,
1253-
persistence: TaskPersistence,
1254-
) -> RawVc {
1255-
self.dynamic_this_call(func, this, arg, persistence)
1176+
self.dynamic_call(func, this, arg, persistence)
12561177
}
12571178
fn native_call(
12581179
&self,
12591180
func: FunctionId,
1181+
this: Option<RawVc>,
12601182
arg: Box<dyn MagicAny>,
12611183
persistence: TaskPersistence,
12621184
) -> RawVc {
1263-
self.native_call(func, arg, persistence)
1264-
}
1265-
fn this_call(
1266-
&self,
1267-
func: FunctionId,
1268-
this: RawVc,
1269-
arg: Box<dyn MagicAny>,
1270-
persistence: TaskPersistence,
1271-
) -> RawVc {
1272-
self.this_call(func, this, arg, persistence)
1185+
self.native_call(func, this, arg, persistence)
12731186
}
12741187
fn trait_call(
12751188
&self,
@@ -1723,21 +1636,11 @@ pub async fn run_once_with_reason<T: Send + 'static>(
17231636
/// Calls [`TurboTasks::dynamic_call`] for the current turbo tasks instance.
17241637
pub fn dynamic_call(
17251638
func: FunctionId,
1639+
this: Option<RawVc>,
17261640
arg: Box<dyn MagicAny>,
17271641
persistence: TaskPersistence,
17281642
) -> RawVc {
1729-
with_turbo_tasks(|tt| tt.dynamic_call(func, arg, persistence))
1730-
}
1731-
1732-
/// Calls [`TurboTasks::dynamic_this_call`] for the current turbo tasks
1733-
/// instance.
1734-
pub fn dynamic_this_call(
1735-
func: FunctionId,
1736-
this: RawVc,
1737-
arg: Box<dyn MagicAny>,
1738-
persistence: TaskPersistence,
1739-
) -> RawVc {
1740-
with_turbo_tasks(|tt| tt.dynamic_this_call(func, this, arg, persistence))
1643+
with_turbo_tasks(|tt| tt.dynamic_call(func, this, arg, persistence))
17411644
}
17421645

17431646
/// Calls [`TurboTasks::trait_call`] for the current turbo tasks instance.

turbopack/crates/turbo-tasks/src/task/local_task.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ impl LocalTaskType {
156156
*this = this.resolve().await?;
157157
}
158158
let arg = registry::get_function(fn_id).arg_meta.resolve(arg).await?;
159-
Ok(if let Some(this) = this {
160-
turbo_tasks.this_call(fn_id, this, arg, persistence)
161-
} else {
162-
turbo_tasks.native_call(fn_id, arg, persistence)
163-
})
159+
Ok(turbo_tasks.native_call(fn_id, this, arg, persistence))
164160
}
165161

166162
pub async fn run_resolve_trait<B: Backend + 'static>(
@@ -179,7 +175,7 @@ impl LocalTaskType {
179175
.arg_meta
180176
.resolve(arg)
181177
.await?;
182-
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, persistence))
178+
Ok(turbo_tasks.dynamic_call(native_fn, Some(this), arg, persistence))
183179
}
184180

185181
fn resolve_trait_method_from_value(

0 commit comments

Comments
 (0)