Skip to content

Commit 831ecf0

Browse files
authored
TaskPool: Prefer task completion over executing new tasks (#18009)
# Objective - Systems that use the task pool, either explicitly or implicitly using parallel queries, will often end up executing tasks from different systems. - This can cause random tasks to block the main or render schedule at random, adding frame variance and increasing frame times when CPU bound. - This profile is a common occurrence on `main`. `propagate_parent_transforms` takes more than twice as long as it should, blocking the main schedule for that time, because it uses `task pool.scope`, which has decided to execute tasks from the render schedule on the main schedule. ![image](https://github.com/user-attachments/assets/c363be40-82ce-451e-ba29-3eb4ee367e0b) ## Solution - In task pool scope execution, prefer to check if the current task is complete instead of ticking the executor to find new work. ## Testing - Ran the scene viewer with tracy to look for images like the one in the objective section. - Things look much, much better, and I could not find any occurrences: ![image](https://github.com/user-attachments/assets/18b79394-1a7c-49c1-820a-f5207e81bbac) ![image](https://github.com/user-attachments/assets/e7d4831d-66c3-41c1-ae2c-a624724c9965)
1 parent f2b37c7 commit 831ecf0

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

crates/bevy_tasks/src/task_pool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl TaskPool {
486486
.is_ok();
487487
}
488488
};
489-
execute_forever.or(get_results).await
489+
get_results.or(execute_forever).await
490490
}
491491

492492
#[inline]
@@ -505,7 +505,7 @@ impl TaskPool {
505505
let _result = AssertUnwindSafe(tick_forever).catch_unwind().await.is_ok();
506506
}
507507
};
508-
execute_forever.or(get_results).await
508+
get_results.or(execute_forever).await
509509
}
510510

511511
#[inline]
@@ -527,7 +527,7 @@ impl TaskPool {
527527
.is_ok();
528528
}
529529
};
530-
execute_forever.or(get_results).await
530+
get_results.or(execute_forever).await
531531
}
532532

533533
#[inline]
@@ -545,7 +545,7 @@ impl TaskPool {
545545
let _result = AssertUnwindSafe(tick_forever).catch_unwind().await.is_ok();
546546
}
547547
};
548-
execute_forever.or(get_results).await
548+
get_results.or(execute_forever).await
549549
}
550550

551551
/// Spawns a static future onto the thread pool. The returned [`Task`] is a

0 commit comments

Comments
 (0)