Unverified Commit ab75f649 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Simplify turbo-tasks-backend guards and execution contexts (#99599)

### What?

Simplify the task guard and execution context abstractions in
`turbo-tasks-backend`, including the companion child context used for
parallel work.

Followup from #99249

### Why?

Each trait has a single implementation, with no alternate
implementations or mocks using the abstraction. The trait layer adds
duplicate declarations, associated types, and generic plumbing without
providing a useful extension point today. This is a simplicity refactor,
not a performance optimization.


<!-- fleet 22e7fce7-cf14-4d21-9cb9-d77f674cb241 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 2d5a45a5
......@@ -31,8 +31,8 @@ use crate::{
backend::{
TurboTasksBackend,
operation::{
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, ExecuteContextImpl,
TaskGuard, capture_all_edges, cleanup_old_edges_deletions_only,
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, capture_all_edges,
cleanup_old_edges_deletions_only,
},
snapshot_coordinator::SnapshotPhase,
storage::{SpecificTaskDataCategory, TaskDataCategory},
......@@ -262,7 +262,7 @@ impl TurboTasksBackend {
GcJob::Collect(task_id) => task_id,
};
let collector = |child_id| spawner.spawn(GcJob::Collect(child_id));
let mut ctx = ExecuteContextImpl::new_for_gc(self, turbo_tasks, phase, &collector);
let mut ctx = ExecuteContext::new_for_gc(self, turbo_tasks, phase, &collector);
// `All` restores Data so `capture_all_edges` below can read the
// Data-category dependency sets. The recheck itself only needs Meta.
let mut task = ctx.task(task_id, TaskDataCategory::All);
......@@ -321,7 +321,7 @@ impl TurboTasksBackend {
let deferred = std::mem::take(&mut result.deferred_balance_edges);
if !deferred.is_empty() {
let noop_collector = |_task_id| {};
let mut ctx = ExecuteContextImpl::new_for_gc(self, turbo_tasks, phase, &noop_collector);
let mut ctx = ExecuteContext::new_for_gc(self, turbo_tasks, phase, &noop_collector);
let mut queue = AggregationUpdateQueue::new();
queue.extend_balance_edges(deferred, &mut ctx);
while !queue.process(&mut ctx) {}
......@@ -333,7 +333,7 @@ impl TurboTasksBackend {
let dirty_dependents = std::mem::take(&mut result.deferred_dirty_dependents);
if !dirty_dependents.is_empty() {
let noop_collector = |_task_id| {};
let mut ctx = ExecuteContextImpl::new_for_gc(self, turbo_tasks, phase, &noop_collector);
let mut ctx = ExecuteContext::new_for_gc(self, turbo_tasks, phase, &noop_collector);
let mut queue = AggregationUpdateQueue::new();
// A dependent collected by this same pass is skipped: the job is weak by construction.
queue.push(AggregationUpdateJob::InvalidateDueToDependencyTornDown {
......
......@@ -64,11 +64,10 @@ pub use self::{
use crate::{
backend::{
operation::{
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext,
ExecuteContextImpl, LeafDistanceUpdateQueue, OutdatedEdge, TaskGuard, TaskType,
TaskTypeRef, capture_all_edges, cleanup_old_edges, connect_child, connect_children,
get_aggregation_number, get_uppers, invalidate, make_task_dirty_internal,
prepare_new_children, update_cell,
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, LeafDistanceUpdateQueue,
OutdatedEdge, TaskGuard, TaskType, TaskTypeRef, capture_all_edges, cleanup_old_edges,
connect_child, connect_children, get_aggregation_number, get_uppers, invalidate,
make_task_dirty_internal, prepare_new_children, update_cell,
},
snapshot_coordinator::{OperationGuard, SlowSettle, SnapshotCoordinator},
storage::Storage,
......@@ -104,7 +103,7 @@ const GC_MIN_PROGRESS: Duration = Duration::from_millis(100);
/// than the (likely higher) priority of the original schedule. We use invalidation priority
/// based on the task's leaf distance, parented under either the task's current dirty priority
/// or `leaf()` if it is no longer dirty.
fn compute_stale_priority(task: &impl TaskGuard) -> TaskPriority {
fn compute_stale_priority(task: &TaskGuard<'_>) -> TaskPriority {
TaskPriority::invalidation(
task.get_leaf_distance()
.copied()
......@@ -371,8 +370,8 @@ impl TurboTasksBackend {
fn execute_context<'a>(
&'a self,
turbo_tasks: &'a TurboTasks<TurboTasksBackend>,
) -> impl ExecuteContext<'a> {
ExecuteContextImpl::new(self, turbo_tasks)
) -> ExecuteContext<'a> {
ExecuteContext::new(self, turbo_tasks)
}
/// Like [`TurboTasksBackend::execute_context`], but refuses to hand out a context once
......@@ -385,12 +384,12 @@ impl TurboTasksBackend {
fn try_execute_context<'a>(
&'a self,
turbo_tasks: &'a TurboTasks<TurboTasksBackend>,
) -> Option<impl ExecuteContext<'a>> {
) -> Option<ExecuteContext<'a>> {
let stopping = self.stopping.read();
if *stopping {
return None;
}
Some(ExecuteContextImpl::new_with_shutdown_guard(
Some(ExecuteContext::new_with_shutdown_guard(
self,
turbo_tasks,
stopping,
......@@ -513,7 +512,7 @@ impl TurboTasksBackend {
fn task_error_to_turbo_tasks_execution_error(
&self,
error: &TaskError,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
) -> TurboTasksExecutionError {
match error {
TaskError::Panic(panic) => TurboTasksExecutionError::Panic(panic.clone()),
......@@ -584,11 +583,11 @@ struct TaskExecutionCompletePrepareResult {
pub is_session_dependent: bool,
}
fn lock_task_and_optional_reader<'e, C: ExecuteContext<'e>>(
ctx: &mut C,
fn lock_task_and_optional_reader<'e>(
ctx: &mut ExecuteContext<'e>,
task_id: TaskId,
reader_id: Option<TaskId>,
) -> (C::TaskGuardImpl, Option<C::TaskGuardImpl>) {
) -> (TaskGuard<'e>, Option<TaskGuard<'e>>) {
let Some(reader_id) = reader_id else {
return (ctx.task(task_id, TaskDataCategory::All), None);
};
......@@ -663,7 +662,7 @@ impl TurboTasksBackend {
/// whether a worker has actually started it. A task that is only `Scheduled` can be taken
/// over and executed by the reader; one that is `InProgress` can only be waited for.
fn check_in_progress<T>(
task: &impl TaskGuard,
task: &TaskGuard<'_>,
reader_description: Option<EventDescription>,
tracking: ReadTracking,
) -> Option<Result<ReadOutcome<T>>> {
......@@ -749,7 +748,7 @@ impl TurboTasksBackend {
.collect::<String>()
}
fn get_info(
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
parent_and_count: Option<(TaskId, i32)>,
visited: &mut FxHashSet<TaskId>,
......@@ -963,9 +962,9 @@ impl TurboTasksBackend {
fn add_cell_dependency(
task_id: TaskId,
mut task: impl TaskGuard,
mut task: TaskGuard<'_>,
reader: Option<TaskId>,
reader_task: Option<impl TaskGuard>,
reader_task: Option<TaskGuard<'_>>,
cell: CellId,
key: Option<u64>,
) {
......@@ -1104,10 +1103,10 @@ impl TurboTasksBackend {
fn listen_to_cell(
&self,
task: &mut impl TaskGuard,
task: &mut TaskGuard<'_>,
task_id: TaskId,
reader: Option<TaskId>,
reader_task: &Option<impl TaskGuard>,
reader_task: &Option<TaskGuard<'_>>,
cell: CellId,
) -> (EventListener, bool) {
let note = || {
......@@ -2298,7 +2297,7 @@ impl TurboTasksBackend {
fn task_execution_completed_prepare(
&self,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
#[cfg(feature = "trace_task_details")] span: &Span,
task_id: TaskId,
result: Result<RawVc, TurboTasksExecutionError>,
......@@ -2603,7 +2602,7 @@ impl TurboTasksBackend {
fn task_execution_completed_invalidate_output_dependent(
&self,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] function_id: Option<FunctionId>,
output_dependent_tasks: SmallVec<[TaskId; 4]>,
......@@ -2626,7 +2625,7 @@ impl TurboTasksBackend {
}
fn process_output_dependents(
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] cause: &TaskDirtyCause,
dependent_task_id: TaskId,
......@@ -2719,7 +2718,7 @@ impl TurboTasksBackend {
fn task_execution_completed_connect(
&self,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
new_children: FxHashSet<TaskId>,
) -> Option<TaskPriority> {
......@@ -2789,7 +2788,7 @@ impl TurboTasksBackend {
#[allow(clippy::type_complexity)]
fn task_execution_completed_finish(
&self,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
#[cfg(feature = "verify_determinism")] no_output_set: bool,
new_output: Option<OutputValue>,
......@@ -2897,7 +2896,7 @@ impl TurboTasksBackend {
fn task_execution_completed_cleanup(
&self,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
cell_counters: &AutoMap<ValueTypeId, u32, BuildHasherDefault<FxHasher>, 8>,
is_error: bool,
......
......@@ -91,7 +91,7 @@ pub fn cleanup_old_edges(
task_id: TaskId,
outdated: Vec<OutdatedEdge>,
queue: AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
) -> Stats {
cleanup_old_edges_inner(task_id, outdated, queue, ctx, false).0
}
......@@ -102,10 +102,10 @@ pub fn cleanup_old_edges(
/// parallel collect is quiescent. Deletion is safe to run concurrently; the deferred work is
/// not. `balance_edge` *adds* edges, and dirtying propagates through the aggregation graph --
/// neither is safe while other workers are still collecting.
pub fn cleanup_old_edges_deletions_only<'a, C: ExecuteContext<'a>>(
pub fn cleanup_old_edges_deletions_only(
task_id: TaskId,
outdated: Vec<OutdatedEdge>,
ctx: &mut C,
ctx: &mut ExecuteContext<'_>,
) -> DeferredCleanup {
let (_, stopped) = cleanup_old_edges_inner(
task_id,
......@@ -122,7 +122,7 @@ fn cleanup_old_edges_inner(
task_id: TaskId,
mut outdated: Vec<OutdatedEdge>,
mut queue: AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
stop_when_only_rebalance_remains: bool,
) -> (Stats, Option<DeferredCleanup>) {
let mut dirty_dependents = AutoSet::default();
......
......@@ -20,12 +20,12 @@ use crate::{
/// connect handshake.
///
/// GC destructively mutates tasks so mark resurrected tasks as dirty to get them re-scheduled.
pub(super) fn resurrect_deleted<'e, C: ExecuteContext<'e>>(
guard: C::TaskGuardImpl,
pub(super) fn resurrect_deleted<'e>(
guard: TaskGuard<'e>,
task_id: TaskId,
queue: &mut AggregationUpdateQueue,
ctx: &mut C,
) -> C::TaskGuardImpl {
ctx: &mut ExecuteContext<'e>,
) -> TaskGuard<'e> {
if !guard.deleted() {
return guard;
}
......@@ -64,7 +64,7 @@ pub(super) fn resurrect_deleted<'e, C: ExecuteContext<'e>>(
task
}
fn release_construction_ref<'e, C: ExecuteContext<'e>>(task_id: TaskId, ctx: &mut C) {
fn release_construction_ref(task_id: TaskId, ctx: &mut ExecuteContext<'_>) {
let mut task = ctx.task(task_id, TaskDataCategory::Meta);
task.update_and_get_transient_ref_count(-1);
}
......@@ -73,7 +73,7 @@ pub fn connect_child(
parent_task_id: Option<TaskId>,
child_task_id: TaskId,
release_construction_ref: bool,
mut ctx: impl ExecuteContext<'_>,
mut ctx: ExecuteContext<'_>,
) {
if parent_task_id.is_none() {
// All parentless tasks receive a transient ref when connected: their lifetime cannot be
......
......@@ -10,17 +10,17 @@ use turbo_tasks::{
use crate::backend::{
operation::{
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext,
TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob, get_aggregation_number,
get_uppers, invalidate::make_task_dirty_internal, is_aggregating_node,
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, TaskGuard,
aggregation_update::InnerOfUppersHasNewFollowersJob, get_aggregation_number, get_uppers,
invalidate::make_task_dirty_internal, is_aggregating_node,
},
storage_schema::TaskStorageAccessors,
};
pub fn connect_children(
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
parent_task_id: TaskId,
mut parent_task: impl TaskGuard,
mut parent_task: TaskGuard<'_>,
new_children: FxHashSet<TaskId>,
parent_has_active_count: bool,
should_track_activeness: bool,
......@@ -46,7 +46,7 @@ pub fn connect_children(
drop(parent_task);
fn process_new_children(
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
new_follower_ids: SmallVec<[TaskId; 4]>,
upper_ids: Option<SmallVec<[TaskId; 4]>>,
parent_task_id: TaskId,
......
......@@ -20,7 +20,7 @@ use crate::{
pub fn invalidate(
task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
mut ctx: impl ExecuteContext<'_>,
mut ctx: ExecuteContext<'_>,
) {
let mut queue = AggregationUpdateQueue::new();
for task_id in task_ids {
......@@ -42,7 +42,7 @@ pub fn try_make_task_dirty(
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
) {
let Some(mut task) = ctx.try_task(task_id, TaskDataCategory::All) else {
return;
......@@ -58,12 +58,12 @@ pub fn try_make_task_dirty(
}
/// Requires the guard to be allocated with [TaskDataCategory::All]
pub fn make_task_dirty_internal<'e, E: ExecuteContext<'e>>(
task: &mut E::TaskGuardImpl,
pub fn make_task_dirty_internal(
task: &mut TaskGuard<'_>,
make_stale: bool,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut E,
ctx: &mut ExecuteContext<'_>,
) {
// There must be no way to invalidate immutable tasks. If there would be a way the task is not
// immutable.
......
......@@ -85,7 +85,7 @@ impl LeafDistanceUpdateQueue {
}
/// Executes a single step of the queue. Returns true, when the queue is empty.
pub fn process(&mut self, ctx: &mut impl ExecuteContext) -> bool {
pub fn process(&mut self, ctx: &mut ExecuteContext<'_>) -> bool {
let mut remaining = MAX_COUNT_BEFORE_YIELD;
while remaining > 0 {
if let Some((Reverse(queue_dependencies_distance), task_id)) = self.queue.pop() {
......@@ -121,7 +121,7 @@ impl LeafDistanceUpdateQueue {
fn update_leaf_distance(
&mut self,
ctx: &mut impl ExecuteContext,
ctx: &mut ExecuteContext<'_>,
task_id: TaskId,
dependencies_distance: u32,
dependencies_max_distance_in_buffer: u32,
......@@ -169,7 +169,7 @@ impl LeafDistanceUpdateQueue {
task.set_leaf_distance(leaf_distance);
}
pub fn execute(&mut self, ctx: &mut impl ExecuteContext<'_>) {
pub fn execute(&mut self, ctx: &mut ExecuteContext<'_>) {
if self.is_empty() {
return;
}
......
......@@ -3,15 +3,18 @@ use std::{cmp::max, num::NonZeroU32};
use rustc_hash::FxHashSet;
use turbo_tasks::TaskId;
use crate::backend::operation::{
AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
use crate::backend::{
operation::{
AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
},
storage_schema::TaskStorageAccessors,
};
const AGGREGATION_NUMBER_BUFFER_SPACE: u32 = 3;
pub fn prepare_new_children(
parent_task_id: TaskId,
parent_task: &mut impl TaskGuard,
parent_task: &mut TaskGuard<'_>,
new_children: &FxHashSet<TaskId>,
queue: &mut AggregationUpdateQueue,
) -> u32 {
......
......@@ -29,7 +29,7 @@ pub fn update_cell(
content_hash: Option<CellHash>,
#[cfg(feature = "verify_determinism")] verification_mode: VerificationMode,
#[cfg(not(feature = "verify_determinism"))] _verification_mode: VerificationMode,
mut ctx: impl ExecuteContext<'_>,
mut ctx: ExecuteContext<'_>,
) {
let value_type = registry::get_value_type(cell.type_id());
// `content_hash` is only ever supplied for `HashOnly` cells — only the
......@@ -219,7 +219,7 @@ pub fn update_cell(
/// Updates the stored cell_data_hash, which only `serialization = "hash"`
/// cells consult (on eviction + recompute). Skips the update for all other
/// persistence modes and when the hash hasn't changed.
fn update_cell_data_hash(task: &mut impl TaskGuard, cell: &CellId, content_hash: Option<CellHash>) {
fn update_cell_data_hash(task: &mut TaskGuard<'_>, cell: &CellId, content_hash: Option<CellHash>) {
let old_hash = task.get_cell_data_hash(cell).copied();
if old_hash != content_hash {
if let Some(hash) = content_hash {
......@@ -235,7 +235,7 @@ fn invalidate_cell_dependents(
mut dependent_tasks: FxIndexMap<TaskId, SmallVec<[Option<u64>; 2]>>,
#[cfg(feature = "task_dirty_cause")] has_updated_key_hashes: bool,
content: Option<TypedSharedReference>,
ctx: &mut impl ExecuteContext<'_>,
ctx: &mut ExecuteContext<'_>,
) {
let mut queue = AggregationUpdateQueue::new();
while let Some((dependent_task_id, keys)) = dependent_tasks.pop() {
......
......@@ -21,7 +21,7 @@ impl UpdateCollectibleOperation {
task_id: TaskId,
collectible: CollectibleRef,
mut count: i32,
mut ctx: impl ExecuteContext<'_>,
mut ctx: ExecuteContext<'_>,
) {
let mut task = ctx.task(task_id, TaskDataCategory::All);
if count < 0
......
#![feature(anonymous_lifetime_in_impl_trait)]
#![feature(deref_patterns)]
mod backend;
......
......@@ -335,7 +335,7 @@ async fn mutating_a_state_read_by_a_collected_task_does_not_panic() {
// GC only soft-deletes, leaving the task resident, and a weak open of a resident-but-deleted
// task returns early on the `deleted` flag. Evicting drops it from memory (and tombstones it on
// disk), so the open below instead reaches the exists-nowhere case in
// `ExecuteContextImpl::open_task` -- nothing restored, nothing found on disk -- which is the
// `ExecuteContext::open_task` -- nothing restored, nothing found on disk -- which is the
// one that would panic under `MustExist`. Drop this line and the test still passes, but it
// stops covering that path.
tt2.backend().snapshot_and_evict_for_testing(&tt2);
......
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