Skip to content

Commit

Permalink
box InProgressState to avoid larger enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 13, 2025
1 parent 5dc092b commit 0084ab7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
34 changes: 19 additions & 15 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ use crate::{
data::{
ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType,
CachedDataItemValue, CachedDataItemValueRef, CachedDataUpdate, CellRef, CollectibleRef,
CollectiblesRef, DirtyState, InProgressCellState, InProgressState, OutputValue, RootType,
CollectiblesRef, DirtyState, InProgressCellState, InProgressState, InProgressStateInner,
OutputValue, RootType,
},
utils::{bi_map::BiMap, chunked_vec::ChunkedVec, ptr_eq_arc::PtrEqArc, sharded::Sharded},
};
Expand Down Expand Up @@ -405,7 +406,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
if let Some(in_progress) = get!(task, InProgress) {
match in_progress {
InProgressState::Scheduled { done_event, .. }
| InProgressState::InProgress { done_event, .. } => {
| InProgressState::InProgress(box InProgressStateInner {
done_event, ..
}) => {
let reader_desc = reader.map(|r| this.get_task_desc_fn(r));
let listener = done_event.listen_with_note(move || {
if let Some(reader_desc) = reader_desc.as_ref() {
Expand Down Expand Up @@ -1005,14 +1008,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return None;
};
task.add_new(CachedDataItem::InProgress {
value: InProgressState::InProgress {
value: InProgressState::InProgress(Box::new(InProgressStateInner {
stale: false,
once_task,
done_event,
session_dependent: false,
marked_as_completed: false,
new_children: Default::default(),
},
})),
});

if self.should_track_children() {
Expand Down Expand Up @@ -1185,22 +1188,22 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let Some(in_progress) = get_mut!(task, InProgress) else {
panic!("Task execution completed, but task is not in progress: {task:#?}");
};
let &mut InProgressState::InProgress {
let &mut InProgressState::InProgress(box InProgressStateInner {
stale,
ref mut new_children,
..
} = in_progress
}) = in_progress
else {
panic!("Task execution completed, but task is not in progress: {task:#?}");
};

// If the task is stale, reschedule it
if stale {
let Some(InProgressState::InProgress {
let Some(InProgressState::InProgress(box InProgressStateInner {
done_event,
new_children,
..
}) = remove!(task, InProgress)
})) = remove!(task, InProgress)
else {
unreachable!();
};
Expand Down Expand Up @@ -1335,14 +1338,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let Some(in_progress) = remove!(task, InProgress) else {
panic!("Task execution completed, but task is not in progress: {task:#?}");
};
let InProgressState::InProgress {
let InProgressState::InProgress(box InProgressStateInner {
done_event,
once_task: _,
stale,
session_dependent,
marked_as_completed: _,
new_children,
} = in_progress
}) = in_progress
else {
panic!("Task execution completed, but task is not in progress: {task:#?}");
};
Expand Down Expand Up @@ -1686,9 +1689,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
) {
let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task, TaskDataCategory::Data);
if let Some(InProgressState::InProgress {
session_dependent, ..
}) = get_mut!(task, InProgress)
if let Some(InProgressState::InProgress(box InProgressStateInner {
session_dependent,
..
})) = get_mut!(task, InProgress)
{
*session_dependent = true;
}
Expand All @@ -1701,10 +1705,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
) {
let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task, TaskDataCategory::Data);
if let Some(InProgressState::InProgress {
if let Some(InProgressState::InProgress(box InProgressStateInner {
marked_as_completed,
..
}) = get_mut!(task, InProgress)
})) = get_mut!(task, InProgress)
{
*marked_as_completed = true;
// TODO this should remove the dirty state (also check session_dependent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
TaskDataCategory,
},
data::{CachedDataItem, CachedDataItemKey, InProgressState},
data::{CachedDataItem, CachedDataItemKey, InProgressState, InProgressStateInner},
};

#[derive(Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -38,7 +38,7 @@ impl ConnectChildOperation {
return;
}
let mut parent_task = ctx.task(parent_task_id, TaskDataCategory::Meta);
let Some(InProgressState::InProgress { new_children, .. }) =
let Some(InProgressState::InProgress(box InProgressStateInner { new_children, .. })) =
get_mut!(parent_task, InProgress)
else {
panic!("Task is not in progress while calling another task");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::{
storage::{get, get_mut},
TaskDataCategory,
},
data::{CachedDataItem, CachedDataItemKey, CachedDataItemValue, DirtyState, InProgressState},
data::{
CachedDataItem, CachedDataItemKey, CachedDataItemValue, DirtyState, InProgressState,
InProgressStateInner,
},
};

#[derive(Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -162,7 +165,9 @@ pub fn make_task_dirty_internal(
ctx: &impl ExecuteContext,
) {
if make_stale {
if let Some(InProgressState::InProgress { stale, .. }) = get_mut!(task, InProgress) {
if let Some(InProgressState::InProgress(box InProgressStateInner { stale, .. })) =
get_mut!(task, InProgress)
{
if !*stale {
let _span = tracing::trace_span!(
"make task stale",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use crate::{
storage::{get, get_many},
TaskDataCategory,
},
data::{CachedDataItem, CachedDataItemKey, CellRef, InProgressState, OutputValue},
data::{
CachedDataItem, CachedDataItemKey, CellRef, InProgressState, InProgressStateInner,
OutputValue,
},
};

#[derive(Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -42,11 +45,11 @@ impl UpdateOutputOperation {
mut ctx: impl ExecuteContext,
) {
let mut task = ctx.task(task_id, TaskDataCategory::Meta);
let Some(InProgressState::InProgress {
let Some(InProgressState::InProgress(box InProgressStateInner {
stale,
new_children,
..
}) = get!(task, InProgress)
})) = get!(task, InProgress)
else {
panic!("Task is not in progress while updating the output");
};
Expand Down
29 changes: 15 additions & 14 deletions turbopack/crates/turbo-tasks-backend/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,23 @@ pub enum RootType {
OnceTask,
}

#[derive(Debug)]
pub struct InProgressStateInner {
pub stale: bool,
#[allow(dead_code)]
pub once_task: bool,
pub session_dependent: bool,
pub marked_as_completed: bool,
pub done_event: Event,
/// Children that should be connected to the task and have their active_count decremented
/// once the task completes.
pub new_children: FxHashSet<TaskId>,
}

#[derive(Debug)]
pub enum InProgressState {
Scheduled {
done_event: Event,
},
InProgress {
stale: bool,
#[allow(dead_code)]
once_task: bool,
session_dependent: bool,
marked_as_completed: bool,
done_event: Event,
/// Children that should be connected to the task and have their active_count decremented
/// once the task completes.
new_children: FxHashSet<TaskId>,
},
Scheduled { done_event: Event },
InProgress(Box<InProgressStateInner>),
}

transient_traits!(InProgressState);
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(anonymous_lifetime_in_impl_trait)]
#![feature(associated_type_defaults)]
#![feature(iter_collect_into)]
#![feature(box_patterns)]

mod backend;
mod backing_storage;
Expand Down

0 comments on commit 0084ab7

Please sign in to comment.