Unverified Commit 315c8eba authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: skip invalidating a task on cell/output change when the dependency is outdated (#84376)

### What?

Fixes some cases of double invalidation of tasks.

The concrete example here was tailwind executing twice on HMR changes.
The problem was that tailwind has a glob dependency and a file dependency on files.
That leads to two paths of invalidation of the `compute_webpack_loader_evaluation` task.

`compute_webpack_loader_evaluation` has a dependency on the file content and on the glob result. So on a `FileContent` cell and a `Completion` cell.

If the file content changes it invalidates `compute_webpack_loader_evaluation` and `track_glob`. Both start executing.
Shortly after `track_glob` changes the content of the `Completion` cell. This invalidates `compute_webpack_loader_evaluation`. As it has already started executing with the "stale" content of the `Completion` cell, we mark it as stale so it's executed again.

But we are a little bit to eager with the invalidation, because at the time the `Completion` cell has changed we haven't even read the cell from the `compute_webpack_loader_evaluation` task yet. But due to an optimization we keep stale dependencies until the task execution has finished (because they are unlikely to change anyway). But this is still possible to detect as there is a `OutdatedCellDependency` on the task. 

And that's what this PR is doing. When a cell or output is changed, we no longer directly invalidate all dependent tasks.
Instead we check every dependent tasks if it still has a dependency on the cell/output or if that dependency is outdated. If that's the case we do not invalidate the task.

There is a cost of that extra check, but we have to access the task anyway to make it dirty. So the cost is small, compared to the win of not unnecessarily executing a task again.
parent efa1ad49
import './globals.css'
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('no-double-tailwind-execution', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
dependencies: {
'@tailwindcss/postcss': '^4',
tailwindcss: '^4',
},
env: {
DEBUG: 'tailwindcss',
...process.env,
},
})
if (skipped) {
return
}
it('should run tailwind only once initially and per change', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
if (isNextDev) {
const filePath = 'app/page.tsx'
const origContent = await next.readFile(filePath)
let getOutput = next.getCliOutputFromHere()
await next.patchFile(
filePath,
origContent.replace('hello world', 'hello hmr'),
async () => {
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('hello hmr')
let tailwindProcessingCount = [
...getOutput().matchAll(
/\[@tailwindcss\/postcss\] app\/globals.css/g
),
].length
expect(tailwindProcessingCount).toBe(1)
})
}
)
}
let tailwindProcessingCount = [
...next.cliOutput.matchAll(/\[@tailwindcss\/postcss\] app\/globals.css/g),
].length
if (isNextDev) {
expect(tailwindProcessingCount).toBe(3) // dev: initial + hmr + hmr (revert)
} else {
expect(tailwindProcessingCount).toBe(1) // build
}
})
})
const config = {
plugins: ['@tailwindcss/postcss'],
}
export default config
...@@ -601,6 +601,7 @@ pub enum AnyOperation { ...@@ -601,6 +601,7 @@ pub enum AnyOperation {
ConnectChild(connect_child::ConnectChildOperation), ConnectChild(connect_child::ConnectChildOperation),
Invalidate(invalidate::InvalidateOperation), Invalidate(invalidate::InvalidateOperation),
UpdateOutput(update_output::UpdateOutputOperation), UpdateOutput(update_output::UpdateOutputOperation),
UpdateCell(update_cell::UpdateCellOperation),
CleanupOldEdges(cleanup_old_edges::CleanupOldEdgesOperation), CleanupOldEdges(cleanup_old_edges::CleanupOldEdgesOperation),
AggregationUpdate(aggregation_update::AggregationUpdateQueue), AggregationUpdate(aggregation_update::AggregationUpdateQueue),
Nested(Vec<AnyOperation>), Nested(Vec<AnyOperation>),
...@@ -612,6 +613,7 @@ impl AnyOperation { ...@@ -612,6 +613,7 @@ impl AnyOperation {
AnyOperation::ConnectChild(op) => op.execute(ctx), AnyOperation::ConnectChild(op) => op.execute(ctx),
AnyOperation::Invalidate(op) => op.execute(ctx), AnyOperation::Invalidate(op) => op.execute(ctx),
AnyOperation::UpdateOutput(op) => op.execute(ctx), AnyOperation::UpdateOutput(op) => op.execute(ctx),
AnyOperation::UpdateCell(op) => op.execute(ctx),
AnyOperation::CleanupOldEdges(op) => op.execute(ctx), AnyOperation::CleanupOldEdges(op) => op.execute(ctx),
AnyOperation::AggregationUpdate(op) => op.execute(ctx), AnyOperation::AggregationUpdate(op) => op.execute(ctx),
AnyOperation::Nested(ops) => { AnyOperation::Nested(ops) => {
...@@ -626,6 +628,7 @@ impl AnyOperation { ...@@ -626,6 +628,7 @@ impl AnyOperation {
impl_operation!(ConnectChild connect_child::ConnectChildOperation); impl_operation!(ConnectChild connect_child::ConnectChildOperation);
impl_operation!(Invalidate invalidate::InvalidateOperation); impl_operation!(Invalidate invalidate::InvalidateOperation);
impl_operation!(UpdateOutput update_output::UpdateOutputOperation); impl_operation!(UpdateOutput update_output::UpdateOutputOperation);
impl_operation!(UpdateCell update_cell::UpdateCellOperation);
impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation); impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation);
impl_operation!(AggregationUpdate aggregation_update::AggregationUpdateQueue); impl_operation!(AggregationUpdate aggregation_update::AggregationUpdateQueue);
...@@ -639,6 +642,5 @@ pub use self::{ ...@@ -639,6 +642,5 @@ pub use self::{
cleanup_old_edges::OutdatedEdge, cleanup_old_edges::OutdatedEdge,
connect_children::connect_children, connect_children::connect_children,
prepare_new_children::prepare_new_children, prepare_new_children::prepare_new_children,
update_cell::UpdateCellOperation,
update_collectible::UpdateCollectibleOperation, update_collectible::UpdateCollectibleOperation,
}; };
use std::mem::take;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use turbo_tasks::{CellId, TaskId, backend::CellContent}; use turbo_tasks::{CellId, TaskId, backend::CellContent};
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
...@@ -5,13 +9,29 @@ use crate::backend::operation::invalidate::TaskDirtyCause; ...@@ -5,13 +9,29 @@ use crate::backend::operation::invalidate::TaskDirtyCause;
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, TaskDataCategory,
operation::{ExecuteContext, InvalidateOperation, TaskGuard}, operation::{
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
invalidate::make_task_dirty_internal,
},
storage::{get_many, remove}, storage::{get_many, remove},
}, },
data::{CachedDataItem, CachedDataItemKey}, data::{CachedDataItem, CachedDataItemKey, CellRef},
}; };
pub struct UpdateCellOperation; #[derive(Serialize, Deserialize, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum UpdateCellOperation {
InvalidateWhenCellDependency {
cell_ref: CellRef,
dependent_tasks: SmallVec<[TaskId; 4]>,
queue: AggregationUpdateQueue,
},
AggregationUpdate {
queue: AggregationUpdateQueue,
},
#[default]
Done,
}
impl UpdateCellOperation { impl UpdateCellOperation {
pub fn run(task_id: TaskId, cell: CellId, content: CellContent, mut ctx: impl ExecuteContext) { pub fn run(task_id: TaskId, cell: CellId, content: CellContent, mut ctx: impl ExecuteContext) {
...@@ -39,7 +59,7 @@ impl UpdateCellOperation { ...@@ -39,7 +59,7 @@ impl UpdateCellOperation {
// This is a hack for the streaming hack. Stateful tasks are never recomputed, so this forces invalidation for them in case of this hack. // This is a hack for the streaming hack. Stateful tasks are never recomputed, so this forces invalidation for them in case of this hack.
task.has_key(&CachedDataItemKey::Stateful {})) task.has_key(&CachedDataItemKey::Stateful {}))
{ {
let dependent = get_many!( let dependent_tasks = get_many!(
task, task,
CellDependent { cell: dependent_cell, task } CellDependent { cell: dependent_cell, task }
if dependent_cell == cell if dependent_cell == cell
...@@ -49,17 +69,78 @@ impl UpdateCellOperation { ...@@ -49,17 +69,78 @@ impl UpdateCellOperation {
drop(task); drop(task);
drop(old_content); drop(old_content);
InvalidateOperation::run( UpdateCellOperation::InvalidateWhenCellDependency {
cell_ref: CellRef {
task: task_id,
cell,
},
dependent_tasks,
queue: AggregationUpdateQueue::new(),
}
.execute(&mut ctx);
} else {
drop(task);
drop(old_content);
}
}
}
impl Operation for UpdateCellOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) {
loop {
ctx.operation_suspend_point(&self);
match self {
UpdateCellOperation::InvalidateWhenCellDependency {
cell_ref,
ref mut dependent_tasks,
ref mut queue,
} => {
if let Some(dependent_task_id) = dependent_tasks.pop() {
if ctx.is_once_task(dependent_task_id) {
// once tasks are never invalidated
continue;
}
let dependent = ctx.task(dependent_task_id, TaskDataCategory::All);
if dependent.has_key(&CachedDataItemKey::OutdatedCellDependency {
target: cell_ref,
}) {
// cell dependency is outdated, so it hasn't read the cell yet
// and doesn't need to be invalidated
continue;
}
if !dependent
.has_key(&CachedDataItemKey::CellDependency { target: cell_ref })
{
// cell dependency has been removed, so the task doesn't depend on the
// cell anymore and doesn't need to be
// invalidated
continue;
}
make_task_dirty_internal(
dependent, dependent,
dependent_task_id,
true,
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
TaskDirtyCause::CellChange { TaskDirtyCause::CellChange {
value_type: cell.type_id, value_type: cell_ref.cell.type_id,
}, },
queue,
ctx, ctx,
); );
} else { }
drop(task); if dependent_tasks.is_empty() {
drop(old_content); self = UpdateCellOperation::AggregationUpdate { queue: take(queue) };
}
}
UpdateCellOperation::AggregationUpdate { ref mut queue } => {
if queue.process(ctx) {
self = UpdateCellOperation::Done
}
}
UpdateCellOperation::Done => {
return;
}
}
} }
} }
} }
...@@ -12,7 +12,7 @@ use crate::{ ...@@ -12,7 +12,7 @@ use crate::{
TaskDataCategory, TaskDataCategory,
operation::{ operation::{
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard, AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
invalidate::{make_task_dirty, make_task_dirty_internal}, invalidate::make_task_dirty_internal,
}, },
storage::{get, get_many}, storage::{get, get_many},
}, },
...@@ -25,7 +25,6 @@ use crate::{ ...@@ -25,7 +25,6 @@ use crate::{
#[derive(Serialize, Deserialize, Clone, Default)] #[derive(Serialize, Deserialize, Clone, Default)]
pub enum UpdateOutputOperation { pub enum UpdateOutputOperation {
MakeDependentTasksDirty { MakeDependentTasksDirty {
#[cfg(feature = "trace_task_dirty")]
task_id: TaskId, task_id: TaskId,
dependent_tasks: SmallVec<[TaskId; 4]>, dependent_tasks: SmallVec<[TaskId; 4]>,
children: SmallVec<[TaskId; 4]>, children: SmallVec<[TaskId; 4]>,
...@@ -132,7 +131,6 @@ impl UpdateOutputOperation { ...@@ -132,7 +131,6 @@ impl UpdateOutputOperation {
} }
UpdateOutputOperation::MakeDependentTasksDirty { UpdateOutputOperation::MakeDependentTasksDirty {
#[cfg(feature = "trace_task_dirty")]
task_id, task_id,
dependent_tasks, dependent_tasks,
children, children,
...@@ -148,15 +146,35 @@ impl Operation for UpdateOutputOperation { ...@@ -148,15 +146,35 @@ impl Operation for UpdateOutputOperation {
ctx.operation_suspend_point(&self); ctx.operation_suspend_point(&self);
match self { match self {
UpdateOutputOperation::MakeDependentTasksDirty { UpdateOutputOperation::MakeDependentTasksDirty {
#[cfg(feature = "trace_task_dirty")]
task_id, task_id,
ref mut dependent_tasks, ref mut dependent_tasks,
ref mut children, ref mut children,
ref mut queue, ref mut queue,
} => { } => {
if let Some(dependent_task_id) = dependent_tasks.pop() { if let Some(dependent_task_id) = dependent_tasks.pop() {
make_task_dirty( if ctx.is_once_task(dependent_task_id) {
// once tasks are never invalidated
continue;
}
let dependent = ctx.task(dependent_task_id, TaskDataCategory::All);
if dependent.has_key(&CachedDataItemKey::OutdatedOutputDependency {
target: task_id,
}) {
// output dependency is outdated, so it hasn't read the output yet
// and doesn't need to be invalidated
continue;
}
if !dependent
.has_key(&CachedDataItemKey::OutputDependency { target: task_id })
{
// output dependency has been removed, so the task doesn't depend on the
// output anymore and doesn't need to be invalidated
continue;
}
make_task_dirty_internal(
dependent,
dependent_task_id, dependent_task_id,
true,
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
TaskDirtyCause::OutputChange { task_id }, TaskDirtyCause::OutputChange { task_id },
queue, queue,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment