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 {
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) {}
queue.execute(&mut ctx);
}
// Collect all active roots
......
......@@ -336,22 +336,17 @@ fn cleanup_old_edges_inner(
}
}
while !stop_when_only_rebalance_remains || !queue.only_rebalance_remains() {
if queue.process(ctx) {
#[cfg(feature = "trace_aggregation_update_stats")]
let stats = take(&mut queue.stats);
#[cfg(not(feature = "trace_aggregation_update_stats"))]
let stats = ();
return (stats, None);
}
if stop_when_only_rebalance_remains {
// Edge removal is done; hand the rebalance back to the caller. Any
// other pending work would be dropped here, so `execute_until_rebalance`
// asserts that nothing else is left.
let balance_edges = queue.execute_until_rebalance(ctx);
return (Default::default(), Some(DeferredCleanup { balance_edges }));
}
// Edge removal is done; hand the rebalance back to the caller. Any
// other pending work would be dropped here, so `only_rebalance_remains`
// asserts that nothing else is left.
(
Default::default(),
Some(DeferredCleanup {
balance_edges: queue.take_deferred_balance_edges().collect(),
}),
)
queue.execute(ctx);
#[cfg(feature = "trace_aggregation_update_stats")]
let stats = take(&mut queue.stats);
#[cfg(not(feature = "trace_aggregation_update_stats"))]
let stats = ();
(stats, None)
}
......@@ -12,9 +12,6 @@ use crate::backend::{
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.
/// 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.
......@@ -52,10 +49,6 @@ impl LeafDistanceUpdateQueue {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
pub fn push(
&mut self,
task_id: TaskId,
......@@ -84,39 +77,32 @@ impl LeafDistanceUpdateQueue {
};
}
/// Executes a single step of the queue. Returns true, when the queue is empty.
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() {
let &mut LeafDistanceUpdate {
dependencies_distance,
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;
}
/// Executes all jobs in the queue.
pub fn execute(&mut self, ctx: &mut ExecuteContext<'_>) {
while let Some((Reverse(queue_dependencies_distance), task_id)) = self.queue.pop() {
let &mut LeafDistanceUpdate {
dependencies_distance,
dependencies_max_distance_in_buffer,
ref mut done,
#[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,
);
remaining -= 1;
} else {
return true;
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")]
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(
......@@ -168,11 +154,4 @@ impl LeafDistanceUpdateQueue {
}
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