Unverified Commit 2cc54081 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: refactor backend jobs (#83019)

### What?

Use associated types to avoid Box and Any.

* avoids any casting
* avoid extra vec collecting
* avoid cloning lists for parallelization
parent e5311667
...@@ -3,12 +3,13 @@ mod operation; ...@@ -3,12 +3,13 @@ mod operation;
mod storage; mod storage;
use std::{ use std::{
any::Any,
borrow::Cow, borrow::Cow,
cmp::min,
fmt::{self, Write}, fmt::{self, Write},
future::Future, future::Future,
hash::BuildHasherDefault, hash::BuildHasherDefault,
mem::take, mem::take,
ops::Range,
pin::Pin, pin::Pin,
sync::{ sync::{
Arc, Arc,
...@@ -26,11 +27,11 @@ use smallvec::{SmallVec, smallvec}; ...@@ -26,11 +27,11 @@ use smallvec::{SmallVec, smallvec};
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
use tracing::{field::Empty, info_span}; use tracing::{field::Empty, info_span};
use turbo_tasks::{ use turbo_tasks::{
CellId, FxDashMap, KeyValuePair, RawVc, ReadCellOptions, ReadConsistency, SessionId, CellId, FxDashMap, FxIndexMap, KeyValuePair, RawVc, ReadCellOptions, ReadConsistency,
TRANSIENT_TASK_BIT, TaskExecutionReason, TaskId, TraitTypeId, TurboTasksBackendApi, SessionId, TRANSIENT_TASK_BIT, TaskExecutionReason, TaskId, TraitTypeId, TurboTasksBackendApi,
ValueTypeId, ValueTypeId,
backend::{ backend::{
Backend, BackendJobId, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot, Backend, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
TransientTaskType, TurboTasksExecutionError, TypedCellContent, TransientTaskType, TurboTasksExecutionError, TypedCellContent,
}, },
event::{Event, EventListener}, event::{Event, EventListener},
...@@ -39,7 +40,7 @@ use turbo_tasks::{ ...@@ -39,7 +40,7 @@ use turbo_tasks::{
task_statistics::TaskStatisticsApi, task_statistics::TaskStatisticsApi,
trace::TraceRawVcs, trace::TraceRawVcs,
turbo_tasks, turbo_tasks,
util::{IdFactoryWithReuse, good_chunk_size, into_chunks}, util::{IdFactoryWithReuse, good_chunk_size},
}; };
pub use self::{operation::AnyOperation, storage::TaskDataCategory}; pub use self::{operation::AnyOperation, storage::TaskDataCategory};
...@@ -70,11 +71,6 @@ use crate::{ ...@@ -70,11 +71,6 @@ use crate::{
}, },
}; };
const BACKEND_JOB_INITIAL_SNAPSHOT: BackendJobId = unsafe { BackendJobId::new_unchecked(1) };
const BACKEND_JOB_FOLLOW_UP_SNAPSHOT: BackendJobId = unsafe { BackendJobId::new_unchecked(2) };
const BACKEND_JOB_PREFETCH_TASK: BackendJobId = unsafe { BackendJobId::new_unchecked(3) };
const BACKEND_JOB_PREFETCH_CHUNK_TASK: BackendJobId = unsafe { BackendJobId::new_unchecked(4) };
const SNAPSHOT_REQUESTED_BIT: usize = 1 << (usize::BITS - 1); const SNAPSHOT_REQUESTED_BIT: usize = 1 << (usize::BITS - 1);
struct SnapshotRequest { struct SnapshotRequest {
...@@ -160,6 +156,15 @@ impl Default for BackendOptions { ...@@ -160,6 +156,15 @@ impl Default for BackendOptions {
} }
} }
pub enum TurboTasksBackendJob {
InitialSnapshot,
FollowUpSnapshot,
Prefetch {
data: Arc<FxIndexMap<TaskId, bool>>,
range: Option<Range<usize>>,
},
}
pub struct TurboTasksBackend<B: BackingStorage>(Arc<TurboTasksBackendInner<B>>); pub struct TurboTasksBackend<B: BackingStorage>(Arc<TurboTasksBackendInner<B>>);
type TaskCacheLog = Sharded<ChunkedVec<(Arc<CachedTaskType>, TaskId)>>; type TaskCacheLog = Sharded<ChunkedVec<(Arc<CachedTaskType>, TaskId)>>;
...@@ -1210,7 +1215,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1210,7 +1215,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
if self.should_persist() { if self.should_persist() {
// Schedule the snapshot job // Schedule the snapshot job
turbo_tasks.schedule_backend_background_job(BACKEND_JOB_INITIAL_SNAPSHOT, None); turbo_tasks.schedule_backend_background_job(TurboTasksBackendJob::InitialSnapshot);
} }
} }
...@@ -2135,12 +2140,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2135,12 +2140,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
fn run_backend_job<'a>( fn run_backend_job<'a>(
self: &'a Arc<Self>, self: &'a Arc<Self>,
id: BackendJobId, job: TurboTasksBackendJob,
data: Option<Box<dyn Any + Send>>,
turbo_tasks: &'a dyn TurboTasksBackendApi<TurboTasksBackend<B>>, turbo_tasks: &'a dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
Box::pin(async move { Box::pin(async move {
if id == BACKEND_JOB_INITIAL_SNAPSHOT || id == BACKEND_JOB_FOLLOW_UP_SNAPSHOT { match job {
TurboTasksBackendJob::InitialSnapshot | TurboTasksBackendJob::FollowUpSnapshot => {
debug_assert!(self.should_persist()); debug_assert!(self.should_persist());
let last_snapshot = self.last_snapshot.load(Ordering::Relaxed); let last_snapshot = self.last_snapshot.load(Ordering::Relaxed);
...@@ -2150,7 +2155,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2150,7 +2155,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
const SNAPSHOT_INTERVAL: Duration = Duration::from_secs(120); const SNAPSHOT_INTERVAL: Duration = Duration::from_secs(120);
const IDLE_TIMEOUT: Duration = Duration::from_secs(2); const IDLE_TIMEOUT: Duration = Duration::from_secs(2);
let time = if id == BACKEND_JOB_INITIAL_SNAPSHOT { let time = if matches!(job, TurboTasksBackendJob::InitialSnapshot) {
FIRST_SNAPSHOT_WAIT FIRST_SNAPSHOT_WAIT
} else { } else {
SNAPSHOT_INTERVAL SNAPSHOT_INTERVAL
...@@ -2207,20 +2212,39 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2207,20 +2212,39 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
Ordering::Relaxed, Ordering::Relaxed,
); );
turbo_tasks turbo_tasks.schedule_backend_background_job(
.schedule_backend_background_job(BACKEND_JOB_FOLLOW_UP_SNAPSHOT, None); TurboTasksBackendJob::FollowUpSnapshot,
);
return; return;
} }
} }
} else if id == BACKEND_JOB_PREFETCH_TASK || id == BACKEND_JOB_PREFETCH_CHUNK_TASK { }
const DATA_EXPECTATION: &str = TurboTasksBackendJob::Prefetch { data, range } => {
"Expected data to be a FxHashMap<TaskId, bool> for BACKEND_JOB_PREFETCH_TASK"; let range = if let Some(range) = range {
let data = Box::<dyn Any + Send>::downcast::<Vec<(TaskId, bool)>>( range
data.expect(DATA_EXPECTATION), } else {
) if data.len() > 128 {
.expect(DATA_EXPECTATION); let chunk_size = good_chunk_size(data.len());
let chunks = data.len().div_ceil(chunk_size);
for i in 0..chunks {
turbo_tasks.schedule_backend_foreground_job(
TurboTasksBackendJob::Prefetch {
data: data.clone(),
range: Some(
(i * chunk_size)..min(data.len(), (i + 1) * chunk_size),
),
},
);
}
return;
}
0..data.len()
};
fn prefetch_task(ctx: &mut impl ExecuteContext<'_>, task: TaskId, with_data: bool) { let _span = info_span!("prefetching").entered();
let mut ctx = self.execute_context(turbo_tasks);
for i in range {
let (&task, &with_data) = data.get_index(i).unwrap();
let category = if with_data { let category = if with_data {
TaskDataCategory::All TaskDataCategory::All
} else { } else {
...@@ -2229,22 +2253,6 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2229,22 +2253,6 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// Prefetch the task // Prefetch the task
drop(ctx.task(task, category)); drop(ctx.task(task, category));
} }
if id == BACKEND_JOB_PREFETCH_TASK && data.len() > 128 {
let chunk_size = good_chunk_size(data.len());
for chunk in into_chunks(*data, chunk_size) {
let data: Box<Vec<(TaskId, bool)>> = Box::new(chunk.collect::<Vec<_>>());
turbo_tasks.schedule_backend_foreground_job(
BACKEND_JOB_PREFETCH_CHUNK_TASK,
Some(data),
);
}
} else {
let _span = info_span!("prefetching").entered();
let mut ctx = self.execute_context(turbo_tasks);
for (task, with_data) in data.into_iter() {
prefetch_task(&mut ctx, task, with_data);
}
} }
} }
}) })
...@@ -2934,13 +2942,14 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> { ...@@ -2934,13 +2942,14 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
) )
} }
type BackendJob = TurboTasksBackendJob;
fn run_backend_job<'a>( fn run_backend_job<'a>(
&'a self, &'a self,
id: BackendJobId, job: Self::BackendJob,
data: Option<Box<dyn Any + Send>>,
turbo_tasks: &'a dyn TurboTasksBackendApi<Self>, turbo_tasks: &'a dyn TurboTasksBackendApi<Self>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
self.0.run_backend_job(id, data, turbo_tasks) self.0.run_backend_job(job, turbo_tasks)
} }
fn try_read_task_output( fn try_read_task_output(
......
...@@ -11,17 +11,16 @@ mod update_output; ...@@ -11,17 +11,16 @@ mod update_output;
use std::{ use std::{
fmt::{Debug, Formatter}, fmt::{Debug, Formatter},
mem::transmute, mem::transmute,
sync::atomic::Ordering, sync::{Arc, atomic::Ordering},
}; };
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{KeyValuePair, SessionId, TaskId, TurboTasksBackendApi}; use turbo_tasks::{FxIndexMap, KeyValuePair, SessionId, TaskId, TurboTasksBackendApi};
use crate::{ use crate::{
backend::{ backend::{
BACKEND_JOB_PREFETCH_TASK, OperationGuard, TaskDataCategory, TransientTask, OperationGuard, TaskDataCategory, TransientTask, TurboTasksBackend, TurboTasksBackendInner,
TurboTasksBackend, TurboTasksBackendInner, TurboTasksBackendJob,
storage::{SpecificTaskDataCategory, StorageWriteGuard, iter_many}, storage::{SpecificTaskDataCategory, StorageWriteGuard, iter_many},
}, },
backing_storage::BackingStorage, backing_storage::BackingStorage,
...@@ -265,10 +264,11 @@ where ...@@ -265,10 +264,11 @@ where
fn schedule_task(&self, mut task: impl TaskGuard + '_) { fn schedule_task(&self, mut task: impl TaskGuard + '_) {
if let Some(tasks_to_prefetch) = task.prefetch() { if let Some(tasks_to_prefetch) = task.prefetch() {
self.turbo_tasks.schedule_backend_foreground_job( self.turbo_tasks
BACKEND_JOB_PREFETCH_TASK, .schedule_backend_foreground_job(TurboTasksBackendJob::Prefetch {
Some(Box::new(tasks_to_prefetch)), data: Arc::new(tasks_to_prefetch),
); range: None,
});
} }
self.turbo_tasks.schedule(task.id()); self.turbo_tasks.schedule(task.id());
} }
...@@ -336,7 +336,7 @@ pub trait TaskGuard: Debug { ...@@ -336,7 +336,7 @@ pub trait TaskGuard: Debug {
where where
F: for<'a> FnMut(CachedDataItemKey, CachedDataItemValueRef<'a>) -> bool + 'l; F: for<'a> FnMut(CachedDataItemKey, CachedDataItemValueRef<'a>) -> bool + 'l;
fn invalidate_serialization(&mut self); fn invalidate_serialization(&mut self);
fn prefetch(&mut self) -> Option<Vec<(TaskId, bool)>>; fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>>;
fn is_immutable(&self) -> bool; fn is_immutable(&self) -> bool;
} }
...@@ -526,15 +526,15 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> { ...@@ -526,15 +526,15 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> {
} }
} }
fn prefetch(&mut self) -> Option<Vec<(TaskId, bool)>> { fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>> {
if !self.task.state().prefetched() { if !self.task.state().prefetched() {
self.task.state_mut().set_prefetched(true); self.task.state_mut().set_prefetched(true);
let map = iter_many!(self, OutputDependency { target } => (target, false)) let map = iter_many!(self, OutputDependency { target } => (target, false))
.chain(iter_many!(self, CellDependency { target } => (target.task, true))) .chain(iter_many!(self, CellDependency { target } => (target.task, true)))
.chain(iter_many!(self, CollectiblesDependency { target } => (target.task, true))) .chain(iter_many!(self, CollectiblesDependency { target } => (target.task, true)))
.collect::<FxHashMap<_, _>>(); .collect::<FxIndexMap<_, _>>();
if map.len() > 16 { if map.len() > 16 {
return Some(map.into_iter().collect()); return Some(map);
} }
} }
None None
......
use std::{ use std::{
any::Any,
borrow::Cow, borrow::Cow,
error::Error, error::Error,
fmt::{self, Debug, Display}, fmt::{self, Debug, Display},
...@@ -17,7 +16,6 @@ use serde::{Deserialize, Serialize}; ...@@ -17,7 +16,6 @@ use serde::{Deserialize, Serialize};
use tracing::Span; use tracing::Span;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
pub use crate::id::BackendJobId;
use crate::{ use crate::{
RawVc, ReadCellOptions, ReadRef, SharedReference, TaskId, TaskIdSet, TraitRef, TraitTypeId, RawVc, ReadCellOptions, ReadRef, SharedReference, TaskId, TaskIdSet, TraitRef, TraitTypeId,
TurboTasksPanic, ValueTypeId, VcRead, VcValueTrait, VcValueType, TurboTasksPanic, ValueTypeId, VcRead, VcValueTrait, VcValueType,
...@@ -583,10 +581,11 @@ pub trait Backend: Sync + Send { ...@@ -583,10 +581,11 @@ pub trait Backend: Sync + Send {
turbo_tasks: &dyn TurboTasksBackendApi<Self>, turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> bool; ) -> bool;
type BackendJob: Send + 'static;
fn run_backend_job<'a>( fn run_backend_job<'a>(
&'a self, &'a self,
id: BackendJobId, job: Self::BackendJob,
data: Option<Box<dyn Any + Send>>,
turbo_tasks: &'a dyn TurboTasksBackendApi<Self>, turbo_tasks: &'a dyn TurboTasksBackendApi<Self>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>; ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
......
...@@ -115,7 +115,6 @@ macro_rules! define_id { ...@@ -115,7 +115,6 @@ macro_rules! define_id {
define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent)); define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent));
define_id!(ValueTypeId: u32); define_id!(ValueTypeId: u32);
define_id!(TraitTypeId: u32); define_id!(TraitTypeId: u32);
define_id!(BackendJobId: u32);
define_id!(SessionId: u32, derive(Debug, Serialize, Deserialize), serde(transparent)); define_id!(SessionId: u32, derive(Debug, Serialize, Deserialize), serde(transparent));
define_id!( define_id!(
LocalTaskId: u32, LocalTaskId: u32,
......
...@@ -121,8 +121,7 @@ where ...@@ -121,8 +121,7 @@ where
} }
} }
/// An [`IdFactory`], but extended with a free list to allow for id reuse for ids such as /// An [`IdFactory`], but extended with a free list to allow for id reuse.
/// [`BackendJobId`][crate::backend::BackendJobId].
/// ///
/// If silent untracked re-use of ids is okay, consider using the cheaper /// If silent untracked re-use of ids is okay, consider using the cheaper
/// [`IdFactory::wrapping_get`] method. /// [`IdFactory::wrapping_get`] method.
......
...@@ -30,7 +30,7 @@ use crate::{ ...@@ -30,7 +30,7 @@ use crate::{
}, },
capture_future::CaptureFuture, capture_future::CaptureFuture,
event::{Event, EventListener}, event::{Event, EventListener},
id::{BackendJobId, ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId}, id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId},
id_factory::IdFactoryWithReuse, id_factory::IdFactoryWithReuse,
macro_helpers::NativeFunction, macro_helpers::NativeFunction,
magic_any::MagicAny, magic_any::MagicAny,
...@@ -250,8 +250,8 @@ pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync + ...@@ -250,8 +250,8 @@ pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync +
unsafe fn reuse_transient_task_id(&self, id: Unused<TaskId>); unsafe fn reuse_transient_task_id(&self, id: Unused<TaskId>);
fn schedule(&self, task: TaskId); fn schedule(&self, task: TaskId);
fn schedule_backend_background_job(&self, id: BackendJobId, data: Option<Box<dyn Any + Send>>); fn schedule_backend_background_job(&self, job: B::BackendJob);
fn schedule_backend_foreground_job(&self, id: BackendJobId, data: Option<Box<dyn Any + Send>>); fn schedule_backend_foreground_job(&self, job: B::BackendJob);
fn try_foreground_done(&self) -> Result<(), EventListener>; fn try_foreground_done(&self) -> Result<(), EventListener>;
fn wait_foreground_done_excluding_own<'a>( fn wait_foreground_done_excluding_own<'a>(
...@@ -1478,16 +1478,16 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> { ...@@ -1478,16 +1478,16 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
} }
#[track_caller] #[track_caller]
fn schedule_backend_background_job(&self, id: BackendJobId, data: Option<Box<dyn Any + Send>>) { fn schedule_backend_background_job(&self, job: B::BackendJob) {
self.schedule_background_job(move |this| async move { self.schedule_background_job(move |this| async move {
this.backend.run_backend_job(id, data, &*this).await; this.backend.run_backend_job(job, &*this).await;
}) })
} }
#[track_caller] #[track_caller]
fn schedule_backend_foreground_job(&self, id: BackendJobId, data: Option<Box<dyn Any + Send>>) { fn schedule_backend_foreground_job(&self, job: B::BackendJob) {
self.schedule_foreground_job(move |this| async move { self.schedule_foreground_job(move |this| async move {
this.backend.run_backend_job(id, data, &*this).await; this.backend.run_backend_job(job, &*this).await;
}) })
} }
......
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