Skip to content

Commit 4d7284c

Browse files
committed
Turbopack: shard amount need to grow quadratic to cpu count to keep propability of conflicts constant
1 parent fcb1dab commit 4d7284c

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ pub struct BackendOptions {
134134
/// Enables the backing storage.
135135
pub storage_mode: Option<StorageMode>,
136136

137+
/// Number of tokio worker threads. It will be used to compute the shard amount of parallel
138+
/// datastructures. If `None`, it will use the available parallelism.
139+
pub num_workers: Option<usize>,
140+
137141
/// Avoid big preallocations for faster startup. Should only be used for testing purposes.
138142
pub small_preallocation: bool,
139143
}
@@ -144,6 +148,7 @@ impl Default for BackendOptions {
144148
dependency_tracking: true,
145149
active_tracking: true,
146150
storage_mode: Some(StorageMode::ReadWrite),
151+
num_workers: None,
147152
small_preallocation: false,
148153
}
149154
}
@@ -235,6 +240,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
235240
options.active_tracking = false;
236241
}
237242
let small_preallocation = options.small_preallocation;
243+
let num_workers = options.num_workers;
238244
let next_task_id = backing_storage
239245
.next_free_task_id()
240246
.expect("Failed to get task id");
@@ -256,7 +262,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
256262
task_cache: BiMap::new(),
257263
transient_tasks: FxDashMap::default(),
258264
local_is_partial: AtomicBool::new(next_task_id != TaskId::MIN),
259-
storage: Storage::new(small_preallocation),
265+
storage: Storage::new(num_workers, small_preallocation),
260266
in_progress_operations: AtomicUsize::new(0),
261267
snapshot_request: Mutex::new(SnapshotRequest::new()),
262268
operations_suspended: Condvar::new(),

turbopack/crates/turbo-tasks-backend/src/backend/storage.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,17 +616,19 @@ pub struct Storage {
616616
}
617617

618618
impl Storage {
619-
pub fn new(small_preallocation: bool) -> Self {
619+
pub fn new(num_worker: Option<usize>, small_preallocation: bool) -> Self {
620620
let map_capacity: usize = if small_preallocation {
621621
1024
622622
} else {
623623
1024 * 1024
624624
};
625625
let modified_capacity: usize = if small_preallocation { 0 } else { 1024 };
626-
let shard_factor: usize = if small_preallocation { 4 } else { 64 };
626+
let shard_factor: usize = if small_preallocation { 1 } else { 16 };
627627

628-
let shard_amount =
629-
(available_parallelism().map_or(4, |v| v.get()) * shard_factor).next_power_of_two();
628+
let num_workers =
629+
num_worker.unwrap_or_else(|| available_parallelism().map_or(4, |v| v.get()));
630+
631+
let shard_amount = (num_workers * num_workers * shard_factor).next_power_of_two();
630632

631633
Self {
632634
snapshot_mode: AtomicBool::new(false),

turbopack/crates/turbo-tasks-backend/tests/test_config.trs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
std::fs::create_dir_all(&path).unwrap();
88
turbo_tasks::TurboTasks::new(
99
turbo_tasks_backend::TurboTasksBackend::new(
10-
turbo_tasks_backend::BackendOptions::default(),
10+
turbo_tasks_backend::BackendOptions {
11+
num_workers: Some(2),
12+
small_preallocation: true,
13+
..Default::default()
14+
},
1115
turbo_tasks_backend::default_backing_storage(
1216
path.as_path(),
1317
&turbo_tasks_backend::GitVersionInfo {

turbopack/crates/turbo-tasks-fetch/tests/test_config.trs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
std::fs::create_dir_all(&path).unwrap();
88
turbo_tasks::TurboTasks::new(
99
turbo_tasks_backend::TurboTasksBackend::new(
10-
turbo_tasks_backend::BackendOptions::default(),
10+
turbo_tasks_backend::BackendOptions {
11+
num_workers: Some(2),
12+
small_preallocation: true,
13+
..Default::default()
14+
},
1115
turbo_tasks_backend::default_backing_storage(
1216
path.as_path(),
1317
&turbo_tasks_backend::GitVersionInfo {

turbopack/crates/turbo-tasks-macros-tests/tests/test_config.trs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
|_name, _initial| {
22
turbo_tasks::TurboTasks::new(
33
turbo_tasks_backend::TurboTasksBackend::new(
4-
turbo_tasks_backend::BackendOptions::default(),
4+
turbo_tasks_backend::BackendOptions {
5+
num_workers: Some(2),
6+
small_preallocation: true,
7+
..Default::default()
8+
},
59
turbo_tasks_backend::noop_backing_storage(),
610
)
711
)

0 commit comments

Comments
 (0)