Unverified Commit 6f6ea204 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Simplify aggregation update queue control flow (#100146)

Now that we are no longer suspending we can simplify control flow and
implementation of the AggregationUpdateQueue

* drop an unneeded `Clone` derive
* process batch tasks via loops instead of `pop` + `push_front`
* when we run out of optimization quota just immediately tag everything
instead of popping each task and checking the quote again
* simplify the public apis
parent 179eba81
...@@ -302,7 +302,7 @@ impl TurboTasksBackend { ...@@ -302,7 +302,7 @@ impl TurboTasksBackend {
let mut ctx = ExecuteContext::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(); let mut queue = AggregationUpdateQueue::new();
queue.extend_balance_edges(deferred, &mut ctx); queue.extend_balance_edges(deferred, &mut ctx);
while !queue.process(&mut ctx) {} queue.execute(&mut ctx);
} }
// Collect all active roots // Collect all active roots
......
...@@ -336,22 +336,17 @@ fn cleanup_old_edges_inner( ...@@ -336,22 +336,17 @@ fn cleanup_old_edges_inner(
} }
} }
while !stop_when_only_rebalance_remains || !queue.only_rebalance_remains() { if stop_when_only_rebalance_remains {
if queue.process(ctx) { // Edge removal is done; hand the rebalance back to the caller. Any
#[cfg(feature = "trace_aggregation_update_stats")] // other pending work would be dropped here, so `execute_until_rebalance`
let stats = take(&mut queue.stats); // asserts that nothing else is left.
#[cfg(not(feature = "trace_aggregation_update_stats"))] let balance_edges = queue.execute_until_rebalance(ctx);
let stats = (); return (Default::default(), Some(DeferredCleanup { balance_edges }));
return (stats, None);
}
} }
// Edge removal is done; hand the rebalance back to the caller. Any queue.execute(ctx);
// other pending work would be dropped here, so `only_rebalance_remains` #[cfg(feature = "trace_aggregation_update_stats")]
// asserts that nothing else is left. let stats = take(&mut queue.stats);
( #[cfg(not(feature = "trace_aggregation_update_stats"))]
Default::default(), let stats = ();
Some(DeferredCleanup { (stats, None)
balance_edges: queue.take_deferred_balance_edges().collect(),
}),
)
} }
...@@ -12,9 +12,6 @@ use crate::backend::{ ...@@ -12,9 +12,6 @@ use crate::backend::{
TaskDataCategory, operation::ExecuteContext, storage_schema::TaskStorageAccessors, TaskDataCategory, operation::ExecuteContext, storage_schema::TaskStorageAccessors,
}; };
/// The maximum number of leaf distance updates processed in one step.
const MAX_COUNT_BEFORE_YIELD: usize = 1000;
/// We avoid incrementing the leaf distance by 1 each time to avoid frequent updates. /// We avoid incrementing the leaf distance by 1 each time to avoid frequent updates.
/// Instead we use a buffer zone that shrinks as the leaf distance increases. /// Instead we use a buffer zone that shrinks as the leaf distance increases.
/// This constant defines the size of that buffer zone at leaf distance 0. /// This constant defines the size of that buffer zone at leaf distance 0.
...@@ -52,10 +49,6 @@ impl LeafDistanceUpdateQueue { ...@@ -52,10 +49,6 @@ impl LeafDistanceUpdateQueue {
Self::default() Self::default()
} }
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
pub fn push( pub fn push(
&mut self, &mut self,
task_id: TaskId, task_id: TaskId,
...@@ -84,39 +77,32 @@ impl LeafDistanceUpdateQueue { ...@@ -84,39 +77,32 @@ impl LeafDistanceUpdateQueue {
}; };
} }
/// Executes a single step of the queue. Returns true, when the queue is empty. /// Executes all jobs in the queue.
pub fn process(&mut self, ctx: &mut ExecuteContext<'_>) -> bool { pub fn execute(&mut self, ctx: &mut ExecuteContext<'_>) {
let mut remaining = MAX_COUNT_BEFORE_YIELD; while let Some((Reverse(queue_dependencies_distance), task_id)) = self.queue.pop() {
while remaining > 0 { let &mut LeafDistanceUpdate {
if let Some((Reverse(queue_dependencies_distance), task_id)) = self.queue.pop() { dependencies_distance,
let &mut LeafDistanceUpdate { dependencies_max_distance_in_buffer,
dependencies_distance, ref mut done,
dependencies_max_distance_in_buffer,
ref mut done,
#[cfg(feature = "trace_leaf_distance_update")]
ref span,
} = self.leaf_distance_updates.get_mut(&task_id).unwrap();
if queue_dependencies_distance != dependencies_distance {
// Stale entry in queue
// Re-enqueue to keep the ordering correct
self.queue.push((Reverse(dependencies_distance), task_id));
continue;
}
#[cfg(feature = "trace_leaf_distance_update")] #[cfg(feature = "trace_leaf_distance_update")]
let _guard = span.as_ref().map(|s| s.clone().entered()); ref span,
*done = true; } = self.leaf_distance_updates.get_mut(&task_id).unwrap();
self.update_leaf_distance( if queue_dependencies_distance != dependencies_distance {
ctx, // Stale entry in queue
task_id, // Re-enqueue to keep the ordering correct
dependencies_distance, self.queue.push((Reverse(dependencies_distance), task_id));
dependencies_max_distance_in_buffer, continue;
);
remaining -= 1;
} else {
return true;
} }
#[cfg(feature = "trace_leaf_distance_update")]
let _guard = span.as_ref().map(|s| s.clone().entered());
*done = true;
self.update_leaf_distance(
ctx,
task_id,
dependencies_distance,
dependencies_max_distance_in_buffer,
);
} }
false
} }
fn update_leaf_distance( fn update_leaf_distance(
...@@ -168,11 +154,4 @@ impl LeafDistanceUpdateQueue { ...@@ -168,11 +154,4 @@ impl LeafDistanceUpdateQueue {
} }
task.set_leaf_distance(leaf_distance); task.set_leaf_distance(leaf_distance);
} }
pub fn execute(&mut self, ctx: &mut ExecuteContext<'_>) {
if self.is_empty() {
return;
}
while !self.process(ctx) {}
}
} }
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