Skip to content

Commit 02ecb50

Browse files
committed
Add TaskPersistence enum for task creation functions
1 parent d714bb3 commit 02ecb50

File tree

8 files changed

+262
-147
lines changed

8 files changed

+262
-147
lines changed

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

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashSet;
22

33
use proc_macro2::{Ident, Span, TokenStream};
4-
use quote::{quote, quote_spanned};
4+
use quote::{quote, quote_spanned, ToTokens};
55
use syn::{
66
parenthesized,
77
parse::{Parse, ParseStream},
@@ -24,6 +24,8 @@ pub struct TurboFn {
2424
inputs: Vec<Input>,
2525
/// Should we check that the return type contains a `ResolvedValue`?
2626
resolved: Option<Span>,
27+
/// Should this function use `TaskPersistence::LocalCells`?
28+
local_cells: bool,
2729
}
2830

2931
#[derive(Debug)]
@@ -257,6 +259,7 @@ impl TurboFn {
257259
this,
258260
inputs,
259261
resolved: args.resolved,
262+
local_cells: args.local_cells.is_some(),
260263
})
261264
}
262265

@@ -301,17 +304,26 @@ impl TurboFn {
301304
}
302305
}
303306

304-
fn inputs(&self) -> Vec<&Ident> {
305-
self.inputs
306-
.iter()
307-
.map(|Input { ident, .. }| ident)
308-
.collect()
307+
fn input_idents(&self) -> impl Iterator<Item = &Ident> {
308+
self.inputs.iter().map(|Input { ident, .. }| ident)
309309
}
310310

311311
pub fn input_types(&self) -> Vec<&Type> {
312312
self.inputs.iter().map(|Input { ty, .. }| ty).collect()
313313
}
314314

315+
pub fn persistence(&self) -> impl ToTokens {
316+
if self.local_cells {
317+
quote! {
318+
turbo_tasks::TaskPersistence::LocalCells
319+
}
320+
} else {
321+
quote! {
322+
turbo_tasks::macro_helpers::get_non_local_persistence_from_inputs(&*inputs)
323+
}
324+
}
325+
}
326+
315327
fn converted_this(&self) -> Option<Expr> {
316328
self.this.as_ref().map(|Input { ty: _, ident }| {
317329
parse_quote! {
@@ -344,31 +356,33 @@ impl TurboFn {
344356
/// The block of the exposed function for a dynamic dispatch call to the
345357
/// given trait.
346358
pub fn dynamic_block(&self, trait_type_id_ident: &Ident) -> Block {
359+
let Some(converted_this) = self.converted_this() else {
360+
return parse_quote! {
361+
{
362+
unimplemented!("trait methods without self are not yet supported")
363+
}
364+
};
365+
};
366+
347367
let ident = &self.ident;
348368
let output = &self.output;
349369
let assertions = self.get_assertions();
350-
if let Some(converted_this) = self.converted_this() {
351-
let inputs = self.inputs();
352-
parse_quote! {
353-
{
354-
#assertions
355-
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
356-
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
357-
turbo_tasks::trait_call(
358-
*#trait_type_id_ident,
359-
std::borrow::Cow::Borrowed(stringify!(#ident)),
360-
#converted_this,
361-
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
362-
turbo_tasks_transient,
363-
)
370+
let inputs = self.input_idents();
371+
let persistence = self.persistence();
372+
parse_quote! {
373+
{
374+
#assertions
375+
let inputs = std::boxed::Box::new((#(#inputs,)*));
376+
let persistence = #persistence;
377+
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
378+
turbo_tasks::trait_call(
379+
*#trait_type_id_ident,
380+
std::borrow::Cow::Borrowed(stringify!(#ident)),
381+
#converted_this,
382+
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
383+
persistence,
364384
)
365-
}
366-
}
367-
} else {
368-
parse_quote! {
369-
{
370-
unimplemented!("trait methods without self are not yet supported")
371-
}
385+
)
372386
}
373387
}
374388
}
@@ -377,19 +391,21 @@ impl TurboFn {
377391
/// given native function.
378392
pub fn static_block(&self, native_function_id_ident: &Ident) -> Block {
379393
let output = &self.output;
380-
let inputs = self.inputs();
394+
let inputs = self.input_idents();
395+
let persistence = self.persistence();
381396
let assertions = self.get_assertions();
382397
if let Some(converted_this) = self.converted_this() {
383398
parse_quote! {
384399
{
385400
#assertions
386-
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
401+
let inputs = std::boxed::Box::new((#(#inputs,)*));
402+
let persistence = #persistence;
387403
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
388404
turbo_tasks::dynamic_this_call(
389405
*#native_function_id_ident,
390406
#converted_this,
391-
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
392-
turbo_tasks_transient
407+
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
408+
persistence,
393409
)
394410
)
395411
}
@@ -398,12 +414,13 @@ impl TurboFn {
398414
parse_quote! {
399415
{
400416
#assertions
401-
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
417+
let inputs = std::boxed::Box::new((#(#inputs,)*));
418+
let persistence = #persistence;
402419
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
403420
turbo_tasks::dynamic_call(
404421
*#native_function_id_ident,
405-
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
406-
turbo_tasks_transient,
422+
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
423+
persistence,
407424
)
408425
)
409426
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ impl Task {
810810
native_fn_id,
811811
*this,
812812
&**arg,
813-
self.id.is_transient(),
813+
self.id.persistence(),
814814
turbo_tasks,
815815
));
816816
drop(entered);
@@ -833,7 +833,7 @@ impl Task {
833833
name,
834834
*this,
835835
&**arg,
836-
self.id.is_transient(),
836+
self.id.persistence(),
837837
turbo_tasks,
838838
));
839839
drop(entered);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use turbo_tasks::{
2020
registry,
2121
test_helpers::with_turbo_tasks_for_testing,
2222
util::{SharedError, StaticOrArc},
23-
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, TaskId, TraitTypeId, TurboTasksApi,
24-
TurboTasksCallApi,
23+
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, TaskId, TaskPersistence, TraitTypeId,
24+
TurboTasksApi, TurboTasksCallApi,
2525
};
2626

2727
pub use crate::run::{run, run_without_cache_check, Registration};
@@ -92,7 +92,7 @@ impl TurboTasksCallApi for VcStorage {
9292
&self,
9393
func: turbo_tasks::FunctionId,
9494
arg: Box<dyn MagicAny>,
95-
_is_transient: bool,
95+
_persistence: TaskPersistence,
9696
) -> RawVc {
9797
self.dynamic_call(func, None, arg)
9898
}
@@ -102,7 +102,7 @@ impl TurboTasksCallApi for VcStorage {
102102
func: turbo_tasks::FunctionId,
103103
this_arg: RawVc,
104104
arg: Box<dyn MagicAny>,
105-
_is_transient: bool,
105+
_persistence: TaskPersistence,
106106
) -> RawVc {
107107
self.dynamic_call(func, Some(this_arg), arg)
108108
}
@@ -111,7 +111,7 @@ impl TurboTasksCallApi for VcStorage {
111111
&self,
112112
_func: turbo_tasks::FunctionId,
113113
_arg: Box<dyn MagicAny>,
114-
_is_transient: bool,
114+
_persistence: TaskPersistence,
115115
) -> RawVc {
116116
unreachable!()
117117
}
@@ -121,7 +121,7 @@ impl TurboTasksCallApi for VcStorage {
121121
_func: turbo_tasks::FunctionId,
122122
_this: RawVc,
123123
_arg: Box<dyn MagicAny>,
124-
_is_transient: bool,
124+
_persistence: TaskPersistence,
125125
) -> RawVc {
126126
unreachable!()
127127
}
@@ -132,7 +132,7 @@ impl TurboTasksCallApi for VcStorage {
132132
_trait_fn_name: Cow<'static, str>,
133133
_this: RawVc,
134134
_arg: Box<dyn MagicAny>,
135-
_is_transient: bool,
135+
_persistence: TaskPersistence,
136136
) -> RawVc {
137137
unreachable!()
138138
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::{
2323
task::shared_reference::TypedSharedReference,
2424
trait_helpers::{get_trait_method, has_trait, traits},
2525
triomphe_utils::unchecked_sidecast_triomphe_arc,
26-
FunctionId, RawVc, ReadRef, SharedReference, TaskId, TaskIdSet, TraitRef, TraitTypeId,
27-
ValueTypeId, VcRead, VcValueTrait, VcValueType,
26+
FunctionId, RawVc, ReadRef, SharedReference, TaskId, TaskIdSet, TaskPersistence, TraitRef,
27+
TraitTypeId, ValueTypeId, VcRead, VcValueTrait, VcValueType,
2828
};
2929

3030
type TransientTaskRoot =
@@ -608,17 +608,17 @@ impl CachedTaskType {
608608
fn_id: FunctionId,
609609
mut this: Option<RawVc>,
610610
arg: &dyn MagicAny,
611-
is_transient: bool,
611+
persistence: TaskPersistence,
612612
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
613613
) -> Result<RawVc> {
614614
if let Some(this) = this.as_mut() {
615615
*this = this.resolve().await?;
616616
}
617617
let arg = registry::get_function(fn_id).arg_meta.resolve(arg).await?;
618618
Ok(if let Some(this) = this {
619-
turbo_tasks.this_call(fn_id, this, arg, is_transient)
619+
turbo_tasks.this_call(fn_id, this, arg, persistence)
620620
} else {
621-
turbo_tasks.native_call(fn_id, arg, is_transient)
621+
turbo_tasks.native_call(fn_id, arg, persistence)
622622
})
623623
}
624624

@@ -636,7 +636,7 @@ impl CachedTaskType {
636636
name: Cow<'static, str>,
637637
this: RawVc,
638638
arg: &dyn MagicAny,
639-
is_transient: bool,
639+
persistence: TaskPersistence,
640640
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
641641
) -> Result<RawVc> {
642642
let this = this.resolve().await?;
@@ -647,7 +647,7 @@ impl CachedTaskType {
647647
.arg_meta
648648
.resolve(arg)
649649
.await?;
650-
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, is_transient))
650+
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, persistence))
651651
}
652652

653653
/// Shared helper used by [`Self::resolve_trait_method`] and

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use serde::{de::Visitor, Deserialize, Serialize};
99

10-
use crate::registry;
10+
use crate::{registry, TaskPersistence};
1111

1212
macro_rules! define_id {
1313
($name:ident : $primitive:ty $(,derive($($derive:ty),*))?) => {
@@ -84,6 +84,14 @@ impl TaskId {
8484
pub fn is_transient(&self) -> bool {
8585
**self & TRANSIENT_TASK_BIT != 0
8686
}
87+
pub fn persistence(&self) -> TaskPersistence {
88+
// tasks with `TaskPersistence::LocalCells` have no `TaskId`, so we can ignore that case
89+
if self.is_transient() {
90+
TaskPersistence::Transient
91+
} else {
92+
TaskPersistence::Persistent
93+
}
94+
}
8795
}
8896

8997
macro_rules! make_serializable {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ pub use magic_any::MagicAny;
9090
pub use manager::{
9191
dynamic_call, dynamic_this_call, emit, get_invalidator, mark_finished, mark_stateful,
9292
prevent_gc, run_once, run_once_with_reason, spawn_blocking, spawn_thread, trait_call,
93-
turbo_tasks, CurrentCellRef, Invalidator, TurboTasks, TurboTasksApi, TurboTasksBackendApi,
94-
TurboTasksCallApi, Unused, UpdateInfo,
93+
turbo_tasks, CurrentCellRef, Invalidator, TaskPersistence, TurboTasks, TurboTasksApi,
94+
TurboTasksBackendApi, TurboTasksCallApi, Unused, UpdateInfo,
9595
};
9696
pub use native_function::{FunctionMeta, NativeFunction};
9797
pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use super::{
88
magic_any::MagicAny,
99
manager::{find_cell_by_type, notify_scheduled_tasks, spawn_detached_for_testing},
1010
};
11-
use crate::debug::ValueDebugFormatString;
11+
use crate::{debug::ValueDebugFormatString, TaskInput, TaskPersistence};
1212

1313
#[inline(never)]
1414
pub async fn value_debug_format_field(value: ValueDebugFormatString<'_>) -> String {
@@ -21,6 +21,14 @@ pub async fn value_debug_format_field(value: ValueDebugFormatString<'_>) -> Stri
2121
}
2222
}
2323

24+
pub fn get_non_local_persistence_from_inputs(inputs: &impl TaskInput) -> TaskPersistence {
25+
if inputs.is_transient() {
26+
TaskPersistence::Transient
27+
} else {
28+
TaskPersistence::Persistent
29+
}
30+
}
31+
2432
#[macro_export]
2533
macro_rules! stringify_path {
2634
($path:path) => {

0 commit comments

Comments
 (0)