Unverified Commit be0a2550 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] allow to disable active tracking in turbo-tasks (#75899)

### What?

* allow to disable active tracking
* disable active tracking when dependency tracking is disabled

This allows to skip a lot of active counter increases and decreases for
better performance.

Without dependency tracking there can't be any inactive tasks in the
system, so we don't need to track that at all.
parent f50164ed
...@@ -121,6 +121,13 @@ pub struct BackendOptions { ...@@ -121,6 +121,13 @@ pub struct BackendOptions {
/// considered as active. Collectibles are disabled. /// considered as active. Collectibles are disabled.
pub children_tracking: bool, pub children_tracking: bool,
/// Enables active tracking.
///
/// Automatically disabled when `dependency_tracking` is disabled.
///
/// When disabled: All tasks are considered as active.
pub active_tracking: bool,
/// Enables the backing storage. /// Enables the backing storage.
pub storage_mode: Option<StorageMode>, pub storage_mode: Option<StorageMode>,
} }
...@@ -130,6 +137,7 @@ impl Default for BackendOptions { ...@@ -130,6 +137,7 @@ impl Default for BackendOptions {
Self { Self {
dependency_tracking: true, dependency_tracking: true,
children_tracking: true, children_tracking: true,
active_tracking: true,
storage_mode: Some(StorageMode::ReadWrite), storage_mode: Some(StorageMode::ReadWrite),
} }
} }
...@@ -194,10 +202,13 @@ impl<B: BackingStorage> TurboTasksBackend<B> { ...@@ -194,10 +202,13 @@ impl<B: BackingStorage> TurboTasksBackend<B> {
} }
impl<B: BackingStorage> TurboTasksBackendInner<B> { impl<B: BackingStorage> TurboTasksBackendInner<B> {
pub fn new(options: BackendOptions, backing_storage: B) -> Self { pub fn new(mut options: BackendOptions, backing_storage: B) -> Self {
let shard_amount = let shard_amount =
(available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two(); (available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two();
let need_log = matches!(options.storage_mode, Some(StorageMode::ReadWrite)); let need_log = matches!(options.storage_mode, Some(StorageMode::ReadWrite));
if !options.dependency_tracking {
options.active_tracking = false;
}
Self { Self {
options, options,
start_time: Instant::now(), start_time: Instant::now(),
...@@ -340,6 +351,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -340,6 +351,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
self.options.dependency_tracking self.options.dependency_tracking
} }
fn should_track_activeness(&self) -> bool {
self.options.active_tracking
}
fn should_track_children(&self) -> bool { fn should_track_children(&self) -> bool {
self.options.children_tracking self.options.children_tracking
} }
...@@ -499,16 +514,18 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -499,16 +514,18 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// is automatically removed when this task is clean. // is automatically removed when this task is clean.
get_mut_or_insert_with!(task, Activeness, || ActivenessState::new(task_id)) get_mut_or_insert_with!(task, Activeness, || ActivenessState::new(task_id))
.set_active_until_clean(); .set_active_until_clean();
// A newly added Activeness need to make sure to schedule the tasks if ctx.should_track_activeness() {
task_ids_to_schedule = get_many!( // A newly added Activeness need to make sure to schedule the tasks
task, task_ids_to_schedule = get_many!(
AggregatedDirtyContainer { task,
task AggregatedDirtyContainer {
} count if count.get(self.session_id) > 0 => { task
task } count if count.get(self.session_id) > 0 => {
} task
); }
task_ids_to_schedule.push(task_id); );
task_ids_to_schedule.push(task_id);
}
get!(task, Activeness).unwrap() get!(task, Activeness).unwrap()
}; };
let listener = root.all_clean_event.listen_with_note(move || { let listener = root.all_clean_event.listen_with_note(move || {
...@@ -1403,14 +1420,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1403,14 +1420,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let mut queue = AggregationUpdateQueue::new(); let mut queue = AggregationUpdateQueue::new();
if has_children { if has_children {
let has_active_count = let has_active_count = ctx.should_track_activeness()
get!(task, Activeness).map_or(false, |activeness| activeness.active_counter > 0); && get!(task, Activeness).map_or(false, |activeness| activeness.active_counter > 0);
connect_children( connect_children(
task_id, task_id,
&mut task, &mut task,
new_children, new_children,
&mut queue, &mut queue,
has_active_count, has_active_count,
ctx.should_track_activeness(),
); );
} }
...@@ -1855,9 +1873,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1855,9 +1873,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
effective: u32::MAX, effective: u32::MAX,
}, },
}); });
task.add(CachedDataItem::Activeness { if self.should_track_activeness() {
value: ActivenessState::new_root(root_type, task_id), task.add(CachedDataItem::Activeness {
}); value: ActivenessState::new_root(root_type, task_id),
});
}
task.add(CachedDataItem::new_scheduled(move || match root_type { task.add(CachedDataItem::new_scheduled(move || match root_type {
RootType::RootTask => "Root Task".to_string(), RootType::RootTask => "Root Task".to_string(),
RootType::OnceTask => "Once Task".to_string(), RootType::OnceTask => "Once Task".to_string(),
......
...@@ -102,8 +102,9 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -102,8 +102,9 @@ impl Operation for CleanupOldEdgesOperation {
}); });
} else { } else {
let upper_ids = get_uppers(&task); let upper_ids = get_uppers(&task);
let has_active_count = get!(task, Activeness) let has_active_count = ctx.should_track_activeness()
.is_some_and(|a| a.active_counter > 0); && get!(task, Activeness)
.is_some_and(|a| a.active_counter > 0);
drop(task); drop(task);
if has_active_count { if has_active_count {
// TODO combine both operations to avoid the clone // TODO combine both operations to avoid the clone
......
...@@ -26,7 +26,7 @@ pub enum ConnectChildOperation { ...@@ -26,7 +26,7 @@ pub enum ConnectChildOperation {
impl ConnectChildOperation { impl ConnectChildOperation {
pub fn run(parent_task_id: TaskId, child_task_id: TaskId, mut ctx: impl ExecuteContext) { pub fn run(parent_task_id: TaskId, child_task_id: TaskId, mut ctx: impl ExecuteContext) {
if !ctx.should_track_children() { if !ctx.should_track_children() {
let mut task = ctx.task(child_task_id, TaskDataCategory::Data); let mut task = ctx.task(child_task_id, TaskDataCategory::All);
if !task.has_key(&CachedDataItemKey::Output {}) { if !task.has_key(&CachedDataItemKey::Output {}) {
let description = ctx.get_task_desc_fn(child_task_id); let description = ctx.get_task_desc_fn(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(description)); let should_schedule = task.add(CachedDataItem::new_scheduled(description));
...@@ -67,9 +67,21 @@ impl ConnectChildOperation { ...@@ -67,9 +67,21 @@ impl ConnectChildOperation {
}); });
} }
queue.push(AggregationUpdateJob::IncreaseActiveCount { if ctx.should_track_activeness() {
task: child_task_id, queue.push(AggregationUpdateJob::IncreaseActiveCount {
}); task: child_task_id,
});
} else {
let mut task = ctx.task(child_task_id, TaskDataCategory::All);
if !task.has_key(&CachedDataItemKey::Output {}) {
let description = ctx.get_task_desc_fn(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(description));
drop(task);
if should_schedule {
ctx.schedule(child_task_id);
}
}
}
ConnectChildOperation::UpdateAggregation { ConnectChildOperation::UpdateAggregation {
aggregation_update: queue, aggregation_update: queue,
......
...@@ -16,6 +16,7 @@ pub fn connect_children( ...@@ -16,6 +16,7 @@ pub fn connect_children(
new_children: FxHashSet<TaskId>, new_children: FxHashSet<TaskId>,
queue: &mut AggregationUpdateQueue, queue: &mut AggregationUpdateQueue,
has_active_count: bool, has_active_count: bool,
should_track_activeness: bool,
) { ) {
if new_children.is_empty() { if new_children.is_empty() {
return; return;
...@@ -49,7 +50,7 @@ pub fn connect_children( ...@@ -49,7 +50,7 @@ pub fn connect_children(
// We need to decrease the active count because we temporarily increased it during // We need to decrease the active count because we temporarily increased it during
// connect_child. We need to increase the active count when the parent has active // connect_child. We need to increase the active count when the parent has active
// count, because it's added as follower. // count, because it's added as follower.
if !has_active_count { if should_track_activeness && !has_active_count {
queue.push(AggregationUpdateJob::DecreaseActiveCounts { queue.push(AggregationUpdateJob::DecreaseActiveCounts {
task_ids: new_follower_ids, task_ids: new_follower_ids,
}) })
...@@ -62,8 +63,10 @@ pub fn connect_children( ...@@ -62,8 +63,10 @@ pub fn connect_children(
}); });
// We need to decrease the active count because we temporarily increased it during // We need to decrease the active count because we temporarily increased it during
// connect_child. // connect_child.
queue.push(AggregationUpdateJob::DecreaseActiveCounts { if should_track_activeness {
task_ids: new_follower_ids, queue.push(AggregationUpdateJob::DecreaseActiveCounts {
}) task_ids: new_follower_ids,
});
}
} }
} }
...@@ -22,7 +22,6 @@ use crate::{ ...@@ -22,7 +22,6 @@ use crate::{
#[derive(Serialize, Deserialize, Clone, Default)] #[derive(Serialize, Deserialize, Clone, Default)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum InvalidateOperation { pub enum InvalidateOperation {
// TODO DetermineActiveness
MakeDirty { MakeDirty {
task_ids: SmallVec<[TaskId; 4]>, task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
...@@ -274,7 +273,7 @@ pub fn make_task_dirty_internal( ...@@ -274,7 +273,7 @@ pub fn make_task_dirty_internal(
AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update), AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update),
)); ));
} }
task.has_key(&CachedDataItemKey::Activeness {}) !ctx.should_track_activeness() || task.has_key(&CachedDataItemKey::Activeness {})
} else { } else {
true true
}; };
......
...@@ -90,6 +90,7 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -90,6 +90,7 @@ pub trait ExecuteContext<'e>: Sized {
fn get_task_description(&self, task_id: TaskId) -> String; fn get_task_description(&self, task_id: TaskId) -> String;
fn should_track_children(&self) -> bool; fn should_track_children(&self) -> bool;
fn should_track_dependencies(&self) -> bool; fn should_track_dependencies(&self) -> bool;
fn should_track_activeness(&self) -> bool;
} }
pub struct ParentRef<'a> { pub struct ParentRef<'a> {
...@@ -357,6 +358,10 @@ where ...@@ -357,6 +358,10 @@ where
fn should_track_dependencies(&self) -> bool { fn should_track_dependencies(&self) -> bool {
self.backend.should_track_dependencies() self.backend.should_track_dependencies()
} }
fn should_track_activeness(&self) -> bool {
self.backend.should_track_activeness()
}
} }
pub trait TaskGuard: Debug { pub trait TaskGuard: Debug {
......
...@@ -171,6 +171,7 @@ async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsRe ...@@ -171,6 +171,7 @@ async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsRe
let tt = TurboTasks::new(TurboTasksBackend::new( let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions { BackendOptions {
storage_mode: None, storage_mode: None,
dependency_tracking: false,
..Default::default() ..Default::default()
}, },
noop_backing_storage(), noop_backing_storage(),
......
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