Skip to content

Commit d6e7503

Browse files
authored
[Turbopack] new backend cleanup (#71132)
### What? review comments from #70945
1 parent fd552c5 commit d6e7503

File tree

7 files changed

+67
-41
lines changed

7 files changed

+67
-41
lines changed

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl TurboTasksBackendInner {
358358

359359
// Check the dirty count of the root node
360360
let dirty_tasks = get!(task, AggregatedDirtyContainerCount)
361-
.copied()
361+
.cloned()
362362
.unwrap_or_default()
363363
.get(self.session_id);
364364
if dirty_tasks > 0 || is_dirty {
@@ -375,7 +375,14 @@ impl TurboTasksBackendInner {
375375
value: RootState::new(ActiveType::CachedActiveUntilClean, task_id),
376376
});
377377
// A newly added AggregateRoot need to make sure to schedule the tasks
378-
task_ids_to_schedule = get_many!(task, AggregatedDirtyContainer { task } count if count.get(self.session_id) > 0 => task);
378+
task_ids_to_schedule = get_many!(
379+
task,
380+
AggregatedDirtyContainer {
381+
task
382+
} count if count.get(self.session_id) > 0 => {
383+
*task
384+
}
385+
);
379386
if is_dirty {
380387
task_ids_to_schedule.push(task_id);
381388
}
@@ -1076,7 +1083,7 @@ impl TurboTasksBackendInner {
10761083
// handle cell counters: update max index and remove cells that are no longer used
10771084
let mut removed_cells = HashMap::new();
10781085
let mut old_counters: HashMap<_, _> =
1079-
get_many!(task, CellTypeMaxIndex { cell_type } max_index => (cell_type, max_index));
1086+
get_many!(task, CellTypeMaxIndex { cell_type } max_index => (*cell_type, *max_index));
10801087
for (&cell_type, &max_index) in cell_counters.iter() {
10811088
if let Some(old_max_index) = old_counters.remove(&cell_type) {
10821089
if old_max_index != max_index {
@@ -1234,7 +1241,7 @@ impl TurboTasksBackendInner {
12341241

12351242
let data_update = if old_dirty_state.is_some() || new_dirty_state.is_some() {
12361243
let mut dirty_containers = get!(task, AggregatedDirtyContainerCount)
1237-
.copied()
1244+
.cloned()
12381245
.unwrap_or_default();
12391246
if let Some(old_dirty_state) = old_dirty_state {
12401247
dirty_containers.update_with_dirty_state(&old_dirty_state);
@@ -1245,7 +1252,7 @@ impl TurboTasksBackendInner {
12451252
(None, Some(new)) => dirty_containers.update_with_dirty_state(&new),
12461253
(Some(old), Some(new)) => dirty_containers.replace_dirty_state(&old, &new),
12471254
};
1248-
if !aggregated_update.is_default() {
1255+
if !aggregated_update.is_zero() {
12491256
if aggregated_update.get(self.session_id) < 0 {
12501257
if let Some(root_state) = get!(task, AggregateRoot) {
12511258
root_state.all_clean_event.notify(usize::MAX);
@@ -1403,7 +1410,7 @@ impl TurboTasksBackendInner {
14031410
task,
14041411
AggregatedCollectible {
14051412
collectible
1406-
} count if collectible.collectible_type == collectible_type && count > 0 => {
1413+
} count if collectible.collectible_type == collectible_type && *count > 0 => {
14071414
collectible.cell
14081415
}
14091416
) {
@@ -1416,7 +1423,7 @@ impl TurboTasksBackendInner {
14161423
Collectible {
14171424
collectible
14181425
} count if collectible.collectible_type == collectible_type => {
1419-
(collectible.cell, count)
1426+
(collectible.cell, *count)
14201427
}
14211428
) {
14221429
*collectibles

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ fn get_followers_with_aggregation_number(
3131
aggregation_number: u32,
3232
) -> Vec<TaskId> {
3333
if is_aggregating_node(aggregation_number) {
34-
get_many!(task, Follower { task } count if count > 0 => task)
34+
get_many!(task, Follower { task } count if *count > 0 => *task)
3535
} else {
36-
get_many!(task, Child { task } => task)
36+
get_many!(task, Child { task } => *task)
3737
}
3838
}
3939

@@ -42,11 +42,11 @@ fn get_followers(task: &TaskGuard<'_>) -> Vec<TaskId> {
4242
}
4343

4444
pub fn get_uppers(task: &TaskGuard<'_>) -> Vec<TaskId> {
45-
get_many!(task, Upper { task } count if count > 0 => task)
45+
get_many!(task, Upper { task } count if *count > 0 => *task)
4646
}
4747

4848
fn iter_uppers<'a>(task: &'a TaskGuard<'a>) -> impl Iterator<Item = TaskId> + 'a {
49-
iter_many!(task, Upper { task } count if count > 0 => task)
49+
iter_many!(task, Upper { task } count if *count > 0 => *task)
5050
}
5151

5252
pub fn get_aggregation_number(task: &TaskGuard<'_>) -> u32 {
@@ -126,17 +126,17 @@ impl AggregatedDataUpdate {
126126
let aggregation = get_aggregation_number(task);
127127
let mut dirty_container_count = Default::default();
128128
let mut collectibles_update: Vec<_> =
129-
get_many!(task, Collectible { collectible } => (collectible, 1));
129+
get_many!(task, Collectible { collectible } => (*collectible, 1));
130130
if is_aggregating_node(aggregation) {
131131
dirty_container_count = get!(task, AggregatedDirtyContainerCount)
132-
.copied()
132+
.cloned()
133133
.unwrap_or_default();
134134
let collectibles = iter_many!(
135135
task,
136136
AggregatedCollectible {
137137
collectible
138-
} count if count > 0 => {
139-
collectible
138+
} count if *count > 0 => {
139+
*collectible
140140
}
141141
);
142142
for collectible in collectibles {
@@ -148,7 +148,7 @@ impl AggregatedDataUpdate {
148148
}
149149

150150
let mut result = Self::new().collectibles_update(collectibles_update);
151-
if !dirty_container_count.is_default() {
151+
if !dirty_container_count.is_zero() {
152152
let DirtyContainerCount {
153153
count,
154154
count_in_session,
@@ -170,7 +170,7 @@ impl AggregatedDataUpdate {
170170
collectibles_update,
171171
} = &mut self;
172172
if let Some((_, value)) = dirty_container_update.as_mut() {
173-
*value = value.invert()
173+
*value = value.negate()
174174
}
175175
for (_, value) in collectibles_update.iter_mut() {
176176
*value = -*value;
@@ -199,7 +199,7 @@ impl AggregatedDataUpdate {
199199
})
200200
}
201201

202-
let mut aggregated_update = Default::default();
202+
let mut aggregated_update = DirtyContainerCount::default();
203203
update!(
204204
task,
205205
AggregatedDirtyContainer {
@@ -208,7 +208,7 @@ impl AggregatedDataUpdate {
208208
|old: Option<DirtyContainerCount>| {
209209
let mut new = old.unwrap_or_default();
210210
aggregated_update = new.update_count(count);
211-
(!new.is_default()).then_some(new)
211+
(!new.is_zero()).then_some(new)
212212
}
213213
);
214214

@@ -225,10 +225,10 @@ impl AggregatedDataUpdate {
225225
if let Some(dirty_state) = dirty_state {
226226
new.undo_update_with_dirty_state(&dirty_state);
227227
}
228-
if !aggregated_update.is_default() {
228+
if !aggregated_update.is_zero() {
229229
result.dirty_container_update = Some((task_id, aggregated_update));
230230
}
231-
(!new.is_default()).then_some(new)
231+
(!new.is_zero()).then_some(new)
232232
});
233233
if let Some((_, count)) = result.dirty_container_update.as_ref() {
234234
if count.get(session_id) < 0 {
@@ -269,8 +269,8 @@ impl AggregatedDataUpdate {
269269
CollectiblesDependent {
270270
collectible_type,
271271
task,
272-
} if collectible_type == ty => {
273-
task
272+
} if *collectible_type == ty => {
273+
*task
274274
}
275275
);
276276
if !dependent.is_empty() {
@@ -608,7 +608,7 @@ impl AggregationUpdateQueue {
608608
value: RootState::new(ActiveType::CachedActiveUntilClean, task_id),
609609
});
610610
}
611-
let dirty_containers: Vec<_> = get_many!(task, AggregatedDirtyContainer { task } count if count.get(session_id) > 0 => task);
611+
let dirty_containers: Vec<_> = get_many!(task, AggregatedDirtyContainer { task } count if count.get(session_id) > 0 => *task);
612612
if !dirty_containers.is_empty() {
613613
self.push(AggregationUpdateJob::FindAndScheduleDirty {
614614
task_ids: dirty_containers,
@@ -954,7 +954,7 @@ impl AggregationUpdateQueue {
954954
if !is_aggregating_node(old) && is_aggregating_node(aggregation_number) {
955955
// When converted from leaf to aggregating node, all children become
956956
// followers
957-
let children: Vec<_> = get_many!(task, Child { task } => task);
957+
let children: Vec<_> = get_many!(task, Child { task } => *task);
958958
for child_id in children {
959959
task.add_new(CachedDataItem::Follower {
960960
task: child_id,
@@ -966,7 +966,7 @@ impl AggregationUpdateQueue {
966966
if is_aggregating_node(aggregation_number) {
967967
// followers might become inner nodes when the aggregation number is
968968
// increased
969-
let followers = iter_many!(task, Follower { task } count if count > 0 => task);
969+
let followers = iter_many!(task, Follower { task } count if *count > 0 => *task);
970970
for follower_id in followers {
971971
self.push(AggregationUpdateJob::BalanceEdge {
972972
upper_id: task_id,
@@ -978,7 +978,7 @@ impl AggregationUpdateQueue {
978978
self.push(AggregationUpdateJob::BalanceEdge { upper_id, task_id });
979979
}
980980
} else {
981-
let children = iter_many!(task, Child { task } => task);
981+
let children = iter_many!(task, Child { task } => *task);
982982
for child_id in children {
983983
self.push(AggregationUpdateJob::UpdateAggregationNumber {
984984
task_id: child_id,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,23 @@ pub fn make_task_dirty_internal(
112112
}) => {
113113
// Got dirty in that one session only
114114
let mut dirty_container = get!(task, AggregatedDirtyContainerCount)
115-
.copied()
115+
.cloned()
116116
.unwrap_or_default();
117117
dirty_container.update_session_dependent(session_id, 1);
118118
dirty_container
119119
}
120120
None => {
121121
// Get dirty for all sessions
122122
get!(task, AggregatedDirtyContainerCount)
123-
.copied()
123+
.cloned()
124124
.unwrap_or_default()
125125
}
126126
_ => unreachable!(),
127127
};
128128
let aggregated_update = dirty_container.update_with_dirty_state(&DirtyState {
129129
clean_in_session: None,
130130
});
131-
if !aggregated_update.is_default() {
131+
if !aggregated_update.is_zero() {
132132
queue.extend(AggregationUpdateJob::data_update(
133133
task,
134134
AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ impl UpdateCellOperation {
4040

4141
let dependent = get_many!(
4242
task,
43-
CellDependent { cell: dependent_cell, task } _value
44-
if dependent_cell == cell
45-
=> task
43+
CellDependent { cell: dependent_cell, task }
44+
if *dependent_cell == cell
45+
=> *task
4646
);
4747

4848
drop(task);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ impl UpdateOutputOperation {
103103
value: output_value,
104104
});
105105

106-
let dependent_tasks = get_many!(task, OutputDependent { task } => task);
107-
let children = get_many!(task, Child { task } => task);
106+
let dependent_tasks = get_many!(task, OutputDependent { task } => *task);
107+
let children = get_many!(task, Child { task } => *task);
108108

109109
let mut queue = AggregationUpdateQueue::new();
110110

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ macro_rules! iter_many {
435435
$task
436436
.iter($crate::data::indicies::$key)
437437
.filter_map(|(key, _)| match key {
438-
&$crate::data::CachedDataItemKey::$key $key_pattern $(if $cond)? => Some(
438+
$crate::data::CachedDataItemKey::$key $key_pattern $(if $cond)? => Some(
439439
$iter_item
440440
),
441441
_ => None,
@@ -446,8 +446,8 @@ macro_rules! iter_many {
446446
.iter($crate::data::indicies::$key)
447447
.filter_map(|(key, value)| match (key, value) {
448448
(
449-
&$crate::data::CachedDataItemKey::$key $input,
450-
&$crate::data::CachedDataItemValue::$key { value: $value_pattern }
449+
$crate::data::CachedDataItemKey::$key $input,
450+
$crate::data::CachedDataItemValue::$key { value: $value_pattern }
451451
) $(if $cond)? => Some($iter_item),
452452
_ => None,
453453
})

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,18 @@ fn add_with_diff(v: &mut i32, u: i32) -> i32 {
108108
}
109109
}
110110

111-
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
111+
/// Represents a count of dirty containers. Since dirtyness can be session dependent, there might be
112+
/// a different count for a specific session. It only need to store the highest session count, since
113+
/// old sessions can't be visited again, so we can ignore their counts.
114+
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
112115
pub struct DirtyContainerCount {
113116
pub count: i32,
114117
pub count_in_session: Option<(SessionId, i32)>,
115118
}
116119

117120
impl DirtyContainerCount {
121+
/// Get the count for a specific session. It's only expected to be asked for the current
122+
/// session, since old session counts might be dropped.
118123
pub fn get(&self, session: SessionId) -> i32 {
119124
if let Some((s, count)) = self.count_in_session {
120125
if s == session {
@@ -124,13 +129,16 @@ impl DirtyContainerCount {
124129
self.count
125130
}
126131

132+
/// Increase/decrease the count by the given value.
127133
pub fn update(&mut self, count: i32) -> DirtyContainerCount {
128134
self.update_count(&DirtyContainerCount {
129135
count,
130136
count_in_session: None,
131137
})
132138
}
133139

140+
/// Increase/decrease the count by the given value, but does not update the count for a specific
141+
/// session. This matches the "dirty, but clean in one session" behavior.
134142
pub fn update_session_dependent(
135143
&mut self,
136144
ignore_session: SessionId,
@@ -142,6 +150,10 @@ impl DirtyContainerCount {
142150
})
143151
}
144152

153+
/// Adds the `count` to the current count. This correctly handles session dependent counts.
154+
/// Returns a new count object that represents the aggregated count. The aggregated count will
155+
/// be +1 when the self count changes from <= 0 to > 0 and -1 when the self count changes from >
156+
/// 0 to <= 0. The same for the session dependent count.
145157
pub fn update_count(&mut self, count: &DirtyContainerCount) -> DirtyContainerCount {
146158
let mut diff = DirtyContainerCount::default();
147159
match (
@@ -181,6 +193,7 @@ impl DirtyContainerCount {
181193
diff
182194
}
183195

196+
/// Applies a dirty state to the count. Returns an aggregated count that represents the change.
184197
pub fn update_with_dirty_state(&mut self, dirty: &DirtyState) -> DirtyContainerCount {
185198
if let Some(clean_in_session) = dirty.clean_in_session {
186199
self.update_session_dependent(clean_in_session, 1)
@@ -189,6 +202,8 @@ impl DirtyContainerCount {
189202
}
190203
}
191204

205+
/// Undoes the effect of a dirty state on the count. Returns an aggregated count that represents
206+
/// the change.
192207
pub fn undo_update_with_dirty_state(&mut self, dirty: &DirtyState) -> DirtyContainerCount {
193208
if let Some(clean_in_session) = dirty.clean_in_session {
194209
self.update_session_dependent(clean_in_session, -1)
@@ -197,6 +212,8 @@ impl DirtyContainerCount {
197212
}
198213
}
199214

215+
/// Replaces the old dirty state with the new one. Returns an aggregated count that represents
216+
/// the change.
200217
pub fn replace_dirty_state(
201218
&mut self,
202219
old: &DirtyState,
@@ -207,11 +224,13 @@ impl DirtyContainerCount {
207224
diff
208225
}
209226

210-
pub fn is_default(&self) -> bool {
227+
/// Returns true if the count is zero and appling it would have no effect
228+
pub fn is_zero(&self) -> bool {
211229
self.count == 0 && self.count_in_session.map(|(_, c)| c == 0).unwrap_or(true)
212230
}
213231

214-
pub fn invert(&self) -> Self {
232+
/// Negates the counts.
233+
pub fn negate(&self) -> Self {
215234
Self {
216235
count: -self.count,
217236
count_in_session: self.count_in_session.map(|(s, c)| (s, -c)),

0 commit comments

Comments
 (0)