Skip to content

Commit

Permalink
Add stubs for RawVc::TaskOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Aug 19, 2024
1 parent 48aea95 commit 7035bf1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 47 deletions.
60 changes: 22 additions & 38 deletions turbopack/crates/turbo-tasks/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ use serde::{de::Visitor, Deserialize, Serialize};
use crate::{registry, TaskPersistence};

macro_rules! define_id {
($name:ident : $primitive:ty $(,derive($($derive:ty),*))?) => {
(
$name:ident : $primitive:ty
$(,derive($($derive:ty),*))?
$(,serde($serde:tt))?
$(,doc = $doc:literal)*
$(,)?
) => {
$(#[doc = $doc])*
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord $($(,$derive)*)? )]
$(#[serde($serde)])?
pub struct $name {
id: NonZero<$primitive>,
}
Expand Down Expand Up @@ -64,13 +72,24 @@ macro_rules! define_id {
};
}

define_id!(TaskId: u32);
define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent));
define_id!(FunctionId: u32);
define_id!(ValueTypeId: u32);
define_id!(TraitTypeId: u32);
define_id!(BackendJobId: u32);
define_id!(ExecutionId: u64, derive(Debug));
define_id!(LocalCellId: u32, derive(Debug));
define_id!(
LocalCellId: u32,
derive(Debug),
doc = "Represents the nth call to `Vc::cell()` with `local_cells` inside of the parent ",
doc = "non-local task.",
);
define_id!(
LocalOutputId: u32,
derive(Debug, Serialize, Deserialize),
serde(transparent),
doc = "Represents the nth `local_cells` function call inside a task.",
);

impl Debug for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -160,38 +179,3 @@ make_serializable!(
registry::get_trait_type_id_by_global_name,
TraitTypeVisitor
);

impl Serialize for TaskId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u32(**self)
}
}

impl<'de> Deserialize<'de> for TaskId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct V;

impl Visitor<'_> for V {
type Value = TaskId;

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "task id")
}

fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(TaskId::from(v))
}
}

deserializer.deserialize_u32(V)
}
}
34 changes: 33 additions & 1 deletion turbopack/crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use crate::{
},
capture_future::{self, CaptureFuture},
event::{Event, EventListener},
id::{BackendJobId, ExecutionId, FunctionId, LocalCellId, TraitTypeId, TRANSIENT_TASK_BIT},
id::{
BackendJobId, ExecutionId, FunctionId, LocalCellId, LocalOutputId, TraitTypeId,
TRANSIENT_TASK_BIT,
},
id_factory::{IdFactory, IdFactoryWithReuse},
magic_any::MagicAny,
raw_vc::{CellId, RawVc},
Expand Down Expand Up @@ -144,6 +147,26 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
index: CellId,
) -> Result<Result<TypedCellContent, EventListener>>;

fn try_read_local_output(
&self,
_task_id: TaskId,
_local_output_id: LocalOutputId,
_consistency: ReadConsistency,
) -> Result<Result<RawVc, EventListener>> {
todo!("bgw: local outputs");
}

/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
fn try_read_local_output_untracked(
&self,
_task: TaskId,
_local_output_id: LocalOutputId,
_consistency: ReadConsistency,
) -> Result<Result<RawVc, EventListener>> {
todo!("bgw: local outputs");
}

fn read_task_collectibles(&self, task: TaskId, trait_id: TraitTypeId) -> TaskCollectiblesMap;

fn emit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc);
Expand Down Expand Up @@ -1952,6 +1975,15 @@ pub(crate) fn read_local_cell(
})
}

pub(crate) async fn read_local_output(
_this: &dyn TurboTasksApi,
_task_id: TaskId,
_local_output_id: LocalOutputId,
_consistency: ReadConsistency,
) -> Result<RawVc> {
todo!("bgw: local outputs");
}

/// Panics if the [`ExecutionId`] does not match the current task's
/// `execution_id`.
pub(crate) fn assert_execution_id(execution_id: ExecutionId) {
Expand Down
56 changes: 48 additions & 8 deletions turbopack/crates/turbo-tasks/src/raw_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use thiserror::Error;
use crate::{
backend::{CellContent, TypedCellContent},
event::EventListener,
id::{ExecutionId, LocalCellId},
id::{ExecutionId, LocalCellId, LocalOutputId},
manager::{
assert_execution_id, current_task, read_local_cell, read_task_cell, read_task_output,
TurboTasksApi,
assert_execution_id, current_task, read_local_cell, read_local_output, read_task_cell,
read_task_output, TurboTasksApi,
},
registry::{self, get_value_type},
turbo_tasks, CollectiblesSource, ReadConsistency, TaskId, TraitTypeId, ValueType, ValueTypeId,
Expand Down Expand Up @@ -58,6 +58,7 @@ impl Display for CellId {
pub enum RawVc {
TaskOutput(TaskId),
TaskCell(TaskId, CellId),
LocalOutput(TaskId, LocalOutputId),
#[serde(skip)]
LocalCell(ExecutionId, LocalCellId),
}
Expand All @@ -67,6 +68,7 @@ impl RawVc {
match self {
RawVc::TaskOutput(_) => false,
RawVc::TaskCell(_, _) => true,
RawVc::LocalOutput(_, _) => false,
RawVc::LocalCell(_, _) => false,
}
}
Expand All @@ -75,6 +77,7 @@ impl RawVc {
match self {
RawVc::TaskOutput(_) => false,
RawVc::TaskCell(_, _) => false,
RawVc::LocalOutput(_, _) => true,
RawVc::LocalCell(_, _) => true,
}
}
Expand Down Expand Up @@ -162,6 +165,12 @@ impl RawVc {
return Err(ResolveTypeError::NoContent);
}
}
RawVc::LocalOutput(task_id, local_cell_id) => {
current =
read_local_output(&*tt, task_id, local_cell_id, ReadConsistency::Eventual)
.await
.map_err(|source| ResolveTypeError::TaskError { source })?;
}
RawVc::LocalCell(execution_id, local_cell_id) => {
let shared_reference = read_local_cell(execution_id, local_cell_id);
return Ok(
Expand Down Expand Up @@ -193,16 +202,23 @@ impl RawVc {
let tt = turbo_tasks();
let mut current = self;
let mut notified = false;
let mut lazily_notify = || {
if !notified {
tt.notify_scheduled_tasks();
notified = true;
}
};
loop {
match current {
RawVc::TaskOutput(task) => {
if !notified {
tt.notify_scheduled_tasks();
notified = true;
}
lazily_notify();
current = read_task_output(&*tt, task, consistency).await?;
}
RawVc::TaskCell(_, _) => return Ok(current),
RawVc::LocalOutput(task_id, local_cell_id) => {
lazily_notify();
current = read_local_output(&*tt, task_id, local_cell_id, consistency).await?;
}
RawVc::LocalCell(execution_id, local_cell_id) => {
let shared_reference = read_local_cell(execution_id, local_cell_id);
let value_type = get_value_type(shared_reference.0);
Expand All @@ -219,7 +235,7 @@ impl RawVc {

pub fn get_task_id(&self) -> TaskId {
match self {
RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) => *t,
RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) | RawVc::LocalOutput(t, _) => *t,
RawVc::LocalCell(execution_id, _) => {
assert_execution_id(*execution_id);
current_task("RawVc::get_task_id")
Expand Down Expand Up @@ -367,6 +383,30 @@ impl Future for ReadRawVcFuture {
Err(err) => return Poll::Ready(Err(err)),
}
}
RawVc::LocalOutput(task_id, local_output_id) => {
let read_result = if this.untracked {
this.turbo_tasks.try_read_local_output_untracked(
task_id,
local_output_id,
this.consistency,
)
} else {
this.turbo_tasks.try_read_local_output(
task_id,
local_output_id,
this.consistency,
)
};
match read_result {
Ok(Ok(vc)) => {
this.consistency = ReadConsistency::Eventual;
this.current = vc;
continue 'outer;
}
Ok(Err(listener)) => listener,
Err(err) => return Poll::Ready(Err(err)),
}
}
RawVc::LocalCell(execution_id, local_cell_id) => {
return Poll::Ready(Ok(read_local_cell(execution_id, local_cell_id).into()));
}
Expand Down

0 comments on commit 7035bf1

Please sign in to comment.