Repository: vercel/next.js
File: turbopack/crates/turbo-tasks-backend/src/backend/mod.rs (Rust)

Code before:
        ctx: &mut impl ExecuteContext<'_>,
        task_id: TaskId,
        cell_counters: &AutoMap<ValueTypeId, u32, BuildHasherDefault<FxHasher>, 8>,
        is_error: bool,
        removed_data: &mut Vec<CachedDataItem>,
    ) {
        let mut task = ctx.task(task_id, TaskDataCategory::All);

        // An error is potentially caused by a eventual consistency, so we avoid updating cells
        // after an error as it is likely transient and we want to keep the dependent tasks
        // clean to avoid re-executions.
        if !is_error {
            // Remove no longer existing cells and
            // find all outdated data items (removed cells, outdated edges)
            // Note: For persistent tasks we only want to call extract_if when there are actual
            // cells to remove to avoid tracking that as modification.
            // Note: We do not mark the tasks as dirty here, as these tasks are unused or stale
            // anyway and we want to avoid needless re-executions. When the cells become
            // used again, they are invalidated from the update cell operation.
            if task_id.is_transient()
                || iter_many!(task, CellData { cell }
                    if cell_counters
                        .get(&cell.type_id)
                        .is_none_or(|start_index| cell.index >= *start_index)
                    => cell)
                .count()
                    > 0
            {
                removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| {
                    matches!(key, CachedDataItemKey::CellData { cell } if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index))
                }));
            }
            if task_id.is_transient()
                || iter_many!(task, TransientCellData { cell }
                    if cell_counters
                        .get(&cell.type_id)
                        .is_none_or(|start_index| cell.index >= *start_index)
                    => cell)
                .count()
                    > 0
            {
                removed_data.extend(task.extract_if(
                    CachedDataItemType::TransientCellData,
                    |key, _| {
                        matches!(key, CachedDataItemKey::TransientCellData { cell } if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index))
                    },
                ));
            }
        }

        // Shrink memory usage

Review comment (refactor):
I'm guessing @lukesandberg 's refactoring will help clean this up, but this nesting is really hard to read.

Code after:
                task.remove(&CachedDataItemKey::CurrentSessionClean {});
            }
        }

        // Propagate dirtyness changes
        let data_update = if old_self_dirty != new_self_dirty
            || old_current_session_self_clean != new_current_session_self_clean
        {
            let dirty_container_count = get!(task, AggregatedDirtyContainerCount)
                .cloned()
                .unwrap_or_default();
            let current_session_clean_container_count =
                get!(task, AggregatedCurrentSessionCleanContainerCount)
                    .copied()
                    .unwrap_or_default();
            let result = ComputeDirtyAndCleanUpdate {
                old_dirty_container_count: dirty_container_count,
                new_dirty_container_count: dirty_container_count,
                old_current_session_clean_container_count: current_session_clean_container_count,
                new_current_session_clean_container_count: current_session_clean_container_count,
                old_self_dirty,
                new_self_dirty,
                old_current_session_self_clean,
                new_current_session_self_clean,
            }
            .compute();
            if result.dirty_count_update - result.current_session_clean_update < 0 {
                // The task is clean now
                if let Some(activeness_state) = get_mut!(task, Activeness) {
                    activeness_state.all_clean_event.notify(usize::MAX);
                    activeness_state.unset_active_until_clean();
                    if activeness_state.is_empty() {
                        task.remove(&CachedDataItemKey::Activeness {});
                    }
                }
            }
            result
                .aggregated_update(task_id)
                .and_then(|aggregated_update| {
                    AggregationUpdateJob::data_update(&mut task, aggregated_update)
                })
        } else {
            None
        };

        #[cfg(feature = "verify_determinism")]
        let reschedule = (dirty_changed || no_output_set) && !task_id.is_transient();
        #[cfg(not(feature = "verify_determinism"))]
        let reschedule = false;
        if reschedule {
            task.add_new(CachedDataItem::InProgress {