Skip to content

Commit 7ed41a4

Browse files
committed
remove old edges when task recomputation completes
1 parent ab49270 commit 7ed41a4

File tree

4 files changed

+223
-8
lines changed

4 files changed

+223
-8
lines changed

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

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use anyhow::Result;
1919
use auto_hash_map::{AutoMap, AutoSet};
2020
use dashmap::DashMap;
2121
pub use operation::AnyOperation;
22-
use operation::ConnectChildOperation;
22+
use operation::{CleanupOldEdgesOperation, ConnectChildOperation, OutdatedEdge};
2323
use parking_lot::{Condvar, Mutex};
2424
use rustc_hash::FxHasher;
2525
use smallvec::smallvec;
@@ -277,13 +277,16 @@ impl TurboTasksBackend {
277277
});
278278
drop(task);
279279
let mut reader_task = ctx.task(reader);
280-
reader_task.add(CachedDataItem::CellDependency {
281-
target: CellRef {
282-
task: task_id,
283-
cell,
284-
},
285-
value: (),
286-
});
280+
let target = CellRef {
281+
task: task_id,
282+
cell,
283+
};
284+
if reader_task
285+
.remove(&CachedDataItemKey::OutdatedCellDependency { target })
286+
.is_none()
287+
{
288+
reader_task.add(CachedDataItem::CellDependency { target, value: () });
289+
}
287290
}
288291
return Ok(Ok(CellContent(Some(content)).into_typed(cell.type_id)));
289292
}
@@ -413,8 +416,95 @@ impl Backend for TurboTasksBackend {
413416
done_event,
414417
},
415418
});
419+
420+
// Make all current children outdated (remove left-over outdated children)
421+
enum Child {
422+
Current(TaskId),
423+
Outdated(TaskId),
424+
}
425+
let children = task
426+
.iter()
427+
.filter_map(|(key, _)| match *key {
428+
CachedDataItemKey::Child { task } => Some(Child::Current(task)),
429+
CachedDataItemKey::OutdatedChild { task } => Some(Child::Outdated(task)),
430+
_ => None,
431+
})
432+
.collect::<Vec<_>>();
433+
for child in children {
434+
match child {
435+
Child::Current(child) => {
436+
task.add(CachedDataItem::OutdatedChild {
437+
task: child,
438+
value: (),
439+
});
440+
}
441+
Child::Outdated(child) => {
442+
if !task.has_key(&CachedDataItemKey::Child { task: child }) {
443+
task.remove(&CachedDataItemKey::OutdatedChild { task: child });
444+
}
445+
}
446+
}
447+
}
448+
449+
// Make all dependencies outdated
450+
enum Dep {
451+
CurrentCell(CellRef),
452+
CurrentOutput(TaskId),
453+
OutdatedCell(CellRef),
454+
OutdatedOutput(TaskId),
455+
}
456+
let dependencies = task
457+
.iter()
458+
.filter_map(|(key, _)| match *key {
459+
CachedDataItemKey::CellDependency { target } => Some(Dep::CurrentCell(target)),
460+
CachedDataItemKey::OutputDependency { target } => {
461+
Some(Dep::CurrentOutput(target))
462+
}
463+
CachedDataItemKey::OutdatedCellDependency { target } => {
464+
Some(Dep::OutdatedCell(target))
465+
}
466+
CachedDataItemKey::OutdatedOutputDependency { target } => {
467+
Some(Dep::OutdatedOutput(target))
468+
}
469+
_ => None,
470+
})
471+
.collect::<Vec<_>>();
472+
for dep in dependencies {
473+
match dep {
474+
Dep::CurrentCell(cell) => {
475+
task.add(CachedDataItem::OutdatedCellDependency {
476+
target: cell,
477+
value: (),
478+
});
479+
}
480+
Dep::CurrentOutput(output) => {
481+
task.add(CachedDataItem::OutdatedOutputDependency {
482+
target: output,
483+
value: (),
484+
});
485+
}
486+
Dep::OutdatedCell(cell) => {
487+
if !task.has_key(&CachedDataItemKey::CellDependency { target: cell }) {
488+
task.remove(&CachedDataItemKey::OutdatedCellDependency {
489+
target: cell,
490+
});
491+
}
492+
}
493+
Dep::OutdatedOutput(output) => {
494+
if !task.has_key(&CachedDataItemKey::OutputDependency { target: output }) {
495+
task.remove(&CachedDataItemKey::OutdatedOutputDependency {
496+
target: output,
497+
});
498+
}
499+
}
500+
}
501+
}
502+
503+
// TODO: Make all collectibles outdated
504+
416505
start_event.notify(usize::MAX);
417506
}
507+
418508
let (span, future) = if let Some(task_type) = self.task_cache.lookup_reverse(&task_id) {
419509
match &*task_type {
420510
CachedTaskType::Native { fn_type, this, arg } => (
@@ -536,8 +626,27 @@ impl Backend for TurboTasksBackend {
536626
done_event,
537627
},
538628
});
629+
drop(task);
630+
drop(ctx);
539631
} else {
632+
let old_edges = task
633+
.iter()
634+
.filter_map(|(key, _)| match *key {
635+
CachedDataItemKey::OutdatedChild { task } => Some(OutdatedEdge::Child(task)),
636+
CachedDataItemKey::OutdatedCellDependency { target } => {
637+
Some(OutdatedEdge::CellDependency(target))
638+
}
639+
CachedDataItemKey::OutdatedOutputDependency { target } => {
640+
Some(OutdatedEdge::OutputDependency(target))
641+
}
642+
_ => None,
643+
})
644+
.collect::<Vec<_>>();
645+
540646
done_event.notify(usize::MAX);
647+
drop(task);
648+
649+
CleanupOldEdgesOperation::run(task_id, old_edges, ctx);
541650
}
542651

543652
stale
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use serde::{Deserialize, Serialize};
2+
use turbo_tasks::TaskId;
3+
4+
use super::{ExecuteContext, Operation};
5+
use crate::data::{CachedDataItemKey, CellRef};
6+
7+
#[derive(Serialize, Deserialize, Clone, Default)]
8+
pub enum CleanupOldEdgesOperation {
9+
RemoveEdges {
10+
task_id: TaskId,
11+
outdated: Vec<OutdatedEdge>,
12+
},
13+
#[default]
14+
Done,
15+
// TODO Add aggregated edge
16+
}
17+
18+
#[derive(Serialize, Deserialize, Clone)]
19+
pub enum OutdatedEdge {
20+
Child(TaskId),
21+
CellDependency(CellRef),
22+
OutputDependency(TaskId),
23+
}
24+
25+
impl CleanupOldEdgesOperation {
26+
pub fn run(task_id: TaskId, outdated: Vec<OutdatedEdge>, ctx: ExecuteContext<'_>) {
27+
CleanupOldEdgesOperation::RemoveEdges { task_id, outdated }.execute(&ctx);
28+
}
29+
}
30+
31+
impl Operation for CleanupOldEdgesOperation {
32+
fn execute(mut self, ctx: &ExecuteContext<'_>) {
33+
loop {
34+
ctx.operation_suspend_point(&self);
35+
match self {
36+
CleanupOldEdgesOperation::RemoveEdges {
37+
task_id,
38+
ref mut outdated,
39+
} => {
40+
if let Some(edge) = outdated.pop() {
41+
match edge {
42+
OutdatedEdge::Child(child_id) => {
43+
let mut task = ctx.task(task_id);
44+
task.remove(&CachedDataItemKey::Child { task: child_id });
45+
// TODO remove aggregated edge
46+
}
47+
OutdatedEdge::CellDependency(CellRef {
48+
task: cell_task_id,
49+
cell,
50+
}) => {
51+
{
52+
let mut task = ctx.task(cell_task_id);
53+
task.remove(&CachedDataItemKey::CellDependent {
54+
cell,
55+
task: task_id,
56+
});
57+
}
58+
{
59+
let mut task = ctx.task(task_id);
60+
task.remove(&CachedDataItemKey::CellDependency {
61+
target: CellRef {
62+
task: cell_task_id,
63+
cell,
64+
},
65+
});
66+
}
67+
}
68+
OutdatedEdge::OutputDependency(output_task_id) => {
69+
{
70+
let mut task = ctx.task(output_task_id);
71+
task.remove(&CachedDataItemKey::OutputDependent {
72+
task: task_id,
73+
});
74+
}
75+
{
76+
let mut task = ctx.task(task_id);
77+
task.remove(&CachedDataItemKey::OutputDependency {
78+
target: output_task_id,
79+
});
80+
}
81+
}
82+
}
83+
}
84+
85+
if outdated.is_empty() {
86+
self = CleanupOldEdgesOperation::Done;
87+
}
88+
continue;
89+
}
90+
CleanupOldEdgesOperation::Done => {
91+
return;
92+
}
93+
}
94+
}
95+
}
96+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod cleanup_old_edges;
12
mod connect_child;
23
mod invalidate;
34
mod update_cell;
@@ -234,11 +235,14 @@ macro_rules! impl_operation {
234235
pub enum AnyOperation {
235236
ConnectChild(connect_child::ConnectChildOperation),
236237
Invalidate(invalidate::InvalidateOperation),
238+
CleanupOldEdges(cleanup_old_edges::CleanupOldEdgesOperation),
237239
Nested(Vec<AnyOperation>),
238240
}
239241

240242
impl_operation!(ConnectChild connect_child::ConnectChildOperation);
241243
impl_operation!(Invalidate invalidate::InvalidateOperation);
244+
impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation);
242245

246+
pub use cleanup_old_edges::OutdatedEdge;
243247
pub use update_cell::UpdateCellOperation;
244248
pub use update_output::UpdateOutputOperation;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ pub enum CachedDataItem {
150150
target: CellRef,
151151
value: (),
152152
},
153+
OutdatedChild {
154+
task: TaskId,
155+
value: (),
156+
},
153157

154158
// Transient Error State
155159
Error {
@@ -183,6 +187,7 @@ impl CachedDataItem {
183187
CachedDataItem::OutdatedCollectible { .. } => false,
184188
CachedDataItem::OutdatedOutputDependency { .. } => false,
185189
CachedDataItem::OutdatedCellDependency { .. } => false,
190+
CachedDataItem::OutdatedChild { .. } => false,
186191
CachedDataItem::Error { .. } => false,
187192
}
188193
}
@@ -224,6 +229,7 @@ impl CachedDataItemKey {
224229
CachedDataItemKey::OutdatedCollectible { .. } => false,
225230
CachedDataItemKey::OutdatedOutputDependency { .. } => false,
226231
CachedDataItemKey::OutdatedCellDependency { .. } => false,
232+
CachedDataItemKey::OutdatedChild { .. } => false,
227233
CachedDataItemKey::Error { .. } => false,
228234
}
229235
}

0 commit comments

Comments
 (0)