Skip to content

Commit 3e74c9e

Browse files
committed
keep children count instead of iterating children every time
1 parent bef280b commit 3e74c9e

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
invalidate::make_task_dirty,
1414
AggregatedDataUpdate, ExecuteContext, Operation,
1515
},
16-
storage::update_count,
16+
storage::{update, update_count},
1717
TaskDataCategory,
1818
},
1919
data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef},
@@ -82,6 +82,11 @@ impl Operation for CleanupOldEdgesOperation {
8282
for &child_id in children.iter() {
8383
task.remove(&CachedDataItemKey::Child { task: child_id });
8484
}
85+
update!(task, ChildrenCount, |count: Option<u32>| {
86+
// If this underflows, we messed up counting somewhere
87+
let count = count.unwrap_or_default() - children.len() as u32;
88+
(count != 0).then_some(count)
89+
});
8590
if is_aggregating_node(get_aggregation_number(&task)) {
8691
queue.push(AggregationUpdateJob::InnerLostFollowers {
8792
upper_ids: vec![task_id],

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use crate::{
1111
},
1212
is_root_node, ExecuteContext, Operation,
1313
},
14-
storage::get,
14+
storage::{get, update},
1515
TaskDataCategory,
1616
},
17-
data::{CachedDataItem, CachedDataItemIndex, CachedDataItemKey},
17+
data::{CachedDataItem, CachedDataItemKey},
1818
};
1919

2020
const AGGREGATION_NUMBER_BUFFER_SPACE: u32 = 2;
@@ -44,6 +44,13 @@ impl ConnectChildOperation {
4444
task: child_task_id,
4545
value: (),
4646
}) {
47+
// Update the children count
48+
let mut children_count = 0;
49+
update!(parent_task, ChildrenCount, |count: Option<u32>| {
50+
children_count = count.unwrap_or_default() + 1;
51+
Some(children_count)
52+
});
53+
4754
// Update the task aggregation
4855
let mut queue = AggregationUpdateQueue::new();
4956

@@ -54,16 +61,6 @@ impl ConnectChildOperation {
5461
let parent_aggregation = if is_root_node(current_parent_aggregation.base) {
5562
u32::MAX
5663
} else {
57-
let children_count = parent_task
58-
.iter(CachedDataItemIndex::Children)
59-
.filter(|(k, _)| {
60-
matches!(
61-
*k,
62-
CachedDataItemKey::Child { .. }
63-
| CachedDataItemKey::OutdatedChild { .. }
64-
)
65-
})
66-
.count();
6764
let target_distance = children_count.ilog2() * 2;
6865
let parent_aggregation = current_parent_aggregation
6966
.base

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ pub enum CachedDataItem {
313313
task: TaskId,
314314
value: (),
315315
},
316+
ChildrenCount {
317+
value: u32,
318+
},
316319

317320
// Cells
318321
CellData {
@@ -438,6 +441,7 @@ impl CachedDataItem {
438441
}
439442
CachedDataItem::Dirty { .. } => true,
440443
CachedDataItem::Child { task, .. } => !task.is_transient(),
444+
CachedDataItem::ChildrenCount { .. } => true,
441445
CachedDataItem::CellData { .. } => true,
442446
CachedDataItem::CellTypeMaxIndex { .. } => true,
443447
CachedDataItem::OutputDependency { target, .. } => !target.is_transient(),
@@ -502,6 +506,7 @@ impl CachedDataItemKey {
502506
}
503507
CachedDataItemKey::Dirty { .. } => true,
504508
CachedDataItemKey::Child { task, .. } => !task.is_transient(),
509+
CachedDataItemKey::ChildrenCount {} => true,
505510
CachedDataItemKey::CellData { .. } => true,
506511
CachedDataItemKey::CellTypeMaxIndex { .. } => true,
507512
CachedDataItemKey::OutputDependency { target, .. } => !target.is_transient(),
@@ -534,6 +539,7 @@ impl CachedDataItemKey {
534539
match self {
535540
CachedDataItemKey::Collectible { .. }
536541
| CachedDataItemKey::Child { .. }
542+
| CachedDataItemKey::ChildrenCount { .. }
537543
| CachedDataItemKey::CellData { .. }
538544
| CachedDataItemKey::CellTypeMaxIndex { .. }
539545
| CachedDataItemKey::OutputDependency { .. }
@@ -678,6 +684,7 @@ impl CachedDataItemValue {
678684
#[derive(Debug)]
679685
pub struct CachedDataUpdate {
680686
pub task: TaskId,
687+
// TODO generate CachedDataItemUpdate to avoid repeating the variant field 3 times
681688
pub key: CachedDataItemKey,
682689
pub value: Option<CachedDataItemValue>,
683690
pub old_value: Option<CachedDataItemValue>,

0 commit comments

Comments
 (0)