Skip to content

Commit

Permalink
box InProgress task state (#8644)
Browse files Browse the repository at this point in the history
### Description

That saves 32 bytes per Task

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored Jul 2, 2024
1 parent 52ed46b commit 5aba12d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
66 changes: 34 additions & 32 deletions crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ impl MaybeCollectibles {
}
}

struct InProgressState {
event: Event,
count_as_finished: bool,
/// Dependencies and children that need to be disconnected once leaving
/// this state
outdated_edges: TaskEdgesSet,
new_children: TaskIdSet,
outdated_collectibles: MaybeCollectibles,
}

enum TaskStateType {
/// Ready
///
Expand Down Expand Up @@ -343,15 +353,7 @@ enum TaskStateType {
/// on finish this will move to Done
///
/// on invalidation this will move to InProgressDirty
InProgress {
event: Event,
count_as_finished: bool,
/// Dependencies and children that need to be disconnected once leaving
/// this state
outdated_edges: Box<TaskEdgesSet>,
new_children: TaskIdSet,
outdated_collectibles: MaybeCollectibles,
},
InProgress(Box<InProgressState>),

/// Invalid execution is happening
///
Expand All @@ -367,11 +369,11 @@ impl TaskStateType {
fn children(&self) -> impl Iterator<Item = TaskId> + '_ {
match self {
TaskStateType::Done { edges, .. } => Either::Left(edges.children()),
TaskStateType::InProgress {
TaskStateType::InProgress(box InProgressState {
outdated_edges,
new_children,
..
} => Either::Right(Either::Left(
}) => Either::Right(Either::Left(
outdated_edges
.children()
.chain(new_children.iter().copied()),
Expand All @@ -395,12 +397,12 @@ impl TaskStateType {
let children = edges.drain_children();
(edges, children)
}
TaskStateType::InProgress {
TaskStateType::InProgress(box InProgressState {
outdated_edges,
new_children,
..
} => {
let mut edges = *outdated_edges;
}) => {
let mut edges = outdated_edges;
let mut children = edges.drain_children();
children.extend(new_children.iter().copied());
(edges, children)
Expand Down Expand Up @@ -669,15 +671,15 @@ impl Task {
ref mut outdated_edges,
} => {
let event = event.take();
let outdated_edges = take(outdated_edges);
let outdated_edges = *take(outdated_edges);
let outdated_collectibles = take(&mut state.collectibles);
state.state_type = InProgress {
state.state_type = InProgress(Box::new(InProgressState {
event,
count_as_finished: false,
outdated_edges,
outdated_collectibles,
new_children: Default::default(),
};
}));
}
Dirty { .. } => {
let state_type = Task::state_string(&state);
Expand Down Expand Up @@ -761,12 +763,12 @@ impl Task {
let TaskMetaStateWriteGuard::Full(mut state) = self.state_mut() else {
return;
};
let TaskStateType::InProgress {
let TaskStateType::InProgress(box InProgressState {
ref mut count_as_finished,
ref mut outdated_collectibles,
ref mut outdated_edges,
..
} = state.state_type
}) = state.state_type
else {
return;
};
Expand Down Expand Up @@ -881,15 +883,15 @@ impl Task {
.gc
.execution_completed(duration, memory_usage, generation);
match state.state_type {
InProgress {
InProgress(box InProgressState {
ref mut event,
count_as_finished,
ref mut outdated_edges,
ref mut outdated_collectibles,
ref mut new_children,
} => {
}) => {
let event = event.take();
let mut outdated_edges = *take(outdated_edges);
let mut outdated_edges = take(outdated_edges);
let outdated_collectibles = outdated_collectibles.take_collectibles();
let mut new_edges = take(&mut dependencies);
outdated_edges.remove_all(&new_edges);
Expand Down Expand Up @@ -1082,13 +1084,13 @@ impl Task {
change_job.apply(&aggregation_context);
}
}
InProgress {
InProgress(box InProgressState {
ref mut event,
count_as_finished,
ref mut outdated_edges,
ref mut outdated_collectibles,
ref mut new_children,
} => {
}) => {
let event = event.take();
let mut outdated_edges = take(outdated_edges);
let children_count = new_children.len();
Expand Down Expand Up @@ -1125,7 +1127,7 @@ impl Task {
});
state.state_type = InProgressDirty {
event,
outdated_edges,
outdated_edges: Box::new(outdated_edges),
children_count,
};
drop(state);
Expand Down Expand Up @@ -1279,14 +1281,14 @@ impl Task {
fn state_string(state: &TaskState) -> &'static str {
match state.state_type {
Scheduled { .. } => "scheduled",
InProgress {
InProgress(box InProgressState {
count_as_finished: false,
..
} => "in progress",
InProgress {
}) => "in progress",
InProgress(box InProgressState {
count_as_finished: true,
..
} => "in progress (marked as finished)",
}) => "in progress (marked as finished)",
InProgressDirty { .. } => "in progress (dirty)",
Done { .. } => "done",
Dirty { .. } => "dirty",
Expand All @@ -1305,11 +1307,11 @@ impl Task {
{
let mut state = self.full_state_mut();
match &mut state.state_type {
TaskStateType::InProgress {
TaskStateType::InProgress(box InProgressState {
outdated_edges,
new_children,
..
} => {
}) => {
if new_children.insert(child_id) {
if outdated_edges.remove(TaskEdge::Child(child_id)) {
drop(state);
Expand Down Expand Up @@ -1436,7 +1438,7 @@ impl Task {
Ok(Err(listener))
}
Scheduled { ref event, .. }
| InProgress { ref event, .. }
| InProgress(box InProgressState { ref event, .. })
| InProgressDirty { ref event, .. } => {
let listener = event.listen_with_note(note);
drop(state);
Expand Down
18 changes: 9 additions & 9 deletions crates/turbo-tasks-memory/src/task/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbo_tasks::{event::Event, RawVc, TaskId, TaskIdSet, TraitTypeId, TurboTask

use super::{
meta_state::{FullTaskWriteGuard, TaskMetaStateWriteGuard},
TaskStateType,
InProgressState, TaskStateType,
};
use crate::{
aggregation::{
Expand Down Expand Up @@ -474,10 +474,10 @@ impl<'l> AggregationNodeGuard for TaskGuard<'l> {
if !matches!(
guard.state_type,
TaskStateType::Done { .. }
| TaskStateType::InProgress {
| TaskStateType::InProgress (box InProgressState{
count_as_finished: true,
..
}
})
) {
change.unfinished = 1;
#[cfg(feature = "track_unfinished")]
Expand All @@ -491,10 +491,10 @@ impl<'l> AggregationNodeGuard for TaskGuard<'l> {
change.collectibles.push((trait_type_id, collectible, 1));
}
}
if let TaskStateType::InProgress {
if let TaskStateType::InProgress(box InProgressState {
outdated_collectibles,
..
} = &guard.state_type
}) = &guard.state_type
{
if let Some(collectibles) = outdated_collectibles.as_ref() {
for (&(trait_type_id, collectible), _) in collectibles.iter() {
Expand All @@ -520,10 +520,10 @@ impl<'l> AggregationNodeGuard for TaskGuard<'l> {
if !matches!(
guard.state_type,
TaskStateType::Done { .. }
| TaskStateType::InProgress {
| TaskStateType::InProgress (box InProgressState{
count_as_finished: true,
..
}
})
) {
change.unfinished = -1;
#[cfg(feature = "track_unfinished")]
Expand All @@ -537,10 +537,10 @@ impl<'l> AggregationNodeGuard for TaskGuard<'l> {
change.collectibles.push((trait_type_id, collectible, -1));
}
}
if let TaskStateType::InProgress {
if let TaskStateType::InProgress(box InProgressState {
outdated_collectibles,
..
} = &guard.state_type
}) = &guard.state_type
{
if let Some(collectibles) = outdated_collectibles.as_ref() {
for (&(trait_type_id, collectible), _) in collectibles.iter() {
Expand Down

0 comments on commit 5aba12d

Please sign in to comment.