Unverified Commit 0e8d6b30 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Support undoable modifications for many field types on TaskStorage (#95180)

## What
Allow some modifications that turn out to be no-ops to be undone on
TaskStorage. This saves us some redundant check-then-mutate cycles.

This doesn't apply to every field type
* `Option<T>` - still read-check-modify
* `CounterMap<T>` - updates are unconditional tracks and removals use
the undo approach
* `AutoSet`/`AutoMap` - mostly uses the new 'undo' approach 
* `take` as a bulk operation does not, taking an empty set is rare and
cheap to check for
* `AutoMap::insert` doesn't use the undo approach or even check, we
always track a modification since all callers are only inserting unique
values anyway

A few other incidental cleanups were landed as well.

## Why

Avoid redundant hash lookups.

## How

Have `track_modification` return a new TrackOutcome that records what
modifications were made and then in the undo case we can pass it to a
new `undo_track_modification` function which can reverse it. This is a
little brittle since both operations need to occur within a single lock
transition

I considered using a 'lambda' oriented approach but decided against it
since it would be awkward for some of the mutators and as all the
callers are generated by a macro (well _almost_ all callers) it isn't
too risky.
parent 91b3570e
......@@ -28,7 +28,7 @@ use crate::{
backend::{
EventDescription, TaskDataCategory, TurboTasksBackend,
snapshot_coordinator::OperationGuard,
storage::{SpecificTaskDataCategory, StorageWriteGuard},
storage::{SpecificTaskDataCategory, StorageWriteGuard, TrackOutcome},
storage_schema::{TaskStorage, TaskStorageAccessors},
},
data::{ActivenessState, CollectibleRef, Dirtyness, InProgressState, TransientTask},
......@@ -1251,7 +1251,9 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
// tracked
self.check_access(SpecificTaskDataCategory::Data);
if matches!(persistence, ValueTypePersistence::Persistable(..)) {
self.track_modification(SpecificTaskDataCategory::Data, "cell_data");
// Unconditional track when persistable: an insert always changes the data snapshot
// (a fresh cell, or a replaced value). No no-op to undo.
let _ = self.track_modification(SpecificTaskDataCategory::Data, "cell_data");
}
self.typed_mut().cell_data_mut().insert(cell, value)
}
......@@ -1263,15 +1265,20 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
persistence: &ValueTypePersistence,
) -> Option<SharedReference> {
self.check_access(SpecificTaskDataCategory::Data);
// Track only when a persistable entry is actually removed (the entry must
// exist and be Persistable; Skip/HashOnly cells contribute nothing to the
// data snapshot).
if matches!(persistence, ValueTypePersistence::Persistable(..))
&& self.cell_data_contains(cell)
{
self.track_modification(SpecificTaskDataCategory::Data, "cell_data");
// Track only when a persistable entry is actually removed (Skip/HashOnly cells contribute
// nothing to the data snapshot). Track BEFORE mutating (snapshot mode clones pre-mutation
// state), then undo if the entry turned out not to be present — avoiding a redundant
// `cell_data_contains` probe.
let outcome = if matches!(persistence, ValueTypePersistence::Persistable(..)) {
self.track_modification(SpecificTaskDataCategory::Data, "cell_data")
} else {
TrackOutcome::NoChange
};
let old = self.typed_mut().cell_data_mut().remove(cell);
if old.is_none() {
self.undo_track_modification(outcome);
}
self.typed_mut().cell_data_mut().remove(cell)
old
}
/// Add new cell data. Panics if the cell already had a value. See
......@@ -1405,9 +1412,12 @@ impl TaskGuard for TaskGuardImpl<'_> {
// TODO this causes race conditions, since we never know when a value is changed. We can't
// "snapshot" the value correctly.
if !self.task_id.is_transient() {
self.task
// Unconditional track (no mutation to detect a no-op against): always mark dirty.
let _ = self
.task
.track_modification(SpecificTaskDataCategory::Data, "invalidate_serialization");
self.task
let _ = self
.task
.track_modification(SpecificTaskDataCategory::Meta, "invalidate_serialization");
}
}
......@@ -1466,12 +1476,21 @@ impl TaskStorageAccessors for TaskGuardImpl<'_> {
&mut self,
category: crate::backend::storage::SpecificTaskDataCategory,
name: &str,
) {
if !self.task_id.is_transient() {
self.task.track_modification(category, name);
) -> TrackOutcome {
if self.task_id.is_transient() {
// Transient tasks are never persisted, so there is nothing to track or undo.
TrackOutcome::NoChange
} else {
self.task.track_modification(category, name)
}
}
#[inline(always)]
fn undo_track_modification(&mut self, outcome: TrackOutcome) {
// Transient tasks return `NoChange` above, so undo is harmlessly a no-op for them.
self.task.undo_track_modification(outcome);
}
fn check_access(&self, category: crate::backend::storage::SpecificTaskDataCategory) {
self.check_access(category);
}
......
......@@ -548,24 +548,20 @@ impl TaskStorage {
pub fn evictability(&self) -> (KeyEvictability, ValueEvictability) {
let flags = &self.flags;
let key_evictability = if flags.new_task() {
KeyEvictability::Unevictable
} else {
match &self.persistent_task_type {
None => KeyEvictability::Unevictable,
// strong_count == 1: only this TaskStorage holds this Arc, so no task_cache entry
// references it. It must have been already evicted on a prior cycle.
Some(arc) if arc.count() == 1 => KeyEvictability::AlreadyEvicted,
Some(_) => KeyEvictability::Evictable,
}
};
// === Absolute blockers ===
if flags.new_task() {
return (
key_evictability,
KeyEvictability::Unevictable,
ValueEvictability::Unevictable(UnevictableReason::Modified),
);
}
let key_evictability = match &self.persistent_task_type {
None => KeyEvictability::Unevictable,
// strong_count == 1: only this TaskStorage holds this Arc, so no task_cache entry
// references it. It must have been already evicted on a prior cycle.
Some(arc) if arc.count() == 1 => KeyEvictability::AlreadyEvicted,
Some(_) => KeyEvictability::Evictable,
};
// All these flags imply that the task is currently being used in some way
// either literally executing, or about to
if self.get_in_progress().is_some()
......
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