Skip to content

Commit f5d6b26

Browse files
authored
[Turbopack] add support for collectibles to new backend (#70798)
### What? add support for collectibles to new backend also fix some bugs in the backend: * make in progress tasks as stale * skip updating output for stale tasks * flag task as dirty when they get output set and add and enable all remaining tests
1 parent 967ecb9 commit f5d6b26

File tree

15 files changed

+634
-82
lines changed

15 files changed

+634
-82
lines changed

packages/next/src/build/swc/generated-native.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export interface NapiUpdateInfo {
249249
}
250250
/**
251251
* Subscribes to lifecycle events of the compilation.
252+
*
252253
* Emits an [UpdateMessage::Start] event when any computation starts.
253254
* Emits an [UpdateMessage::End] event when there was no computation for the
254255
* specified time (`aggregation_ms`). The [UpdateMessage::End] event contains

turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs

+8
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ impl<'a, K: Eq + Hash, V, H: BuildHasher + Default + 'a, const I: usize> Entry<'
547547
Entry::Vacant(entry) => entry.insert(default()),
548548
}
549549
}
550+
551+
/// see [HashMap::Entry::or_insert](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.or_insert)
552+
pub fn or_insert(self, default: V) -> &'a mut V {
553+
match self {
554+
Entry::Occupied(entry) => entry.into_mut(),
555+
Entry::Vacant(entry) => entry.insert(default),
556+
}
557+
}
550558
}
551559

552560
impl<'a, K: Eq + Hash, V: Default, H: BuildHasher + Default + 'a, const I: usize>

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

+177-25
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ use crate::{
4343
AggregationUpdateQueue, CleanupOldEdgesOperation, ConnectChildOperation,
4444
ExecuteContext, OutdatedEdge,
4545
},
46-
storage::{get, get_many, remove, Storage},
46+
storage::{get, get_many, iter_many, remove, Storage},
4747
},
4848
backing_storage::{BackingStorage, ReadTransaction},
4949
data::{
5050
ActiveType, AggregationNumber, CachedDataItem, CachedDataItemIndex, CachedDataItemKey,
51-
CachedDataItemValue, CachedDataUpdate, CellRef, InProgressCellState, InProgressState,
52-
OutputValue, RootState,
51+
CachedDataItemValue, CachedDataUpdate, CellRef, CollectibleRef, CollectiblesRef,
52+
InProgressCellState, InProgressState, OutputValue, RootState,
5353
},
5454
utils::{bi_map::BiMap, chunked_vec::ChunkedVec, ptr_eq_arc::PtrEqArc, sharded::Sharded},
5555
};
@@ -361,7 +361,7 @@ impl TurboTasksBackendInner {
361361
// active and this task won't stale. CachedActiveUntilClean
362362
// is automatically removed when this task is clean.
363363
task.add_new(CachedDataItem::AggregateRoot {
364-
value: RootState::new(ActiveType::CachedActiveUntilClean),
364+
value: RootState::new(ActiveType::CachedActiveUntilClean, task_id),
365365
});
366366
get!(task, AggregateRoot).unwrap()
367367
};
@@ -845,6 +845,38 @@ impl TurboTasksBackendInner {
845845
}
846846
}
847847

848+
// Make all current collectibles outdated (remove left-over outdated collectibles)
849+
enum Collectible {
850+
Current(CollectibleRef, i32),
851+
Outdated(CollectibleRef),
852+
}
853+
let collectibles = task
854+
.iter(CachedDataItemIndex::Collectibles)
855+
.filter_map(|(key, value)| match (key, value) {
856+
(
857+
&CachedDataItemKey::Collectible { collectible },
858+
&CachedDataItemValue::Collectible { value },
859+
) => Some(Collectible::Current(collectible, value)),
860+
(&CachedDataItemKey::OutdatedCollectible { collectible }, _) => {
861+
Some(Collectible::Outdated(collectible))
862+
}
863+
_ => None,
864+
})
865+
.collect::<Vec<_>>();
866+
for collectible in collectibles {
867+
match collectible {
868+
Collectible::Current(collectible, value) => {
869+
let _ =
870+
task.insert(CachedDataItem::OutdatedCollectible { collectible, value });
871+
}
872+
Collectible::Outdated(collectible) => {
873+
if !task.has_key(&CachedDataItemKey::Collectible { collectible }) {
874+
task.remove(&CachedDataItemKey::OutdatedCollectible { collectible });
875+
}
876+
}
877+
}
878+
}
879+
848880
// Make all dependencies outdated
849881
enum Dep {
850882
CurrentCell(CellRef),
@@ -898,8 +930,6 @@ impl TurboTasksBackendInner {
898930
}
899931
}
900932
}
901-
902-
// TODO: Make all collectibles outdated
903933
}
904934

905935
let (span, future) = match task_type {
@@ -1096,16 +1126,25 @@ impl TurboTasksBackendInner {
10961126
.collect::<Vec<_>>()
10971127
} else {
10981128
task.iter_all()
1099-
.filter_map(|(key, _)| match *key {
1129+
.filter_map(|(key, value)| match *key {
11001130
CachedDataItemKey::OutdatedChild { task } => {
11011131
Some(OutdatedEdge::Child(task))
11021132
}
1133+
CachedDataItemKey::OutdatedCollectible { collectible } => {
1134+
let CachedDataItemValue::OutdatedCollectible { value } = *value else {
1135+
unreachable!();
1136+
};
1137+
Some(OutdatedEdge::Collectible(collectible, value))
1138+
}
11031139
CachedDataItemKey::OutdatedCellDependency { target } => {
11041140
Some(OutdatedEdge::CellDependency(target))
11051141
}
11061142
CachedDataItemKey::OutdatedOutputDependency { target } => {
11071143
Some(OutdatedEdge::OutputDependency(target))
11081144
}
1145+
CachedDataItemKey::OutdatedCollectiblesDependency { target } => {
1146+
Some(OutdatedEdge::CollectiblesDependency(target))
1147+
}
11091148
CachedDataItemKey::CellDependent { cell, task }
11101149
if removed_cells
11111150
.get(&cell.type_id)
@@ -1132,7 +1171,7 @@ impl TurboTasksBackendInner {
11321171
}
11331172
AggregationUpdateJob::data_update(
11341173
&mut task,
1135-
AggregatedDataUpdate::no_longer_dirty_container(task_id),
1174+
AggregatedDataUpdate::new().no_longer_dirty_container(task_id),
11361175
)
11371176
} else {
11381177
None
@@ -1246,6 +1285,116 @@ impl TurboTasksBackendInner {
12461285
}
12471286
}
12481287

1288+
fn read_task_collectibles(
1289+
&self,
1290+
task_id: TaskId,
1291+
collectible_type: TraitTypeId,
1292+
reader_id: TaskId,
1293+
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend>,
1294+
) -> AutoMap<RawVc, i32, BuildHasherDefault<FxHasher>, 1> {
1295+
let mut ctx = self.execute_context(turbo_tasks);
1296+
let mut collectibles = AutoMap::default();
1297+
{
1298+
let mut task = ctx.task(task_id, TaskDataCategory::Data);
1299+
// Ensure it's an root node
1300+
loop {
1301+
let aggregation_number = get_aggregation_number(&task);
1302+
if is_root_node(aggregation_number) {
1303+
break;
1304+
}
1305+
drop(task);
1306+
AggregationUpdateQueue::run(
1307+
AggregationUpdateJob::UpdateAggregationNumber {
1308+
task_id,
1309+
base_aggregation_number: u32::MAX,
1310+
distance: None,
1311+
},
1312+
&mut ctx,
1313+
);
1314+
task = ctx.task(task_id, TaskDataCategory::All);
1315+
}
1316+
for collectible in iter_many!(task, AggregatedCollectible { collectible } count if collectible.collectible_type == collectible_type && count > 0 => collectible.cell)
1317+
{
1318+
*collectibles
1319+
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
1320+
.or_insert(0) += 1;
1321+
}
1322+
for (collectible, count) in iter_many!(task, Collectible { collectible } count if collectible.collectible_type == collectible_type => (collectible.cell, count))
1323+
{
1324+
*collectibles
1325+
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
1326+
.or_insert(0) += count;
1327+
}
1328+
task.insert(CachedDataItem::CollectiblesDependent {
1329+
collectible_type,
1330+
task: reader_id,
1331+
value: (),
1332+
});
1333+
}
1334+
{
1335+
let mut reader = ctx.task(reader_id, TaskDataCategory::Data);
1336+
let target = CollectiblesRef {
1337+
task: task_id,
1338+
collectible_type,
1339+
};
1340+
if reader.add(CachedDataItem::CollectiblesDependency { target, value: () }) {
1341+
reader.remove(&CachedDataItemKey::OutdatedCollectiblesDependency { target });
1342+
}
1343+
}
1344+
collectibles
1345+
}
1346+
1347+
fn emit_collectible(
1348+
&self,
1349+
collectible_type: TraitTypeId,
1350+
collectible: RawVc,
1351+
task_id: TaskId,
1352+
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend>,
1353+
) {
1354+
let RawVc::TaskCell(collectible_task, cell) = collectible else {
1355+
panic!("Collectibles need to be resolved");
1356+
};
1357+
let cell = CellRef {
1358+
task: collectible_task,
1359+
cell,
1360+
};
1361+
operation::UpdateCollectibleOperation::run(
1362+
task_id,
1363+
CollectibleRef {
1364+
collectible_type,
1365+
cell,
1366+
},
1367+
1,
1368+
self.execute_context(turbo_tasks),
1369+
);
1370+
}
1371+
1372+
fn unemit_collectible(
1373+
&self,
1374+
collectible_type: TraitTypeId,
1375+
collectible: RawVc,
1376+
count: u32,
1377+
task_id: TaskId,
1378+
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend>,
1379+
) {
1380+
let RawVc::TaskCell(collectible_task, cell) = collectible else {
1381+
panic!("Collectibles need to be resolved");
1382+
};
1383+
let cell = CellRef {
1384+
task: collectible_task,
1385+
cell,
1386+
};
1387+
operation::UpdateCollectibleOperation::run(
1388+
task_id,
1389+
CollectibleRef {
1390+
collectible_type,
1391+
cell,
1392+
},
1393+
-(i32::try_from(count).unwrap()),
1394+
self.execute_context(turbo_tasks),
1395+
);
1396+
}
1397+
12491398
fn update_task_cell(
12501399
&self,
12511400
task_id: TaskId,
@@ -1293,7 +1442,7 @@ impl TurboTasksBackendInner {
12931442
},
12941443
});
12951444
task.add(CachedDataItem::AggregateRoot {
1296-
value: RootState::new(root_type),
1445+
value: RootState::new(root_type, task_id),
12971446
});
12981447
task.add(CachedDataItem::new_scheduled(move || match root_type {
12991448
ActiveType::RootTask => "Root Task".to_string(),
@@ -1474,33 +1623,36 @@ impl Backend for TurboTasksBackend {
14741623

14751624
fn read_task_collectibles(
14761625
&self,
1477-
_: TaskId,
1478-
_: TraitTypeId,
1479-
_: TaskId,
1480-
_: &dyn TurboTasksBackendApi<Self>,
1626+
task_id: TaskId,
1627+
collectible_type: TraitTypeId,
1628+
reader: TaskId,
1629+
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
14811630
) -> AutoMap<RawVc, i32, BuildHasherDefault<FxHasher>, 1> {
1482-
todo!()
1631+
self.0
1632+
.read_task_collectibles(task_id, collectible_type, reader, turbo_tasks)
14831633
}
14841634

14851635
fn emit_collectible(
14861636
&self,
1487-
_: TraitTypeId,
1488-
_: RawVc,
1489-
_: TaskId,
1490-
_: &dyn TurboTasksBackendApi<Self>,
1637+
collectible_type: TraitTypeId,
1638+
collectible: RawVc,
1639+
task_id: TaskId,
1640+
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
14911641
) {
1492-
todo!()
1642+
self.0
1643+
.emit_collectible(collectible_type, collectible, task_id, turbo_tasks)
14931644
}
14941645

14951646
fn unemit_collectible(
14961647
&self,
1497-
_: TraitTypeId,
1498-
_: RawVc,
1499-
_: u32,
1500-
_: TaskId,
1501-
_: &dyn TurboTasksBackendApi<Self>,
1648+
collectible_type: TraitTypeId,
1649+
collectible: RawVc,
1650+
count: u32,
1651+
task_id: TaskId,
1652+
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
15021653
) {
1503-
todo!()
1654+
self.0
1655+
.unemit_collectible(collectible_type, collectible, count, task_id, turbo_tasks)
15041656
}
15051657

15061658
fn update_task_cell(

0 commit comments

Comments
 (0)