Skip to content

Commit a51f1ee

Browse files
committed
Split Dirty into Dirtyness and CleanInSession
1 parent 19aee3f commit a51f1ee

File tree

5 files changed

+171
-70
lines changed

5 files changed

+171
-70
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::{
6161
backing_storage::BackingStorage,
6262
data::{
6363
ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType,
64-
CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, DirtyState,
64+
CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, Dirtyness,
6565
InProgressCellState, InProgressState, InProgressStateInner, OutputValue, RootType,
6666
},
6767
utils::{
@@ -567,8 +567,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
567567
}
568568
}
569569

570-
let is_dirty =
571-
get!(task, Dirty).map_or(false, |dirty_state| dirty_state.get(self.session_id));
570+
let is_dirty = task
571+
.dirty_state()
572+
.map_or(false, |dirty_state| dirty_state.get(self.session_id));
572573

573574
// Check the dirty count of the root node
574575
let dirty_tasks = get!(task, AggregatedDirtyContainerCount)
@@ -624,7 +625,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
624625
visited: &mut FxHashSet<TaskId>,
625626
) -> String {
626627
let task = ctx.task(task_id, TaskDataCategory::Data);
627-
let is_dirty = get!(task, Dirty)
628+
let is_dirty = task
629+
.dirty_state()
628630
.map_or(false, |dirty_state| dirty_state.get(ctx.session_id()));
629631
let in_progress =
630632
get!(task, InProgress).map_or("not in progress", |p| match p {
@@ -2369,57 +2371,60 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
23692371
));
23702372

23712373
// Update the dirty state
2372-
let old_dirty_state = get!(task, Dirty).copied();
2374+
let old_dirtyness = task.dirtyness_and_session();
23732375

2374-
let new_dirty_state = if session_dependent {
2375-
Some(DirtyState {
2376-
clean_in_session: Some(self.session_id),
2377-
})
2376+
let new_dirtyness = if session_dependent {
2377+
Some((Dirtyness::SessionDependent, Some(self.session_id)))
23782378
} else {
23792379
None
23802380
};
23812381

2382-
let dirty_changed = old_dirty_state != new_dirty_state;
2382+
let dirty_changed = old_dirtyness != new_dirtyness;
23832383
let data_update = if dirty_changed {
2384-
if let Some(new_dirty_state) = new_dirty_state {
2385-
task.insert(CachedDataItem::Dirty {
2386-
value: new_dirty_state,
2387-
});
2388-
} else {
2384+
if let Some((value, _)) = new_dirtyness {
2385+
task.insert(CachedDataItem::Dirty { value });
2386+
} else if old_dirtyness.is_some() {
23892387
task.remove(&CachedDataItemKey::Dirty {});
23902388
}
2389+
if let Some(session_id) = new_dirtyness.and_then(|t| t.1) {
2390+
task.insert(CachedDataItem::CleanInSession { value: session_id });
2391+
} else if old_dirtyness.is_some_and(|t| t.1.is_some()) {
2392+
task.remove(&CachedDataItemKey::CleanInSession {});
2393+
}
23912394

2392-
if old_dirty_state.is_some() || new_dirty_state.is_some() {
2393-
let mut dirty_containers = get!(task, AggregatedDirtyContainerCount)
2394-
.cloned()
2395-
.unwrap_or_default();
2396-
if let Some(old_dirty_state) = old_dirty_state {
2397-
dirty_containers.update_with_dirty_state(&old_dirty_state);
2395+
let mut dirty_containers = get!(task, AggregatedDirtyContainerCount)
2396+
.cloned()
2397+
.unwrap_or_default();
2398+
if let Some((old_dirtyness, old_clean_in_session)) = old_dirtyness {
2399+
dirty_containers
2400+
.update_with_dirtyness_and_session(old_dirtyness, old_clean_in_session);
2401+
}
2402+
let aggregated_update = match (old_dirtyness, new_dirtyness) {
2403+
(None, None) => unreachable!(),
2404+
(Some(old), None) => {
2405+
dirty_containers.undo_update_with_dirtyness_and_session(old.0, old.1)
23982406
}
2399-
let aggregated_update = match (old_dirty_state, new_dirty_state) {
2400-
(None, None) => unreachable!(),
2401-
(Some(old), None) => dirty_containers.undo_update_with_dirty_state(&old),
2402-
(None, Some(new)) => dirty_containers.update_with_dirty_state(&new),
2403-
(Some(old), Some(new)) => dirty_containers.replace_dirty_state(&old, &new),
2404-
};
2405-
if !aggregated_update.is_zero() {
2406-
if aggregated_update.get(self.session_id) < 0
2407-
&& let Some(activeness_state) = get_mut!(task, Activeness)
2408-
{
2409-
activeness_state.all_clean_event.notify(usize::MAX);
2410-
activeness_state.unset_active_until_clean();
2411-
if activeness_state.is_empty() {
2412-
task.remove(&CachedDataItemKey::Activeness {});
2413-
}
2407+
(None, Some(new)) => {
2408+
dirty_containers.update_with_dirtyness_and_session(new.0, new.1)
2409+
}
2410+
(Some(old), Some(new)) => {
2411+
dirty_containers.replace_dirtyness_and_session(old.0, old.1, new.0, new.1)
2412+
}
2413+
};
2414+
if !aggregated_update.is_zero() {
2415+
if aggregated_update.get(self.session_id) < 0
2416+
&& let Some(activeness_state) = get_mut!(task, Activeness)
2417+
{
2418+
activeness_state.all_clean_event.notify(usize::MAX);
2419+
activeness_state.unset_active_until_clean();
2420+
if activeness_state.is_empty() {
2421+
task.remove(&CachedDataItemKey::Activeness {});
24142422
}
2415-
AggregationUpdateJob::data_update(
2416-
&mut task,
2417-
AggregatedDataUpdate::new()
2418-
.dirty_container_update(task_id, aggregated_update),
2419-
)
2420-
} else {
2421-
None
24222423
}
2424+
AggregationUpdateJob::data_update(
2425+
&mut task,
2426+
AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update),
2427+
)
24232428
} else {
24242429
None
24252430
}
@@ -2887,7 +2892,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
28872892

28882893
let mut ctx = self.execute_context(turbo_tasks);
28892894
let mut task = ctx.task(task_id, TaskDataCategory::All);
2890-
let is_dirty = get!(task, Dirty).map_or(false, |dirty| dirty.get(self.session_id));
2895+
let is_dirty = task
2896+
.dirty_state()
2897+
.map_or(false, |dirty| dirty.get(self.session_id));
28912898
let has_dirty_containers = get!(task, AggregatedDirtyContainerCount)
28922899
.map_or(false, |dirty_containers| {
28932900
dirty_containers.get(self.session_id) > 0
@@ -2980,7 +2987,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
29802987
}
29812988
}
29822989

2983-
let is_dirty = get!(task, Dirty).is_some_and(|dirty| dirty.get(self.session_id));
2990+
let is_dirty = task
2991+
.dirty_state()
2992+
.is_some_and(|dirty| dirty.get(self.session_id));
29842993
let has_dirty_container = get!(task, AggregatedDirtyContainerCount)
29852994
.is_some_and(|count| count.get(self.session_id) > 0);
29862995
let should_be_in_upper = is_dirty || has_dirty_container;

turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ impl AggregatedDataUpdate {
249249
collectibles_update.push((collectible, 1));
250250
}
251251
}
252-
if let Some(dirty) = get!(task, Dirty) {
253-
dirty_container_count.update_with_dirty_state(dirty);
252+
if let Some((dirtyness, clean_in_session)) = task.dirtyness_and_session() {
253+
dirty_container_count.update_with_dirtyness_and_session(dirtyness, clean_in_session);
254254
}
255255

256256
let mut result = Self::new().collectibles_update(collectibles_update);
@@ -323,7 +323,7 @@ impl AggregatedDataUpdate {
323323
);
324324

325325
if !aggregated_update.is_zero() {
326-
let dirty_state = get!(task, Dirty).copied();
326+
let dirty_state = task.dirty_state();
327327
let task_id = task.id();
328328
update!(task, AggregatedDirtyContainerCount, |old: Option<
329329
DirtyContainerCount,
@@ -1209,7 +1209,7 @@ impl AggregationUpdateQueue {
12091209
) {
12101210
let session_id = ctx.session_id();
12111211
// Task need to be scheduled if it's dirty or doesn't have output
1212-
let dirty = get!(task, Dirty).map_or(false, |d| d.get(session_id));
1212+
let dirty = task.dirty_state().map_or(false, |d| d.get(session_id));
12131213
let should_schedule = if dirty {
12141214
Some(TaskExecutionReason::ActivateDirty)
12151215
} else if !task.has_key(&CachedDataItemKey::Output {}) {

turbopack/crates/turbo-tasks-backend/src/backend/operation/invalidate.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use crate::{
1111
AggregatedDataUpdate, AggregationUpdateJob, AggregationUpdateQueue,
1212
},
1313
},
14-
storage::{get, get_mut},
14+
storage::{get, get_mut, remove},
1515
},
1616
data::{
17-
CachedDataItem, CachedDataItemKey, CachedDataItemValue, DirtyState, InProgressState,
18-
InProgressStateInner,
17+
CachedDataItem, CachedDataItemKey, CachedDataItemValue, DirtyState, Dirtyness,
18+
InProgressState, InProgressStateInner,
1919
},
2020
};
2121

@@ -232,15 +232,11 @@ pub fn make_task_dirty_internal(
232232
*stale = true;
233233
}
234234
let old = task.insert(CachedDataItem::Dirty {
235-
value: DirtyState {
236-
clean_in_session: None,
237-
},
235+
value: Dirtyness::Dirty,
238236
});
239237
let mut dirty_container = match old {
240238
Some(CachedDataItemValue::Dirty {
241-
value: DirtyState {
242-
clean_in_session: None,
243-
},
239+
value: Dirtyness::Dirty,
244240
}) => {
245241
#[cfg(feature = "trace_task_dirty")]
246242
let _span = tracing::trace_span!(
@@ -254,16 +250,30 @@ pub fn make_task_dirty_internal(
254250
return;
255251
}
256252
Some(CachedDataItemValue::Dirty {
257-
value: DirtyState {
258-
clean_in_session: Some(session_id),
259-
},
253+
value: Dirtyness::SessionDependent,
260254
}) => {
261-
// Got dirty in that one session only
262-
let mut dirty_container = get!(task, AggregatedDirtyContainerCount)
263-
.cloned()
264-
.unwrap_or_default();
265-
dirty_container.update_session_dependent(session_id, 1);
266-
dirty_container
255+
let old = remove!(task, CleanInSession);
256+
match old {
257+
None => {
258+
#[cfg(feature = "trace_task_dirty")]
259+
let _span = tracing::trace_span!(
260+
"session-dependent task already dirty",
261+
name = ctx.get_task_description(task_id),
262+
cause = %TaskDirtyCauseInContext::new(&cause, ctx)
263+
)
264+
.entered();
265+
// already dirty
266+
return;
267+
}
268+
Some(session_id) => {
269+
// Got dirty in that one session only
270+
let mut dirty_container = get!(task, AggregatedDirtyContainerCount)
271+
.cloned()
272+
.unwrap_or_default();
273+
dirty_container.update_session_dependent(session_id, 1);
274+
dirty_container
275+
}
276+
}
267277
}
268278
None => {
269279
// Get dirty for all sessions

turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ use crate::{
2020
backend::{
2121
OperationGuard, TaskDataCategory, TransientTask, TurboTasksBackend, TurboTasksBackendInner,
2222
TurboTasksBackendJob,
23-
storage::{SpecificTaskDataCategory, StorageWriteGuard, iter_many},
23+
storage::{SpecificTaskDataCategory, StorageWriteGuard, get, iter_many},
2424
},
2525
backing_storage::BackingStorage,
2626
data::{
2727
CachedDataItem, CachedDataItemKey, CachedDataItemType, CachedDataItemValue,
28-
CachedDataItemValueRef, CachedDataItemValueRefMut,
28+
CachedDataItemValueRef, CachedDataItemValueRefMut, DirtyState, Dirtyness,
2929
},
3030
};
3131

@@ -415,6 +415,30 @@ pub trait TaskGuard: Debug {
415415
fn invalidate_serialization(&mut self);
416416
fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>>;
417417
fn is_immutable(&self) -> bool;
418+
fn dirtyness_and_session(&self) -> Option<(Dirtyness, Option<SessionId>)> {
419+
match get!(self, Dirty)? {
420+
Dirtyness::Dirty => Some((Dirtyness::Dirty, None)),
421+
Dirtyness::SessionDependent => Some((
422+
Dirtyness::SessionDependent,
423+
get!(self, CleanInSession).copied(),
424+
)),
425+
}
426+
}
427+
fn dirty_state(&self) -> Option<DirtyState> {
428+
match get!(self, Dirty)? {
429+
Dirtyness::Dirty => Some(DirtyState {
430+
clean_in_session: None,
431+
}),
432+
Dirtyness::SessionDependent => match get!(self, CleanInSession) {
433+
None => Some(DirtyState {
434+
clean_in_session: None,
435+
}),
436+
Some(session) => Some(DirtyState {
437+
clean_in_session: Some(*session),
438+
}),
439+
},
440+
}
441+
}
418442
}
419443

420444
pub struct TaskGuardImpl<'a, B: BackingStorage> {

0 commit comments

Comments
 (0)