Skip to content

Commit 5f7cee9

Browse files
committed
Local task resolution
1 parent 6381877 commit 5f7cee9

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

turbopack/crates/turbo-tasks/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bench = false
1212
default = []
1313
tokio_tracing = ["tokio/tracing"]
1414
hanging_detection = []
15+
local_resolution = []
1516

1617
[lints]
1718
workspace = true

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl CurrentGlobalTaskState {
439439
fn create_local_task(
440440
&mut self,
441441
ty: CachedTaskType,
442+
// if this is a `CachedTaskType::Resolve*`, we'll spawn another task with this persistence
442443
persistence: TaskPersistence,
443444
) -> LocalTaskId {
444445
self.local_tasks
@@ -475,7 +476,7 @@ struct CurrentLocalTaskState {
475476
local_task_id: Option<LocalTaskId>,
476477

477478
/// A unique identifier created for each unique[`CurrentLocalTaskState`]. Used to check that
478-
/// [`CurrentTaskState::local_cells`] are valid for the current [`RawVc::LocalCell`].
479+
/// [`CurrentTaskState::local_cells`] are valid for the currant [`RawVc::LocalCell`].
479480
execution_id: ExecutionId,
480481

481482
/// The function's metadata if this is a persistent task. Contains information about arguments
@@ -678,28 +679,32 @@ impl<B: Backend + 'static> TurboTasks<B> {
678679
if registry::get_function(func).arg_meta.is_resolved(&*arg) {
679680
return self.native_call(func, arg, persistence);
680681
}
682+
let task_type = CachedTaskType::ResolveNative {
683+
fn_type: func,
684+
this: None,
685+
arg,
686+
};
687+
#[cfg(feature = "local_resolution")]
688+
return CURRENT_GLOBAL_TASK_STATE.with(move |gts| {
689+
let mut gts_write = gts.write().unwrap();
690+
let local_task_id = gts_write.create_local_task(task_type, persistence);
691+
RawVc::LocalOutput(gts_write.task_id, local_task_id)
692+
});
693+
#[cfg(not(feature = "local_resolution"))]
681694
match persistence {
682695
TaskPersistence::LocalCells => {
683696
todo!("bgw: local tasks");
684697
}
685698
TaskPersistence::Transient => {
686699
RawVc::TaskOutput(self.backend.get_or_create_transient_task(
687-
CachedTaskType::ResolveNative {
688-
fn_type: func,
689-
this: None,
690-
arg,
691-
},
700+
task_type,
692701
current_task("turbo_function calls"),
693702
self,
694703
))
695704
}
696705
TaskPersistence::Persistent => {
697706
RawVc::TaskOutput(self.backend.get_or_create_persistent_task(
698-
CachedTaskType::ResolveNative {
699-
fn_type: func,
700-
this: None,
701-
arg,
702-
},
707+
task_type,
703708
current_task("turbo_function calls"),
704709
self,
705710
))
@@ -722,7 +727,14 @@ impl<B: Backend + 'static> TurboTasks<B> {
722727
this: Some(this),
723728
arg,
724729
};
725-
match persistence {
730+
#[cfg(feature = "local_resolution")]
731+
return CURRENT_GLOBAL_TASK_STATE.with(move |gts| {
732+
let mut gts_write = gts.write().unwrap();
733+
let local_task_id = gts_write.create_local_task(task_type, persistence);
734+
RawVc::LocalOutput(gts_write.task_id, local_task_id)
735+
});
736+
#[cfg(not(feature = "local_resolution"))]
737+
return match persistence {
726738
TaskPersistence::LocalCells => {
727739
todo!("bgw: local tasks");
728740
}
@@ -740,7 +752,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
740752
self,
741753
))
742754
}
743-
}
755+
};
744756
}
745757

746758
pub fn trait_call(
@@ -772,7 +784,15 @@ impl<B: Backend + 'static> TurboTasks<B> {
772784
this,
773785
arg,
774786
};
775-
match persistence {
787+
788+
#[cfg(feature = "local_resolution")]
789+
return CURRENT_GLOBAL_TASK_STATE.with(move |gts| {
790+
let mut gts_write = gts.write().unwrap();
791+
let local_task_id = gts_write.create_local_task(task_type, persistence);
792+
RawVc::LocalOutput(gts_write.task_id, local_task_id)
793+
});
794+
#[cfg(not(feature = "local_resolution"))]
795+
return match persistence {
776796
TaskPersistence::LocalCells => {
777797
todo!("bgw: local tasks");
778798
}
@@ -790,7 +810,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
790810
self,
791811
))
792812
}
793-
}
813+
};
794814
}
795815

796816
#[track_caller]
@@ -2165,12 +2185,17 @@ pub(crate) fn read_local_cell(
21652185
}
21662186

21672187
pub(crate) async fn read_local_output(
2168-
_this: &dyn TurboTasksApi,
2169-
_task_id: TaskId,
2170-
_local_output_id: LocalTaskId,
2171-
_consistency: ReadConsistency,
2188+
this: &dyn TurboTasksApi,
2189+
parent_task_id: TaskId,
2190+
local_task_id: LocalTaskId,
2191+
consistency: ReadConsistency,
21722192
) -> Result<RawVc> {
2173-
todo!("bgw: local outputs");
2193+
loop {
2194+
match this.try_read_local_output(parent_task_id, local_task_id, consistency)? {
2195+
Ok(raw_vc) => return Ok(raw_vc),
2196+
Err(event_listener) => event_listener.await,
2197+
}
2198+
}
21742199
}
21752200

21762201
/// Panics if the [`ExecutionId`] does not match the current task's

0 commit comments

Comments
 (0)