Unverified Commit ad399701 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: reduce the amount of task modifications caused by recomputation (#78728)

### What?

Improve backend code to avoid flagging a task as modified when it is just recomputed without changes.
parent fff47d0f
...@@ -56,9 +56,8 @@ use crate::{ ...@@ -56,9 +56,8 @@ use crate::{
backing_storage::BackingStorage, backing_storage::BackingStorage,
data::{ data::{
ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType, ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType,
CachedDataItemValue, CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, DirtyState,
DirtyState, InProgressCellState, InProgressState, InProgressStateInner, OutputValue, InProgressCellState, InProgressState, InProgressStateInner, OutputValue, RootType,
RootType,
}, },
utils::{ utils::{
bi_map::BiMap, chunked_vec::ChunkedVec, ptr_eq_arc::PtrEqArc, sharded::Sharded, swap_retain, bi_map::BiMap, chunked_vec::ChunkedVec, ptr_eq_arc::PtrEqArc, sharded::Sharded, swap_retain,
...@@ -422,7 +421,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -422,7 +421,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
} }
let mut ctx = self.execute_context(turbo_tasks); let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::Meta);
fn listen_to_done_event<B: BackingStorage>( fn listen_to_done_event<B: BackingStorage>(
this: &TurboTasksBackendInner<B>, this: &TurboTasksBackendInner<B>,
...@@ -493,7 +492,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -493,7 +492,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
&mut ctx, &mut ctx,
); );
} }
task = ctx.task(task_id, TaskDataCategory::All); task = ctx.task(task_id, TaskDataCategory::Meta);
} }
let is_dirty = let is_dirty =
...@@ -1552,10 +1551,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1552,10 +1551,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// Remove no longer existing cells and // Remove no longer existing cells and
// find all outdated data items (removed cells, outdated edges) // find all outdated data items (removed cells, outdated edges)
// Note: For persistent tasks we only want to call extract_if when there are actual cells to
// remove to avoid tracking that as modification.
if task_id.is_transient() || iter_many!(task, CellData { cell }
if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index) => cell
).count() > 0 {
removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| { removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| {
matches!(key, CachedDataItemKey::CellData { cell } if cell_counters matches!(key, CachedDataItemKey::CellData { cell } if cell_counters
.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index)) .get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index))
})); }));
}
if self.should_track_children() { if self.should_track_children() {
old_edges.extend( old_edges.extend(
task.iter(CachedDataItemType::OutdatedCollectible) task.iter(CachedDataItemType::OutdatedCollectible)
...@@ -1708,6 +1713,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1708,6 +1713,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
)); ));
// Update the dirty state // Update the dirty state
let old_dirty_state = get!(task, Dirty).copied();
let new_dirty_state = if session_dependent { let new_dirty_state = if session_dependent {
Some(DirtyState { Some(DirtyState {
clean_in_session: Some(self.session_id), clean_in_session: Some(self.session_id),
...@@ -1716,20 +1723,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1716,20 +1723,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
None None
}; };
let old_dirty = if let Some(new_dirty_state) = new_dirty_state { let data_update = if old_dirty_state != new_dirty_state {
if let Some(new_dirty_state) = new_dirty_state {
task.insert(CachedDataItem::Dirty { task.insert(CachedDataItem::Dirty {
value: new_dirty_state, value: new_dirty_state,
})
} else {
task.remove(&CachedDataItemKey::Dirty {})
};
let old_dirty_state = old_dirty.map(|old_dirty| match old_dirty {
CachedDataItemValue::Dirty { value } => value,
_ => unreachable!(),
}); });
} else {
task.remove(&CachedDataItemKey::Dirty {});
}
let data_update = if self.should_track_children() if self.should_track_children()
&& (old_dirty_state.is_some() || new_dirty_state.is_some()) && (old_dirty_state.is_some() || new_dirty_state.is_some())
{ {
let mut dirty_containers = get!(task, AggregatedDirtyContainerCount) let mut dirty_containers = get!(task, AggregatedDirtyContainerCount)
...@@ -1756,13 +1759,17 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1756,13 +1759,17 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
} }
AggregationUpdateJob::data_update( AggregationUpdateJob::data_update(
&mut task, &mut task,
AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update), AggregatedDataUpdate::new()
.dirty_container_update(task_id, aggregated_update),
) )
} else { } else {
None None
} }
} else { } else {
None None
}
} else {
None
}; };
drop(task); drop(task);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment