-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Turbopack: shard amount need to grow quadratic to cpu count to keep propability of conflicts constant #84921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
turbopack/crates/turbo-tasks-backend/src/utils/shard_amount.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::thread::available_parallelism; | ||
|
||
/// Compute a good number of shards to use for sharded data structures. | ||
/// The number of shards is computed based on the number of worker threads | ||
/// and whether a small preallocation is requested. | ||
/// A small preallocation is useful for tests where performance is not | ||
/// critical and we want to reduce memory usage and startup time. | ||
/// The number of shards is chosen to minimize the probability of shard | ||
/// collisions (which can lead to false sharing) while keeping memory | ||
/// usage reasonable. | ||
/// The returned number is always a power of two as this is often required | ||
/// by sharded data structures. The maximum shard amount is capped at 1 << 16 (65536). | ||
pub fn compute_shard_amount(num_workers: Option<usize>, small_preallocation: bool) -> usize { | ||
let num_workers = num_workers.unwrap_or_else(|| available_parallelism().map_or(4, |v| v.get())); | ||
|
||
// Once can compute the probability of a shard collision (which leads to false sharing) using | ||
// the birthday paradox formula. It's notable that the probability of collisions increases | ||
// with more worker threads. To mitigate this effect, the number of shards need to grow | ||
// quadratically with the number of worker threads. This way the probability of at least one | ||
// collision remains constant. | ||
// | ||
// Lets call the worker thread count `N` and the number of shards `S`. When using `S = k * N^2` | ||
// for some constant `k` the probability of at least one collision for large `N` can be | ||
// approximated as: P = 1 - exp(-N^2 / (2*S)) = 1 - exp(-1/(2*k)) | ||
// | ||
// For `k = 16` this results in a collision probability of about 3%. | ||
// For `k = 1` this results in a collision probability of about 39%. | ||
// | ||
// We clamp the number of shards to 1 << 16 to avoid excessive memory usage in case of a very | ||
// high number of worker threads. This case is hit with more than 64 worker threads for `k = | ||
// 16` and more than 256 worker threads for `k = 1`. | ||
|
||
if small_preallocation { | ||
// We also clamp the minimum number of workers to 256 so all following multiplications can't | ||
// overflow. | ||
let num_workers = num_workers.max(256); | ||
(num_workers * num_workers).next_power_of_two() | ||
} else { | ||
// We also clamp the minimum number of workers to 64 so all following multiplications can't | ||
// overflow. | ||
let num_workers = num_workers.max(64); | ||
(num_workers * num_workers * 16).next_power_of_two() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.