From 0953e60e3ef38c4303860d880ea4429b9ef3c597 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 22 Jan 2024 07:34:57 +0000 Subject: [PATCH 1/2] add lazy_remove_children feature flag --- crates/turbo-tasks-memory/Cargo.toml | 3 +- crates/turbo-tasks-memory/src/task.rs | 31 +++++++++++++- .../src/task/aggregation.rs | 40 ++++++++++++------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/crates/turbo-tasks-memory/Cargo.toml b/crates/turbo-tasks-memory/Cargo.toml index cad5a19744c6a..ae1acec5a021e 100644 --- a/crates/turbo-tasks-memory/Cargo.toml +++ b/crates/turbo-tasks-memory/Cargo.toml @@ -54,7 +54,8 @@ print_scope_updates = [] print_task_invalidation = [] inline_add_to_scope = [] inline_remove_from_scope = [] -default = [] +lazy_remove_children = [] +default = ["lazy_remove_children"] [[bench]] name = "mod" diff --git a/crates/turbo-tasks-memory/src/task.rs b/crates/turbo-tasks-memory/src/task.rs index 88f9ead93260f..4e213b0f80117 100644 --- a/crates/turbo-tasks-memory/src/task.rs +++ b/crates/turbo-tasks-memory/src/task.rs @@ -404,6 +404,7 @@ enum TaskStateType { event: Event, count_as_finished: bool, /// Children that need to be disconnected once leaving this state + #[cfg(feature = "lazy_remove_children")] outdated_children: TaskIdSet, outdated_collectibles: MaybeCollectibles, }, @@ -680,6 +681,8 @@ impl Task { let dependencies; let (future, span) = { let mut state = self.full_state_mut(); + #[cfg(not(feature = "lazy_remove_children"))] + let remove_job; match state.state_type { Done { .. } | InProgress { .. } | InProgressDirty { .. } => { // should not start in this state @@ -693,9 +696,16 @@ impl Task { dependencies = take(outdated_dependencies); let outdated_children = take(&mut state.children); let outdated_collectibles = take(&mut state.collectibles); + #[cfg(not(feature = "lazy_remove_children"))] + { + remove_job = state + .aggregation_leaf + .remove_children_job(&aggregation_context, outdated_children); + } state.state_type = InProgress { event, count_as_finished: false, + #[cfg(feature = "lazy_remove_children")] outdated_children, outdated_collectibles, }; @@ -709,7 +719,12 @@ impl Task { ) } }; - self.make_execution_future(state, backend, turbo_tasks) + let result = self.make_execution_future(state, backend, turbo_tasks); + #[cfg(not(feature = "lazy_remove_children"))] + { + remove_job(); + } + result }; aggregation_context.apply_queued_updates(); self.clear_dependencies(dependencies, backend, turbo_tasks); @@ -816,6 +831,7 @@ impl Task { }; let TaskStateType::InProgress { ref mut count_as_finished, + #[cfg(feature = "lazy_remove_children")] ref mut outdated_children, ref mut outdated_collectibles, .. @@ -829,6 +845,7 @@ impl Task { *count_as_finished = true; let mut aggregation_context = TaskAggregationContext::new(turbo_tasks, backend); { + #[cfg(feature = "lazy_remove_children")] let outdated_children = take(outdated_children); let outdated_collectibles = outdated_collectibles.take_collectibles(); @@ -846,6 +863,7 @@ impl Task { let change_job = state .aggregation_leaf .change_job(&aggregation_context, change); + #[cfg(feature = "lazy_remove_children")] let remove_job = if outdated_children.is_empty() { None } else { @@ -857,6 +875,7 @@ impl Task { }; drop(state); change_job(); + #[cfg(feature = "lazy_remove_children")] if let Some(job) = remove_job { job(); } @@ -926,6 +945,7 @@ impl Task { let mut schedule_task = false; { let mut change_job = None; + #[cfg(feature = "lazy_remove_children")] let mut remove_job = None; let mut dependencies = DEPENDENCIES_TO_TRACK.with(|deps| deps.take()); { @@ -938,10 +958,12 @@ impl Task { InProgress { ref mut event, count_as_finished, + #[cfg(feature = "lazy_remove_children")] ref mut outdated_children, ref mut outdated_collectibles, } => { let event = event.take(); + #[cfg(feature = "lazy_remove_children")] let outdated_children = take(outdated_children); let outdated_collectibles = outdated_collectibles.take_collectibles(); let mut dependencies = take(&mut dependencies); @@ -972,6 +994,7 @@ impl Task { .change_job(&aggregation_context, change), ); } + #[cfg(feature = "lazy_remove_children")] if !outdated_children.is_empty() { remove_job = Some( state @@ -1003,6 +1026,7 @@ impl Task { if let Some(job) = change_job { job(); } + #[cfg(feature = "lazy_remove_children")] if let Some(job) = remove_job { job(); } @@ -1149,10 +1173,12 @@ impl Task { InProgress { ref mut event, count_as_finished, + #[cfg(feature = "lazy_remove_children")] ref mut outdated_children, ref mut outdated_collectibles, } => { let event = event.take(); + #[cfg(feature = "lazy_remove_children")] let outdated_children = take(outdated_children); let outdated_collectibles = outdated_collectibles.take_collectibles(); let change = if count_as_finished { @@ -1182,6 +1208,7 @@ impl Task { .aggregation_leaf .change_job(&aggregation_context, change) }); + #[cfg(feature = "lazy_remove_children")] let remove_job = state .aggregation_leaf .remove_children_job(&aggregation_context, outdated_children); @@ -1190,6 +1217,7 @@ impl Task { if let Some(job) = change_job { job(); } + #[cfg(feature = "lazy_remove_children")] remove_job(); } } @@ -1461,6 +1489,7 @@ impl Task { let TaskGuard { guard, .. } = guard; let mut state = TaskMetaStateWriteGuard::full_from(guard.into_inner(), self); if state.children.insert(child_id) { + #[cfg(feature = "lazy_remove_children")] if let TaskStateType::InProgress { outdated_children, .. } = &mut state.state_type diff --git a/crates/turbo-tasks-memory/src/task/aggregation.rs b/crates/turbo-tasks-memory/src/task/aggregation.rs index 707c2ac7efad2..1e8fd40a2d027 100644 --- a/crates/turbo-tasks-memory/src/task/aggregation.rs +++ b/crates/turbo-tasks-memory/src/task/aggregation.rs @@ -416,6 +416,7 @@ impl<'l> AggregationItemLock for TaskGuard<'l> { fn number_of_children(&self) -> usize { match self.guard { TaskMetaStateWriteGuard::Full(ref guard) => match &guard.state_type { + #[cfg(feature = "lazy_remove_children")] TaskStateType::InProgress { outdated_children, .. } => guard.children.len() + outdated_children.len(), @@ -428,21 +429,30 @@ impl<'l> AggregationItemLock for TaskGuard<'l> { fn children(&self) -> Self::ChildrenIter<'_> { match self.guard { TaskMetaStateWriteGuard::Full(ref guard) => { - let outdated_children = match &guard.state_type { - TaskStateType::InProgress { - outdated_children, .. - } => Some(outdated_children.iter().map(Cow::Borrowed)), - _ => None, - }; - Some( - guard - .children - .iter() - .map(Cow::Borrowed) - .chain(outdated_children.into_iter().flatten()), - ) - .into_iter() - .flatten() + #[cfg(feature = "lazy_remove_children")] + { + let outdated_children = match &guard.state_type { + TaskStateType::InProgress { + outdated_children, .. + } => Some(outdated_children.iter().map(Cow::Borrowed)), + _ => None, + }; + Some( + guard + .children + .iter() + .map(Cow::Borrowed) + .chain(outdated_children.into_iter().flatten()), + ) + .into_iter() + .flatten() + } + #[cfg(not(feature = "lazy_remove_children"))] + { + Some(guard.children.iter().map(Cow::Borrowed)) + .into_iter() + .flatten() + } } TaskMetaStateWriteGuard::Partial(_) | TaskMetaStateWriteGuard::Unloaded(_) => { None.into_iter().flatten() From f195f87a843ccbabe7a0e0f84568f21c9b87af30 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 22 Jan 2024 14:57:52 +0000 Subject: [PATCH 2/2] clippy --- crates/turbo-tasks-memory/src/task.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/turbo-tasks-memory/src/task.rs b/crates/turbo-tasks-memory/src/task.rs index 4e213b0f80117..aa369a239f2c1 100644 --- a/crates/turbo-tasks-memory/src/task.rs +++ b/crates/turbo-tasks-memory/src/task.rs @@ -724,6 +724,7 @@ impl Task { { remove_job(); } + #[allow(clippy::let_and_return)] result }; aggregation_context.apply_queued_updates();