Repository: vercel/next.js
Pull request: Turbopack: remove lazy invalidated tasks

### What?

Lazy invalidation is no longer used with the new backend

Closes PACK-5468

Changed files:
filename: turbopack/crates/turbo-tasks-testing/src/lib.rs
patch: @@ -157,10 +157,6 @@ impl TurboTasksApi for VcStorage {
         // ignore
     }
 
-    fn notify_scheduled_tasks(&self) {
-        // ignore
-    }
-
     fn try_read_task_output(
         &self,
         id: TaskId,

filename: turbopack/crates/turbo-tasks/src/macro_helpers.rs
patch: @@ -14,7 +14,7 @@ use crate::{
 pub use crate::{
     global_name, inventory_submit,
     magic_any::MagicAny,
-    manager::{find_cell_by_type, notify_scheduled_tasks, spawn_detached_for_testing},
+    manager::{find_cell_by_type, spawn_detached_for_testing},
     native_function::{
         CollectableFunction, FunctionMeta, NativeFunction, downcast_args_owned, downcast_args_ref,
     },

filename: turbopack/crates/turbo-tasks/src/manager.rs
patch: @@ -15,15 +15,14 @@ use anyhow::{Result, anyhow};
 use auto_hash_map::AutoMap;
 use rustc_hash::FxHasher;
 use serde::{Deserialize, Serialize};
-use smallvec::SmallVec;
 use tokio::{select, sync::mpsc::Receiver, task_local};
 use tokio_util::task::TaskTracker;
-use tracing::{Instrument, Level, instrument, trace_span};
+use tracing::{Instrument, Level, instrument};
 
 use crate::{
     Completion, InvalidationReason, InvalidationReasonSet, OutputContent, ReadCellOptions,
-    ResolvedVc, SharedReference, TaskId, TaskIdSet, TraitMethod, ValueTypeId, Vc, VcRead,
-    VcValueTrait, VcValueType,
+    ResolvedVc, SharedReference, TaskId, TraitMethod, ValueTypeId, Vc, VcRead, VcValueTrait,
+    VcValueType,
     backend::{
         Backend, CachedTaskType, CellContent, TaskCollectiblesMap, TaskExecutionSpec,
         TransientTaskType, TurboTasksExecutionError, TypedCellContent,
@@ -102,10 +101,6 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
 
     fn invalidate_serialization(&self, task: TaskId);
 
-    /// Eagerly notifies all tasks that were scheduled for notifications via
-    /// `schedule_notify_tasks_set()`
-    fn notify_scheduled_tasks(&self);
-
     fn try_read_task_output(
         &self,
         task: TaskId,
@@ -264,14 +259,6 @@ pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync +
     /// idle even with active background jobs.
     fn schedule_backend_background_job(&self, job: B::BackendJob);
 
-    /// Enqueues tasks for notification of changed dependencies. This will
-    /// eventually call `invalidate_tasks()` on all tasks.
-    fn schedule_notify_tasks(&self, tasks: &[TaskId]);
-
-    /// Enqueues tasks for notification of changed dependencies. This will
-    /// eventually call `invalidate_tasks()` on all tasks.
-    fn schedule_notify_tasks_set(&self, tasks: &TaskIdSet);
-
     /// Returns the duration from the start of the program to the given instant.
     fn program_duration_until(&self, instant: Instant) -> Duration;
 
@@ -403,11 +390,6 @@ struct CurrentTaskState {
     task_id: TaskId,
     execution_id: ExecutionId,
 
-    /// Affected tasks, that are tracked during task execution. These tasks will
-    /// be invalidated when the execution finishes or before reading a cell
-    /// value.
-    tasks_to_notify: SmallVec<[TaskId; 4]>,
-
     /// True if the current task has state in cells
     stateful: bool,
 
@@ -439,7 +421,6 @@ impl CurrentTaskState {
         Self {
             task_id,
             execution_id,
-            tasks_to_notify: SmallVec::new(),
             stateful: false,
             has_invalidator: false,
             cell_counters: Some(AutoMap::default()),
@@ -749,18 +730,15 @@ impl<B: Backend + 'static> TurboTasks<B> {
                         } = this.finish_current_task_state();
                         let cell_counters = CURRENT_TASK_STATE
                             .with(|ts| ts.write().unwrap().cell_counters.take().unwrap());
-                        let schedule_again = this.backend.task_execution_completed(
+                        this.backend.task_execution_completed(
                             task_id,
                             duration,
                             alloc_info.memory_usage(),
                             &cell_counters,
                             stateful,
                             has_invalidator,
                             &*this,
-                        );
-                        // task_execution_completed might need to notify tasks
-                        this.notify_scheduled_tasks();
-                        schedule_again
+                        )
                     }
                     .instrument(span)
                     .await
@@ -1123,19 +1101,15 @@ impl<B: Backend + 'static> TurboTasks<B> {
     }
 
     fn finish_current_task_state(&self) -> FinishedTaskState {
-        let (stateful, has_invalidator, tasks) = CURRENT_TASK_STATE.with(|cell| {
+        let (stateful, has_invalidator) = CURRENT_TASK_STATE.with(|cell| {
             let CurrentTaskState {
-                tasks_to_notify,
                 stateful,
                 has_invalidator,
                 ..
             } = &mut *cell.write().unwrap();
-            (*stateful, *has_invalidator, take(tasks_to_notify))
+            (*stateful, *has_invalidator)
         });
 
-        if !tasks.is_empty() {
-            self.backend.invalidate_tasks(&tasks, self);
-        }
         FinishedTaskState {
             stateful,
             has_invalidator,
@@ -1245,21 +1219,6 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
         self.backend.invalidate_serialization(task, self);
     }
 
-    fn notify_scheduled_tasks(&self) {
-        let _ = CURRENT_TASK_STATE.try_with(|cell| {
-            let tasks = {
-                let CurrentTaskState {
-                    tasks_to_notify, ..
-                } = &mut *cell.write().unwrap();
-                take(tasks_to_notify)
-            };
-            if tasks.is_empty() {
-                return;
-            }
-            self.backend.invalidate_tasks(&tasks, self);
-        });
-    }
-
     fn try_read_task_output(
         &self,
         task: TaskId,
@@ -1475,36 +1434,6 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
         })
     }
 
-    /// Enqueues tasks for notification of changed dependencies. This will
-    /// eventually call `dependent_cell_updated()` on all tasks.
-    fn schedule_notify_tasks(&self, tasks: &[TaskId]) {
-        let result = CURRENT_TASK_STATE.try_with(|cell| {
-            let CurrentTaskState {
-                tasks_to_notify, ..
-            } = &mut *cell.write().unwrap();
-            tasks_to_notify.extend(tasks.iter().copied());
-        });
-        if result.is_err() {
-            let _guard = trace_span!("schedule_notify_tasks", count = tasks.len()).entered();
-            self.backend.invalidate_tasks(tasks, self);
-        }
-    }
-
-    /// Enqueues tasks for notification of changed dependencies. This will
-    /// eventually call `dependent_cell_updated()` on all tasks.
-    fn schedule_notify_tasks_set(&self, tasks: &TaskIdSet) {
-        let result = CURRENT_TASK_STATE.try_with(|cell| {
-            let CurrentTaskState {
-                tasks_to_notify, ..
-            } = &mut *cell.write().unwrap();
-            tasks_to_notify.extend(tasks.iter().copied());
-        });
-        if result.is_err() {
-            let _guard = trace_span!("schedule_notify_tasks_set", count = tasks.len()).entered();
-            self.backend.invalidate_tasks_set(tasks, self);
-        };
-    }
-
     #[track_caller]
     fn schedule(&self, task: TaskId) {
         self.schedule(task)
@@ -1730,11 +1659,6 @@ pub fn prevent_gc() {
     mark_stateful();
 }
 
-/// Notifies scheduled tasks for execution.
-pub fn notify_scheduled_tasks() {
-    with_turbo_tasks(|tt| tt.notify_scheduled_tasks())
-}
-
 pub fn emit<T: VcValueTrait + ?Sized>(collectible: ResolvedVc<T>) {
     with_turbo_tasks(|tt| {
         let raw_vc = collectible.node.node;

filename: turbopack/crates/turbo-tasks/src/raw_vc.rs
patch: @@ -166,7 +166,6 @@ impl RawVc {
         conditional: impl FnOnce(ValueTypeId) -> (bool, Option<&'static ValueType>),
     ) -> Result<Option<RawVc>, ResolveTypeError> {
         let tt = turbo_tasks();
-        tt.notify_scheduled_tasks();
         let mut current = self;
         loop {
             match current {
@@ -211,14 +210,9 @@ impl RawVc {
     async fn resolve_inner(self, mut consistency: ReadConsistency) -> Result<RawVc> {
         let tt = turbo_tasks();
         let mut current = self;
-        let mut notified = false;
         loop {
             match current {
                 RawVc::TaskOutput(task) => {
-                    if !notified {
-                        tt.notify_scheduled_tasks();
-                        notified = true;
-                    }
                     current = read_task_output(&*tt, task, consistency).await?;
                     // We no longer need to read strongly consistent, as any Vc returned
                     // from the first task will be inside of the scope of the first
@@ -302,7 +296,6 @@ impl CollectiblesSource for RawVc {
             );
         };
         let tt = turbo_tasks();
-        tt.notify_scheduled_tasks();
         let map = tt.read_task_collectibles(task_id, T::get_trait_type_id());
         map.into_iter()
             .filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
@@ -317,7 +310,6 @@ impl CollectiblesSource for RawVc {
             );
         };
         let tt = turbo_tasks();
-        tt.notify_scheduled_tasks();
         let map = tt.read_task_collectibles(task_id, T::get_trait_type_id());
         tt.unemit_collectibles(T::get_trait_type_id(), &map);
         map.into_iter()
@@ -368,7 +360,6 @@ impl Future for ReadRawVcFuture {
 
     fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
         with_turbo_tasks(|tt| {
-            tt.notify_scheduled_tasks();
             // SAFETY: we are not moving this
             let this = unsafe { self.get_unchecked_mut() };
             'outer: loop {