From d911897941cd4a2bac77b8ebb14a04e4533c5757 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 08:59:02 +0200 Subject: [PATCH 1/8] deref --- turbopack/crates/turbo-tasks-memory/src/task/aggregation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task/aggregation.rs b/turbopack/crates/turbo-tasks-memory/src/task/aggregation.rs index cd94e23fa42e8..6841e50152b39 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task/aggregation.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task/aggregation.rs @@ -574,7 +574,7 @@ impl<'l> AggregationNodeGuard for TaskGuard<'l> { for (&(trait_type_id, collectible), count) in collectibles.iter() { change .collectibles - .push((trait_type_id, collectible, -count)); + .push((trait_type_id, collectible, -*count)); } } if let TaskStateType::InProgress(box InProgressState { From 1fe68bcbd02c8333e614d5376095fcd16f1d8c75 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 08:59:48 +0200 Subject: [PATCH 2/8] switch remove change order --- .../crates/turbo-tasks-memory/src/task.rs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index c40964eb68d78..92dca7d284128 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -828,6 +828,16 @@ impl Task { let outdated_children = outdated_edges.drain_children(); let outdated_collectibles = outdated_collectibles.take_collectibles(); + let remove_job = if outdated_children.is_empty() { + None + } else { + Some(state.aggregation_node.handle_lost_edges( + &aggregation_context, + &self.id, + outdated_children, + )) + }; + let mut change = TaskChange { unfinished: -1, #[cfg(feature = "track_unfinished")] @@ -842,18 +852,10 @@ impl Task { let change_job = state .aggregation_node .apply_change(&aggregation_context, change); - let remove_job = if outdated_children.is_empty() { - None - } else { - Some(state.aggregation_node.handle_lost_edges( - &aggregation_context, - &self.id, - outdated_children, - )) - }; + drop(state); - change_job.apply(&aggregation_context); remove_job.apply(&aggregation_context); + change_job.apply(&aggregation_context); } aggregation_context.apply_queued_updates(); } From cf5089eff0a7bcd4137d7ac8ea758b5d1df15e68 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 09:00:19 +0200 Subject: [PATCH 3/8] switch remove change order 2 --- .../crates/turbo-tasks-memory/src/task.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index 92dca7d284128..f1354e571de45 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -1024,6 +1024,14 @@ impl Task { stateful, edges: new_edges.into_list(), }; + let outdated_children = outdated_edges.drain_children(); + if !outdated_children.is_empty() { + remove_job = state.aggregation_node.handle_lost_edges( + &aggregation_context, + &self.id, + outdated_children, + ); + } if !count_as_finished { let mut change = TaskChange { unfinished: -1, @@ -1048,14 +1056,7 @@ impl Task { .aggregation_node .apply_change(&aggregation_context, change); } - let outdated_children = outdated_edges.drain_children(); - if !outdated_children.is_empty() { - remove_job = state.aggregation_node.handle_lost_edges( - &aggregation_context, - &self.id, - outdated_children, - ); - } + done_event.notify(usize::MAX); drop(state); self.clear_dependencies(outdated_edges, backend, turbo_tasks); @@ -1064,8 +1065,8 @@ impl Task { for cell in drained_cells { cell.gc_drop(turbo_tasks); } - change_job.apply(&aggregation_context); remove_job.apply(&aggregation_context); + change_job.apply(&aggregation_context); } if let TaskType::Once(_) = self.ty { // unset the root type, so tasks below are no longer active From 62ff997648064e5fb8f723989a44156b04d0b2a4 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 09:31:18 +0200 Subject: [PATCH 4/8] take_collectibles does not need to return an Option --- .../crates/turbo-tasks-memory/src/task.rs | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index f1354e571de45..43818b2be00b3 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -268,8 +268,11 @@ struct MaybeCollectibles { impl MaybeCollectibles { /// Consumes the collectibles (if any) and return them. - fn take_collectibles(&mut self) -> Option { - self.inner.as_mut().map(|boxed| take(&mut **boxed)) + fn take_collectibles(&mut self) -> Collectibles { + self.inner + .as_mut() + .map(|boxed| take(&mut **boxed)) + .unwrap_or_default() } /// Consumes the collectibles (if any) and return them. @@ -844,10 +847,8 @@ impl Task { unfinished_tasks_update: vec![(self.id, -1)], ..Default::default() }; - if let Some(collectibles) = outdated_collectibles { - for ((trait_type, value), count) in collectibles.into_iter() { - change.collectibles.push((trait_type, value, -count)); - } + for ((trait_type, value), count) in outdated_collectibles.into_iter() { + change.collectibles.push((trait_type, value, -count)); } let change_job = state .aggregation_node @@ -986,9 +987,9 @@ impl Task { for child in new_children { outdated_edges.insert(TaskEdge::Child(child)); } - if let Some(collectibles) = outdated_collectibles { + if !outdated_collectibles.is_empty() { let mut change = TaskChange::default(); - for ((trait_type, value), count) in collectibles.into_iter() { + for ((trait_type, value), count) in outdated_collectibles.into_iter() { change.collectibles.push((trait_type, value, -count)); } change_job = state @@ -1039,17 +1040,15 @@ impl Task { unfinished_tasks_update: vec![(self.id, -1)], ..Default::default() }; - if let Some(collectibles) = outdated_collectibles { - for ((trait_type, value), count) in collectibles.into_iter() { - change.collectibles.push((trait_type, value, -count)); - } + for ((trait_type, value), count) in outdated_collectibles.into_iter() { + change.collectibles.push((trait_type, value, -count)); } change_job = state .aggregation_node .apply_change(&aggregation_context, change); - } else if let Some(collectibles) = outdated_collectibles { + } else if !outdated_collectibles.is_empty() { let mut change = TaskChange::default(); - for ((trait_type, value), count) in collectibles.into_iter() { + for ((trait_type, value), count) in outdated_collectibles.into_iter() { change.collectibles.push((trait_type, value, -count)); } change_job = state From d4cdf5160f44fd2e6d9c55aa352317f6afb99aca Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 09:32:39 +0200 Subject: [PATCH 5/8] avoid double Option --- turbopack/crates/turbo-tasks-memory/src/task.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index 43818b2be00b3..5b2948cdb481a 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -834,11 +834,11 @@ impl Task { let remove_job = if outdated_children.is_empty() { None } else { - Some(state.aggregation_node.handle_lost_edges( + state.aggregation_node.handle_lost_edges( &aggregation_context, &self.id, outdated_children, - )) + ) }; let mut change = TaskChange { From dc8eae02a1c1b6d7fdb2672b91a6e7497d4b6df3 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 09:44:35 +0200 Subject: [PATCH 6/8] fix edges set bugs --- .../crates/turbo-tasks-memory/src/edges_set.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/edges_set.rs b/turbopack/crates/turbo-tasks-memory/src/edges_set.rs index 648841200db90..f8933dd4c57d4 100644 --- a/turbopack/crates/turbo-tasks-memory/src/edges_set.rs +++ b/turbopack/crates/turbo-tasks-memory/src/edges_set.rs @@ -194,7 +194,7 @@ impl EdgesDataEntry { | EdgesDataEntry::OutputAndCell0(type_id) | EdgesDataEntry::ChildAndCell0(type_id) | EdgesDataEntry::ChildOutputAndCell0(type_id), - ) => *type_id == cell_id.type_id, + ) => cell_id.index == 0 && *type_id == cell_id.type_id, (entry, EdgesDataEntry::Complex(set)) => set.contains(&entry), _ => false, } @@ -330,26 +330,26 @@ impl EdgesDataEntry { } _ => {} }, - EdgeEntry::Cell(_) => match self { - EdgesDataEntry::Cell0(_) => { + EdgeEntry::Cell(cell_id) if cell_id.index == 0 => match self { + EdgesDataEntry::Cell0(value_ty) if cell_id.type_id == *value_ty => { *self = EdgesDataEntry::Empty; return true; } - EdgesDataEntry::OutputAndCell0(_) => { + EdgesDataEntry::OutputAndCell0(value_ty) if cell_id.type_id == *value_ty => { *self = EdgesDataEntry::Output; return true; } - EdgesDataEntry::ChildAndCell0(_) => { + EdgesDataEntry::ChildAndCell0(value_ty) if cell_id.type_id == *value_ty => { *self = EdgesDataEntry::Child; return true; } - EdgesDataEntry::ChildOutputAndCell0(_) => { + EdgesDataEntry::ChildOutputAndCell0(value_ty) if cell_id.type_id == *value_ty => { *self = EdgesDataEntry::ChildAndOutput; return true; } _ => {} }, - EdgeEntry::Collectibles(_) => {} + EdgeEntry::Cell(_) | EdgeEntry::Collectibles(_) => {} } if let EdgesDataEntry::Complex(set) = self { if set.remove(&entry) { From e61889c8de10246bb90948beb491c98e861b0699 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 17 Sep 2024 15:01:33 +0200 Subject: [PATCH 7/8] remove collectibles from outdated collectibles --- .../crates/turbo-tasks-memory/src/task.rs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index 5b2948cdb481a..317640d36482f 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -308,6 +308,23 @@ impl MaybeCollectibles { .or_default(); *value -= count as i32; } + + /// Removes an collectible if the count is positive. + fn remove_emit(&mut self, trait_type: TraitTypeId, value: RawVc) -> bool { + let Some(inner) = self.inner.as_mut() else { + return false; + }; + + let auto_hash_map::map::Entry::Occupied(mut e) = inner.entry((trait_type, value)) else { + return false; + }; + let value = e.get_mut(); + *value -= 1; + if *value == 0 { + e.remove(); + } + true + } } struct InProgressState { @@ -1682,9 +1699,18 @@ impl Task { backend: &MemoryBackend, turbo_tasks: &dyn TurboTasksBackendApi, ) { - let mut aggregation_context = TaskAggregationContext::new(turbo_tasks, backend); let mut state = self.full_state_mut(); state.collectibles.emit(trait_type, collectible); + if let TaskStateType::InProgress(box InProgressState { + outdated_collectibles, + .. + }) = &mut state.state_type + { + if outdated_collectibles.remove_emit(trait_type, collectible) { + return; + } + } + let mut aggregation_context = TaskAggregationContext::new(turbo_tasks, backend); let change_job = state.aggregation_node.apply_change( &aggregation_context, TaskChange { From 46b5ade8154426a6a02f28eed53e9cf9234695bb Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 18 Sep 2024 12:42:12 +0200 Subject: [PATCH 8/8] remove unneccessary remove --- turbopack/crates/turbo-tasks-memory/src/task.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/turbopack/crates/turbo-tasks-memory/src/task.rs b/turbopack/crates/turbo-tasks-memory/src/task.rs index 317640d36482f..886ffcf24f2be 100644 --- a/turbopack/crates/turbo-tasks-memory/src/task.rs +++ b/turbopack/crates/turbo-tasks-memory/src/task.rs @@ -1028,7 +1028,6 @@ impl Task { outdated_edges.remove_all(&new_edges); for child in new_children { new_edges.insert(TaskEdge::Child(child)); - outdated_edges.remove(TaskEdge::Child(child)); } if !backend.has_gc() { // This will stay here for longer, so make sure to not consume too