Skip to content

Commit

Permalink
refactor(turbo-tasks): Move OutputContent from memory backend into tu…
Browse files Browse the repository at this point in the history
…rbo-tasks, represent Empty as None (#69473)

This is preparation for local tasks/cells in #69126.

- Moves this into the `turbo-tasks` crate so that it can be used with
local cells/outputs in #69126.
- Remove the `Empty` state from the enum because the implementation in
#69126 does not currently need that. This can still be represented using
`None`.
  • Loading branch information
bgw authored Sep 10, 2024
1 parent 6d14461 commit b755e1e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
54 changes: 14 additions & 40 deletions turbopack/crates/turbo-tasks-memory/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
use std::{
borrow::Cow,
fmt::{Debug, Display},
mem::take,
};
use std::{borrow::Cow, fmt::Debug, mem::take};

use anyhow::{anyhow, Error, Result};
use turbo_tasks::{util::SharedError, RawVc, TaskId, TaskIdSet, TurboTasksBackendApi};
use turbo_tasks::{
util::SharedError, OutputContent, RawVc, TaskId, TaskIdSet, TurboTasksBackendApi,
};

use crate::MemoryBackend;

#[derive(Default, Debug)]
pub struct Output {
pub(crate) content: OutputContent,
pub(crate) content: Option<OutputContent>,
pub(crate) dependent_tasks: TaskIdSet,
}

#[derive(Clone, Debug, Default)]
pub enum OutputContent {
#[default]
Empty,
Link(RawVc),
Error(SharedError),
Panic(Option<Box<Cow<'static, str>>>),
}

impl Display for OutputContent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OutputContent::Empty => write!(f, "empty"),
OutputContent::Link(raw_vc) => write!(f, "link {:?}", raw_vc),
OutputContent::Error(err) => write!(f, "error {}", err),
OutputContent::Panic(Some(message)) => write!(f, "panic {}", message),
OutputContent::Panic(None) => write!(f, "panic"),
}
}
}

impl Output {
pub fn read(&mut self, reader: TaskId) -> Result<RawVc> {
self.dependent_tasks.insert(reader);
Expand All @@ -44,33 +21,30 @@ impl Output {

/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
pub fn read_untracked(&mut self) -> Result<RawVc> {
pub fn read_untracked(&self) -> Result<RawVc> {
match &self.content {
OutputContent::Empty => Err(anyhow!("Output is empty")),
OutputContent::Error(err) => Err(anyhow::Error::new(err.clone())),
OutputContent::Link(raw_vc) => Ok(*raw_vc),
OutputContent::Panic(Some(message)) => Err(anyhow!("A task panicked: {message}")),
OutputContent::Panic(None) => Err(anyhow!("A task panicked")),
None => Err(anyhow!("Output is empty")),
Some(content) => content.as_read_result(),
}
}

pub fn link(&mut self, target: RawVc, turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>) {
debug_assert!(*self != target);
if let OutputContent::Link(old_target) = &self.content {
if let Some(OutputContent::Link(old_target)) = &self.content {
if *old_target == target {
// unchanged
return;
}
}
self.content = OutputContent::Link(target);
self.content = Some(OutputContent::Link(target));
// notify
if !self.dependent_tasks.is_empty() {
turbo_tasks.schedule_notify_tasks_set(&take(&mut self.dependent_tasks));
}
}

pub fn error(&mut self, error: Error, turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>) {
self.content = OutputContent::Error(SharedError::new(error));
self.content = Some(OutputContent::Error(SharedError::new(error)));
// notify
if !self.dependent_tasks.is_empty() {
turbo_tasks.schedule_notify_tasks_set(&take(&mut self.dependent_tasks));
Expand All @@ -82,7 +56,7 @@ impl Output {
message: Option<Cow<'static, str>>,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) {
self.content = OutputContent::Panic(message.map(Box::new));
self.content = Some(OutputContent::Panic(message.map(Box::new)));
// notify
if !self.dependent_tasks.is_empty() {
turbo_tasks.schedule_notify_tasks_set(&take(&mut self.dependent_tasks));
Expand All @@ -100,8 +74,8 @@ impl Output {
impl PartialEq<RawVc> for Output {
fn eq(&self, rhs: &RawVc) -> bool {
match &self.content {
OutputContent::Link(old_target) => old_target == rhs,
OutputContent::Empty | OutputContent::Error(_) | OutputContent::Panic(_) => false,
Some(OutputContent::Link(old_target)) => old_target == rhs,
Some(OutputContent::Error(_) | OutputContent::Panic(_)) | None => false,
}
}
}
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
cell::{Cell, ReadContentError},
edges_set::{TaskEdge, TaskEdgesList, TaskEdgesSet},
gc::{GcQueue, GcTaskState},
output::{Output, OutputContent},
output::Output,
task::aggregation::{TaskAggregationContext, TaskChange},
MemoryBackend,
};
Expand Down Expand Up @@ -876,7 +876,7 @@ impl Task {
Ok(Ok(result)) => {
if state.output != result {
if cfg!(feature = "print_task_invalidation")
&& !matches!(state.output.content, OutputContent::Empty)
&& state.output.content.is_some()
{
println!(
"Task {{ id: {}, name: {} }} invalidates:",
Expand Down
2 changes: 2 additions & 0 deletions turbopack/crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod manager;
mod native_function;
mod no_move_vec;
mod once_map;
mod output;
pub mod persisted_graph;
pub mod primitives;
mod raw_vc;
Expand Down Expand Up @@ -98,6 +99,7 @@ pub use manager::{
TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, UpdateInfo,
};
pub use native_function::{FunctionMeta, NativeFunction};
pub use output::OutputContent;
pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};
pub use read_ref::ReadRef;
use rustc_hash::FxHasher;
Expand Down
38 changes: 38 additions & 0 deletions turbopack/crates/turbo-tasks/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{
borrow::Cow,
fmt::{self, Display},
};

use anyhow::anyhow;

use crate::{util::SharedError, RawVc};

/// A helper type representing the output of a resolved task.
#[derive(Clone, Debug)]
pub enum OutputContent {
Link(RawVc),
Error(SharedError),
Panic(Option<Box<Cow<'static, str>>>),
}

impl OutputContent {
pub fn as_read_result(&self) -> anyhow::Result<RawVc> {
match &self {
Self::Error(err) => Err(anyhow::Error::new(err.clone())),
Self::Link(raw_vc) => Ok(*raw_vc),
Self::Panic(Some(message)) => Err(anyhow!("A task panicked: {message}")),
Self::Panic(None) => Err(anyhow!("A task panicked")),
}
}
}

impl Display for OutputContent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Link(raw_vc) => write!(f, "link {:?}", raw_vc),
Self::Error(err) => write!(f, "error {}", err),
Self::Panic(Some(message)) => write!(f, "panic {}", message),
Self::Panic(None) => write!(f, "panic"),
}
}
}

0 comments on commit b755e1e

Please sign in to comment.