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