Unverified Commit a98213cb authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Refactor effects system: dedup/conflict detection, simplified Effect trait (#92300)

### What?

Refactors the turbo-tasks effects system to support duplicate detection,
conflict detection, and state tracking across effect applications. Also
adds a ptr_eq fast path to `ReadRef::PartialEq`.

### Why?

The existing effects system had several problems:

- **No duplicate detection**: The same effect (e.g., writing the same
content to the same path) would be re-applied every time, wasting I/O
- **No conflict detection**: Two effects writing different content to
the same path could cause an invalidation loop where the two writes
invalidate each other, keeping a CPU busy-looping. On production builds
this would crash with `Dependency tracking is disabled so invalidation
is not allowed`
- **Unnecessarily generic**: The old `DynEffect`→`DynEffectApplyFuture`
chain wrapped errors in `Arc<dyn EffectError>`, plus a
`DynEffectApplyFuture` chain, all to work around the fact that
`anyhow::Error` doesn't implement `std::error::Error`
- **Complex dedup state machine**: `EffectInstance` used a
`Mutex<EffectState>` with 4 states
(`NotStarted`/`Started`/`Finished`/`Invalid`), `Event`/`EventListener`,
and `spawn()` to ensure idempotent application — all of which is now
unnecessary since dedup is handled at the `Effects::apply` level
- **Task-local context**: `ApplyEffectsContext` was a task-local typed
map used only for a directory-creation cache, requiring `spawn()` +
scope propagation
- **Slow `ReadRef` equality**: `ReadRef::PartialEq` always performed
deep content comparison even when both refs pointed to the same
allocation

### How?

#### Changed files

| File | Change |
|------|--------|
| `turbo-tasks/src/effect.rs` | Rewrote `Effect` trait, `DynEffect`,
`EffectStateStorage`, `Effects::apply()` |
| `turbo-tasks/src/lib.rs` | Updated re-exports (`EffectError`,
`EffectStateStorage`) |
| `turbo-tasks/src/read_ref.rs` | Added `ptr_eq` fast path to
`ReadRef::PartialEq` |
| `turbo-tasks-fs/src/lib.rs` | Updated `WriteEffect`/`WriteLinkEffect`
impls, restored `AnyhowWrapper`, removed `register_write_invalidator` |
| `turbo-tasks-fs/src/invalidator_map.rs` | Simplified — removed
`WriteContent` enum, invalidators now use `()` values |
| `turbo-tasks-fs/src/invalidation.rs` | Deleted — `Write`/`WriteKind`
invalidation reason types no longer needed |

#### New `Effect` trait (in `turbo-tasks/src/effect.rs`)

```rust
pub trait Effect: TraceRawVcs + NonLocalValue + Send + Sync + 'static {
    type Error: EffectError;
    type Value: Clone + DynPartialEq + Eq + Send + Sync + 'static;
    fn key(&self) -> Vec<u8>;
    fn value(&self) -> &Self::Value;
    fn state_storage(&self) -> &EffectStateStorage;
    fn apply(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
```

The `Error` associated type uses `dyn std::error::Error` (via
`EffectError`) rather than `anyhow::Error` to encourage structured error
types that can be downcast into `Issue`s — particularly for filesystem
errors from `turbo-tasks-fs`. The current fs implementations use
`AnyhowWrapper` as a bridge.

#### `EffectStateStorage`

New struct stored on `DiskFileSystemInner`, using a two-level locking
scheme for performance:
- `DashMap<Vec<u8>, Arc<EffectStateEntry>>` tracks per-key state
- Each `EffectStateEntry` has a sync `parking_lot::Mutex<Option<Box<dyn
Any>>>` for the last applied value (fast dedup reads) and a
`tokio::sync::Mutex<()>` write lock (serializes concurrent writes to the
same key)
- The sync fast path avoids `.await` entirely when the stored value
matches, critical for high-iteration scenarios (e.g., the `writeToDisk`
test that calls `Effects::apply` 10,000+ times per route)

#### `Effects::apply()` flow

1. Group effects by `key()`, cache the `(index, Arc<EffectStateEntry>)`
pairs in a `OnceLock` — computed once on first call, reused on
subsequent calls with **no DashMap lookup** in the hot path
2. Detect duplicates (same key + same value → keep one) and conflicts
(same key + different value → error)
3. For each unique effect:
- **Sync fast path**: check `last_applied` via `parking_lot::Mutex` —
return immediately if value unchanged
- **Slow path**: acquire `tokio::sync::Mutex` write lock, re-check,
clear `last_applied` to `None` (prevents stale fast-path matches during
write), apply the effect, then store the new value

#### Equality dispatch

`eq_value_dyn` now calls
`turbo_dyn_eq_hash::DynPartialEq::dyn_partial_eq` instead of a manual
downcast, using the existing `turbo-dyn-eq-hash` crate machinery.

#### `ReadRef::PartialEq` ptr_eq fast path

```rust
fn eq(&self, other: &Self) -> bool {
    Self::ptr_eq(self, other) || Self::as_raw_ref(self).eq(Self::as_raw_ref(other))
}
```
Short-circuits deep comparison when both `ReadRef`s share the same `Arc`
allocation — the common case in the effects dedup path where the stored
and new `ReadRef` originate from the same turbo-tasks cell.

#### `Effect::value()` returns `&Self::Value`

Avoids cloning `ReadRef` (Arc increment) on every `eq_value_dyn`
comparison. The reference goes straight to the stored field. Only
`value_dyn()` (called when actually storing a new value after a write)
clones.

#### Simplified internals

- `EffectInstance` reduced from `Mutex<EffectState>` state machine to
plain `Box<dyn DynEffect>`
- `DynEffect` trait gains `key()`, `eq_value_dyn()`, `value_dyn()`,
`state_storage()` for object-safe dispatch; `dyn_apply()` converts
`Self::Error` to `anyhow::Error` via the `Into` blanket impl
- `Effects::apply()` now performs grouping, dedup, conflict detection,
and per-key state-aware application

#### Removed

- `EffectState` enum (4 variants) + associated event machinery
- `ApplyEffectsContext` + `APPLY_EFFECTS_CONTEXT` task-local
- `DiskFileSystemApplyContext` struct
- `register_write_invalidator` (complex write-invalidation tracking)
- `WriteContent` enum from `InvalidatorMap`
- `Write`/`WriteKind` invalidation reason types (deleted
`invalidation.rs`)

#### Kept from original design

- `EffectError` trait + `AnyhowWrapper` in `turbo-tasks-fs` — preserved
for future structured filesystem error types (to produce typed `Issue`s
from write failures)
- `invalidate_from_write` — simplified to just remove+fire path
invalidators for read-task invalidation after writes
- Directory creation cache — moved from task-local `ApplyEffectsContext`
to `DashMap<PathBuf, ()>` on `DiskFileSystemInner`

#### Test changes

- `test_symlink_stress`: Updated to deduplicate random updates per
symlink index before writing (the test previously relied on silent
conflict resolution)
- `is_sync_and_send` test: Relaxed to `is_send` since the new
`Effects::apply()` future holds a `Pin<Box<dyn Future + Send>>` across
await points (not `Sync`, but `Send` is sufficient)

### Testing

- [x] `cargo check --workspace` — zero errors, zero warnings
- [x] `cargo clippy -p turbo-tasks --all-targets -- -D warnings -A
deprecated` — clean
- [x] `ast-grep scan` — clean (all `Err(anyhow!())` replaced with
`bail!()`)
- [x] `cargo test -p turbo-tasks --lib` — 50 passed
- [x] `cargo test -p turbo-tasks-fs --lib` — 110 passed
- [x] `cargo test -p turbo-tasks-backend --lib` — 35 passed
- [x] `cargo test -p turbopack-tests` — 89 passed

<!-- NEXT_JS_LLM_PR -->

---------
Co-authored-by: 's avatarTobias Koppers <sokra@users.noreply.github.com>
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
parent 69264a76
...@@ -144,7 +144,7 @@ impl InvalidationReasonKind for InitializeKind { ...@@ -144,7 +144,7 @@ impl InvalidationReasonKind for InitializeKind {
} }
/// Invalidation was caused by a write operation on the filesystem /// Invalidation was caused by a write operation on the filesystem
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct Write { pub(crate) struct Write {
pub path: String, pub path: String,
} }
......
...@@ -5,21 +5,13 @@ use std::{ ...@@ -5,21 +5,13 @@ use std::{
}; };
use concurrent_queue::ConcurrentQueue; use concurrent_queue::ConcurrentQueue;
use rustc_hash::FxHashMap; use rustc_hash::FxHashSet;
use turbo_tasks::{Invalidator, ReadRef}; use turbo_tasks::Invalidator;
use crate::{LinkContent, PersistedFileContent}; pub type LockedInvalidatorMap = BTreeMap<PathBuf, FxHashSet<Invalidator>>;
#[derive(PartialEq, Eq)]
pub enum WriteContent {
File(ReadRef<PersistedFileContent>),
Link(ReadRef<LinkContent>),
}
pub type LockedInvalidatorMap = BTreeMap<PathBuf, FxHashMap<Invalidator, Option<WriteContent>>>;
pub struct InvalidatorMap { pub struct InvalidatorMap {
queue: ConcurrentQueue<(PathBuf, Invalidator, Option<WriteContent>)>, queue: ConcurrentQueue<(PathBuf, Invalidator)>,
map: Mutex<LockedInvalidatorMap>, map: Mutex<LockedInvalidatorMap>,
} }
...@@ -39,27 +31,20 @@ impl InvalidatorMap { ...@@ -39,27 +31,20 @@ impl InvalidatorMap {
pub fn lock(&self) -> LockResult<MutexGuard<'_, LockedInvalidatorMap>> { pub fn lock(&self) -> LockResult<MutexGuard<'_, LockedInvalidatorMap>> {
let mut guard = self.map.lock()?; let mut guard = self.map.lock()?;
while let Ok((key, value, write_content)) = self.queue.pop() { while let Ok((key, value)) = self.queue.pop() {
guard.entry(key).or_default().insert(value, write_content); guard.entry(key).or_default().insert(value);
} }
Ok(guard) Ok(guard)
} }
pub fn insert( pub fn insert(&self, key: PathBuf, invalidator: Invalidator) {
&self, self.queue.push((key, invalidator)).unwrap_or_else(|err| {
key: PathBuf, let (key, ..) = err.into_inner();
invalidator: Invalidator, // PushError<T> is not Debug
write_content: Option<WriteContent>, panic!(
) { "failed to push {key:?} queue push should never fail, queue is unbounded and \
self.queue never closed"
.push((key, invalidator, write_content)) )
.unwrap_or_else(|err| { });
let (key, ..) = err.into_inner();
// PushError<T> is not Debug
panic!(
"failed to push {key:?} queue push should never fail, queue is unbounded and \
never closed"
)
});
} }
} }
This diff is collapsed.
...@@ -419,12 +419,12 @@ impl DiskWatcher { ...@@ -419,12 +419,12 @@ impl DiskWatcher {
invalidators.into_iter().map(move |i| (reason.clone(), i)) invalidators.into_iter().map(move |i| (reason.clone(), i))
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
parallel::for_each_owned(invalidators, |(reason, (invalidator, _))| { parallel::for_each_owned(invalidators, |(reason, invalidator)| {
invalidator.invalidate_with_reason(&*turbo_tasks, reason); invalidator.invalidate_with_reason(&*turbo_tasks, reason);
}); });
} else { } else {
let invalidators = iter let invalidators = iter
.flat_map(|(_, invalidators)| invalidators.into_keys()) .flat_map(|(_, invalidators)| invalidators.into_iter())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
parallel::for_each_owned(invalidators, |invalidator| { parallel::for_each_owned(invalidators, |invalidator| {
invalidator.invalidate(&*turbo_tasks); invalidator.invalidate(&*turbo_tasks);
...@@ -786,9 +786,9 @@ fn invalidate_path( ...@@ -786,9 +786,9 @@ fn invalidate_path(
) { ) {
for path in paths { for path in paths {
if let Some(invalidators) = invalidator_map.remove(&path) { if let Some(invalidators) = invalidator_map.remove(&path) {
invalidators.into_iter().for_each(|(i, _)| { invalidators
invalidate(inner, turbo_tasks, report_invalidation_reason, &path, i) .into_iter()
}); .for_each(|i| invalidate(inner, turbo_tasks, report_invalidation_reason, &path, i));
} }
} }
} }
...@@ -802,9 +802,9 @@ fn invalidate_path_and_children_execute( ...@@ -802,9 +802,9 @@ fn invalidate_path_and_children_execute(
) { ) {
for path in paths { for path in paths {
for (_, invalidators) in invalidator_map.extract_path_with_children(&path) { for (_, invalidators) in invalidator_map.extract_path_with_children(&path) {
invalidators.into_iter().for_each(|(i, _)| { invalidators
invalidate(inner, turbo_tasks, report_invalidation_reason, &path, i) .into_iter()
}); .for_each(|i| invalidate(inner, turbo_tasks, report_invalidation_reason, &path, i));
} }
} }
} }
......
This diff is collapsed.
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#![feature(error_generic_member_access)] #![feature(error_generic_member_access)]
#![feature(arbitrary_self_types)] #![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)] #![feature(arbitrary_self_types_pointers)]
#![feature(downcast_unchecked)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(sync_unsafe_cell)] #![feature(sync_unsafe_cell)]
#![feature(async_fn_traits)] #![feature(async_fn_traits)]
...@@ -76,7 +75,7 @@ pub use crate::{ ...@@ -76,7 +75,7 @@ pub use crate::{
collectibles::CollectiblesSource, collectibles::CollectiblesSource,
completion::{Completion, Completions}, completion::{Completion, Completions},
display::{ValueToString, ValueToStringRef}, display::{ValueToString, ValueToStringRef},
effect::{ApplyEffectsContext, Effect, EffectError, Effects, emit_effect, take_effects}, effect::{Effect, EffectError, EffectStateStorage, Effects, emit_effect, take_effects},
error::PrettyPrintError, error::PrettyPrintError,
id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId}, id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId},
invalidation::{ invalidation::{
......
...@@ -91,10 +91,11 @@ where ...@@ -91,10 +91,11 @@ where
impl<T> PartialEq for ReadRef<T> impl<T> PartialEq for ReadRef<T>
where where
T: PartialEq, T: Eq,
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
Self::as_raw_ref(self).eq(Self::as_raw_ref(other)) // Fast path: if both point to the same allocation, they're equal.
Self::ptr_eq(self, other) || Self::as_raw_ref(self).eq(Self::as_raw_ref(other))
} }
} }
...@@ -102,7 +103,7 @@ impl<T> Eq for ReadRef<T> where T: Eq {} ...@@ -102,7 +103,7 @@ impl<T> Eq for ReadRef<T> where T: Eq {}
impl<T> PartialOrd for ReadRef<T> impl<T> PartialOrd for ReadRef<T>
where where
T: PartialOrd, T: PartialOrd + Eq,
{ {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Self::as_raw_ref(self).partial_cmp(Self::as_raw_ref(other)) Self::as_raw_ref(self).partial_cmp(Self::as_raw_ref(other))
...@@ -111,7 +112,7 @@ where ...@@ -111,7 +112,7 @@ where
impl<T> Ord for ReadRef<T> impl<T> Ord for ReadRef<T>
where where
T: Ord, T: Ord + Eq,
{ {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
Self::as_raw_ref(self).cmp(Self::as_raw_ref(other)) Self::as_raw_ref(self).cmp(Self::as_raw_ref(other))
......
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