Skip to content

Commit

Permalink
improve performance of the graph aggregation (#8686)
Browse files Browse the repository at this point in the history
### Description

Improves performance for larger applications by up to 30%
improves memory usage a bit

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored Jul 8, 2024
1 parent 7bde147 commit d5975d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions crates/turbo-tasks-memory/src/aggregation/followers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub fn on_added<C: AggregationContext>(
};
let followers_len = aggregating.followers.len();
let optimize = (!already_optimizing_for_node
&& followers_len > MAX_FOLLOWERS
&& (followers_len - MAX_FOLLOWERS).count_ones() == 1)
&& followers_len >= MAX_FOLLOWERS
&& followers_len.count_ones() == 1)
.then(|| {
aggregating
.followers
Expand Down
34 changes: 18 additions & 16 deletions crates/turbo-tasks-memory/src/aggregation/increase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
balance_queue::BalanceQueue, AggegatingNode, AggregationContext, AggregationNode,
AggregationNodeGuard, PreparedInternalOperation, PreparedOperation, StackVec,
};
pub(super) const LEAF_NUMBER: u32 = 64;
pub(super) const LEAF_NUMBER: u32 = 16;

#[derive(Debug)]
pub enum IncreaseReason {
Expand Down Expand Up @@ -179,21 +179,23 @@ impl<C: AggregationContext> PreparedInternalOperation<C>
uppers,
reason,
} => {
let mut need_to_run = true;
while need_to_run {
need_to_run = false;
let mut max = 0;
for upper_id in &uppers {
let upper = ctx.node(upper_id);
let aggregation_number = upper.aggregation_number();
if aggregation_number != u32::MAX {
if aggregation_number > max {
max = aggregation_number;
}
if aggregation_number == target_aggregation_number {
target_aggregation_number += 1;
if max >= target_aggregation_number {
need_to_run = true;
if target_aggregation_number >= LEAF_NUMBER {
let mut need_to_run = true;
while need_to_run {
need_to_run = false;
let mut max = 0;
for upper_id in &uppers {
let upper = ctx.node(upper_id);
let aggregation_number = upper.aggregation_number();
if aggregation_number != u32::MAX {
if aggregation_number > max {
max = aggregation_number;
}
if aggregation_number == target_aggregation_number {
target_aggregation_number += 1;
if max >= target_aggregation_number {
need_to_run = true;
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/turbo-tasks-memory/src/aggregation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ fn chain() {
let aggregated = aggregation_data(&ctx, &current);
assert_eq!(aggregated.value, 15050);
}
assert_eq!(ctx.additions.load(Ordering::SeqCst), 122);
assert_eq!(ctx.additions.load(Ordering::SeqCst), 182);
ctx.additions.store(0, Ordering::SeqCst);
check_invariants(&ctx, once(current.clone()));

Expand All @@ -609,8 +609,8 @@ fn chain() {
check_invariants(&ctx, once(current.clone()));

leaf.incr(&ctx);
// The change need to propagate through 2 aggregated nodes
assert_eq!(ctx.additions.load(Ordering::SeqCst), 2);
// The change need to propagate through 4 aggregated nodes
assert_eq!(ctx.additions.load(Ordering::SeqCst), 4);
ctx.additions.store(0, Ordering::SeqCst);

{
Expand Down Expand Up @@ -640,7 +640,7 @@ fn chain() {

leaf.incr(&ctx);
// This should be less the 20 to prove that we are reusing trees
assert_eq!(ctx.additions.load(Ordering::SeqCst), 3);
assert_eq!(ctx.additions.load(Ordering::SeqCst), 5);
ctx.additions.store(0, Ordering::SeqCst);

{
Expand Down Expand Up @@ -678,10 +678,10 @@ fn chain_double_connected() {

{
let aggregated = aggregation_data(&ctx, &current);
assert_eq!(aggregated.value, 13188);
assert_eq!(aggregated.value, 20017);
}
check_invariants(&ctx, once(current.clone()));
assert_eq!(ctx.additions.load(Ordering::SeqCst), 285);
assert_eq!(ctx.additions.load(Ordering::SeqCst), 643);
ctx.additions.store(0, Ordering::SeqCst);

print(&ctx, &current, true);
Expand Down
7 changes: 3 additions & 4 deletions crates/turbo-tasks-memory/src/aggregation/uppers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ pub fn on_added<C: AggregationContext>(
) -> usize {
let uppers = node.uppers();
let uppers_len = uppers.len();
let optimize = (!already_optimizing_for_upper
&& uppers_len > MAX_UPPERS
&& (uppers_len - MAX_UPPERS).count_ones() == 1)
.then(|| (true, uppers.iter().cloned().collect::<StackVec<_>>()));
let optimize =
(!already_optimizing_for_upper && uppers_len >= MAX_UPPERS && uppers_len.count_ones() == 1)
.then(|| (true, uppers.iter().cloned().collect::<StackVec<_>>()));
let (add_change, followers) = match &mut *node {
AggregationNode::Leaf { .. } => {
let add_change = node.get_add_change();
Expand Down

0 comments on commit d5975d4

Please sign in to comment.