Unverified Commit 2aa59eda authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

chore(turbo-tasks): Remove argument resolution from backends, only allow…

chore(turbo-tasks): Remove argument resolution from backends, only allow local-task-based argument resolution (#75214)

Removes the (now default) feature flag for local task argument resolution.

With local-task based argument resolution in place, we don't need to support argument resolution in the backend, which simplifies some code.
parent 7b8fc758
......@@ -866,7 +866,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
panic!(
"Calling transient function {} from persistent function function {} is not allowed",
task_type.get_name(),
parent_task_type.map_or_else(|| "unknown".into(), |t| t.get_name())
parent_task_type.map_or("unknown", |t| t.get_name())
);
}
if let Some(task_id) = self.task_cache.lookup_forward(&task_type) {
......@@ -963,10 +963,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
fn try_get_function_id(&self, task_id: TaskId) -> Option<FunctionId> {
self.lookup_task_type(task_id)
.and_then(|task_type| match &*task_type {
CachedTaskType::Native { fn_type, .. } => Some(*fn_type),
_ => None,
})
.map(|task_type| task_type.fn_type)
}
fn try_start_task_execution(
......@@ -1105,64 +1102,13 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
let (span, future) = match task_type {
TaskType::Cached(task_type) => match &*task_type {
CachedTaskType::Native { fn_type, this, arg } => (
TaskType::Cached(task_type) => {
let CachedTaskType { fn_type, this, arg } = &*task_type;
(
registry::get_function(*fn_type).span(task_id.persistence()),
registry::get_function(*fn_type).execute(*this, &**arg),
),
CachedTaskType::ResolveNative { fn_type, .. } => {
let span = registry::get_function(*fn_type).resolve_span(task_id.persistence());
let turbo_tasks = turbo_tasks.pin();
(
span,
Box::pin(async move {
let CachedTaskType::ResolveNative { fn_type, this, arg } = &*task_type
else {
unreachable!()
};
CachedTaskType::run_resolve_native(
*fn_type,
*this,
&**arg,
task_id.persistence(),
turbo_tasks,
)
.await
}) as Pin<Box<dyn Future<Output = _> + Send + '_>>,
)
}
CachedTaskType::ResolveTrait {
trait_type,
method_name,
..
} => {
let span = registry::get_trait(*trait_type).resolve_span(method_name);
let turbo_tasks = turbo_tasks.pin();
(
span,
Box::pin(async move {
let CachedTaskType::ResolveTrait {
trait_type,
method_name,
this,
arg,
} = &*task_type
else {
unreachable!()
};
CachedTaskType::run_resolve_trait(
*trait_type,
method_name.clone(),
*this,
&**arg,
task_id.persistence(),
turbo_tasks,
)
.await
}) as Pin<Box<dyn Future<Output = _> + Send + '_>>,
)
}
},
)
}
TaskType::Transient(task_type) => {
let task_type = task_type.clone();
let span = tracing::trace_span!("turbo_tasks::root_task");
......
......@@ -337,72 +337,14 @@ impl MemoryBackend {
&self.task_statistics
}
fn track_cache_hit(
&self,
task_type: &PreHashed<CachedTaskType>,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) {
self.task_statistics().map(|stats| match &**task_type {
CachedTaskType::ResolveNative {
fn_type: function_id,
this: _,
arg: _,
}
| CachedTaskType::Native {
fn_type: function_id,
this: _,
arg: _,
} => {
stats.increment_cache_hit(*function_id);
}
CachedTaskType::ResolveTrait {
trait_type,
method_name: name,
this,
arg: _,
} => {
// HACK: Resolve the this argument (`self`) in order to attribute the cache hit
// to the concrete trait implementation, rather than the dynamic trait method.
// This ensures cache hits and misses are both attributed to the same thing.
//
// Because this task already resolved, in most cases `self` should either be
// resolved, or already in the process of being resolved.
//
// However, `self` could become unloaded due to cache eviction, and this might
// trigger an otherwise unnecessary re-evalutation.
//
// This is a potentially okay trade-off as long as we don't log statistics by
// default. The alternative would be to store function ids on completed
// ResolveTrait tasks.
let trait_type = *trait_type;
let name = name.clone();
let this = *this;
let stats = Arc::clone(stats);
turbo_tasks.run_once(Box::pin(async move {
let function_id =
CachedTaskType::resolve_trait_method(trait_type, name, this).await?;
stats.increment_cache_hit(function_id);
Ok(())
}));
}
});
fn track_cache_hit(&self, task_type: &PreHashed<CachedTaskType>) {
self.task_statistics()
.map(|stats| stats.increment_cache_hit(task_type.fn_type));
}
fn track_cache_miss(&self, task_type: &PreHashed<CachedTaskType>) {
self.task_statistics().map(|stats| match &**task_type {
CachedTaskType::Native {
fn_type: function_id,
this: _,
arg: _,
} => {
stats.increment_cache_miss(*function_id);
}
CachedTaskType::ResolveTrait { .. } | CachedTaskType::ResolveNative { .. } => {
// these types re-execute themselves as `Native` after
// resolving their arguments, skip counting their
// executions here to avoid double-counting
}
});
self.task_statistics()
.map(|stats| stats.increment_cache_miss(task_type.fn_type));
}
}
......@@ -711,7 +653,7 @@ impl Backend for MemoryBackend {
self.lookup_and_connect_task(parent_task, &self.task_cache, &task_type, turbo_tasks)
{
// fast pass without creating a new task
self.track_cache_hit(&task_type, turbo_tasks);
self.track_cache_hit(&task_type);
task
} else {
self.track_cache_miss(&task_type);
......@@ -754,7 +696,7 @@ impl Backend for MemoryBackend {
turbo_tasks,
) {
// fast pass without creating a new task
self.track_cache_hit(&task_type, turbo_tasks);
self.track_cache_hit(&task_type);
task
} else {
self.track_cache_miss(&task_type);
......@@ -785,7 +727,7 @@ impl Backend for MemoryBackend {
fn try_get_function_id(&self, task_id: TaskId) -> Option<FunctionId> {
self.with_task(task_id, |task| match &task.ty {
TaskType::Persistent { ty } => ty.try_get_function_id(),
TaskType::Persistent { ty } => Some(ty.fn_type),
_ => None,
})
}
......
......@@ -602,7 +602,7 @@ impl Task {
aggregation_context.apply_queued_updates();
}
pub(crate) fn get_function_name(&self) -> Option<Cow<'static, str>> {
pub(crate) fn get_function_name(&self) -> Option<&'static str> {
if let TaskType::Persistent { ty, .. } | TaskType::Transient { ty, .. } = &self.ty {
Some(ty.get_name())
} else {
......@@ -743,7 +743,7 @@ impl Task {
)
}
};
self.make_execution_future(backend, turbo_tasks)
self.make_execution_future()
};
aggregation_context.apply_queued_updates();
Some(TaskExecutionSpec { future, span })
......@@ -752,8 +752,6 @@ impl Task {
/// Prepares task execution and returns a future that will execute the task.
fn make_execution_future<'a>(
self: &'a Task,
_backend: &MemoryBackend,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) -> (
Pin<Box<dyn Future<Output = Result<RawVc>> + Send + 'a>>,
Span,
......@@ -766,58 +764,19 @@ impl Task {
mutex.lock().take().expect("Task can only be executed once"),
tracing::trace_span!("turbo_tasks::once_task"),
),
TaskType::Persistent { ty, .. } | TaskType::Transient { ty, .. } => match &***ty {
CachedTaskType::Native {
fn_type: native_fn_id,
this,
arg,
} => {
let func = registry::get_function(*native_fn_id);
let span = func.span(self.id.persistence());
let entered = span.enter();
let future = func.execute(*this, &**arg);
drop(entered);
(future, span)
}
CachedTaskType::ResolveNative {
TaskType::Persistent { ty, .. } | TaskType::Transient { ty, .. } => {
let CachedTaskType {
fn_type: native_fn_id,
this,
arg,
} => {
let func = registry::get_function(*native_fn_id);
let span = func.resolve_span(self.id.persistence());
let entered = span.enter();
let future = Box::pin(CachedTaskType::run_resolve_native(
*native_fn_id,
*this,
&**arg,
self.id.persistence(),
turbo_tasks.pin(),
));
drop(entered);
(future, span)
}
CachedTaskType::ResolveTrait {
trait_type: trait_type_id,
method_name: name,
this,
arg,
} => {
let trait_type = registry::get_trait(*trait_type_id);
let span = trait_type.resolve_span(name);
let entered = span.enter();
let future = Box::pin(CachedTaskType::run_resolve_trait(
*trait_type_id,
name.clone(),
*this,
&**arg,
self.id.persistence(),
turbo_tasks.pin(),
));
drop(entered);
(future, span)
}
},
} = &***ty;
let func = registry::get_function(*native_fn_id);
let span = func.span(self.id.persistence());
let entered = span.enter();
let future = func.execute(*this, &**arg);
drop(entered);
(future, span)
}
}
}
......
......@@ -186,13 +186,13 @@ async fn test_no_execution() {
.await;
}
// Internally, this function uses `CachedTaskType::Native`.
// Internally, this function uses `PersistentTaskType`.
#[turbo_tasks::function]
fn double(val: u64) -> Vc<u64> {
Vc::cell(val * 2)
}
// Internally, this function uses `CachedTaskType::ResolveNative`.
// Internally, this function uses `LocalTaskType::ResolveNative`.
#[turbo_tasks::function]
async fn double_vc(val: Vc<u64>) -> Result<Vc<u64>> {
let val = *val.await?;
......
......@@ -9,10 +9,8 @@ edition = "2021"
bench = false
[features]
default = ["local_resolution"]
tokio_tracing = ["tokio/tracing"]
hanging_detection = []
local_resolution = []
# TODO(bgw): This feature is here to unblock turning on local tasks by default. It's only turned on
# in unit tests. This will be removed very soon.
......
This diff is collapsed.
......@@ -40,7 +40,10 @@ use crate::{
raw_vc::{CellId, RawVc},
registry::{self, get_function},
serialization_invalidation::SerializationInvalidator,
task::{local_task::LocalTask, shared_reference::TypedSharedReference},
task::{
local_task::{LocalTask, LocalTaskType},
shared_reference::TypedSharedReference,
},
trace::TraceRawVcs,
trait_helpers::get_trait_method,
util::StaticOrArc,
......@@ -441,7 +444,6 @@ impl CurrentGlobalTaskState {
}
}
#[cfg(feature = "local_resolution")]
fn create_local_task(&mut self, local_task: LocalTask) -> LocalTaskId {
self.local_tasks.push(local_task);
// generate a one-indexed id
......@@ -457,7 +459,6 @@ impl CurrentGlobalTaskState {
&self.local_tasks[(*local_task_id as usize) - 1]
}
#[cfg(feature = "local_resolution")]
fn get_mut_local_task(&mut self, local_task_id: LocalTaskId) -> &mut LocalTask {
&mut self.local_tasks[(*local_task_id as usize) - 1]
}
......@@ -609,7 +610,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
let task_type = CachedTaskType::Native {
let task_type = CachedTaskType {
fn_type: func,
this: None,
arg,
......@@ -642,7 +643,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
let task_type = CachedTaskType::Native {
let task_type = CachedTaskType {
fn_type: func,
this: Some(this),
arg,
......@@ -674,37 +675,15 @@ impl<B: Backend + 'static> TurboTasks<B> {
arg: Box<dyn MagicAny>,
persistence: TaskPersistence,
) -> RawVc {
// TODO(bgw): Don't create a full turbo task if this is a function using local_cells
if registry::get_function(func).arg_meta.is_resolved(&*arg) {
return self.native_call(func, arg, persistence);
}
let task_type = CachedTaskType::ResolveNative {
let task_type = LocalTaskType::ResolveNative {
fn_type: func,
this: None,
arg,
};
#[cfg(feature = "local_resolution")]
return self.schedule_local_task(task_type, persistence);
#[cfg(not(feature = "local_resolution"))]
match persistence {
TaskPersistence::LocalCells => {
todo!("bgw: local tasks");
}
TaskPersistence::Transient => {
RawVc::TaskOutput(self.backend.get_or_create_transient_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
TaskPersistence::Persistent => {
RawVc::TaskOutput(self.backend.get_or_create_persistent_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
}
self.schedule_local_task(task_type, persistence)
}
pub fn dynamic_this_call(
......@@ -717,33 +696,12 @@ impl<B: Backend + 'static> TurboTasks<B> {
if this.is_resolved() && registry::get_function(func).arg_meta.is_resolved(&*arg) {
return self.this_call(func, this, arg, persistence);
}
let task_type = CachedTaskType::ResolveNative {
let task_type = LocalTaskType::ResolveNative {
fn_type: func,
this: Some(this),
arg,
};
#[cfg(feature = "local_resolution")]
return self.schedule_local_task(task_type, persistence);
#[cfg(not(feature = "local_resolution"))]
return match persistence {
TaskPersistence::LocalCells => {
todo!("bgw: local tasks");
}
TaskPersistence::Transient => {
RawVc::TaskOutput(self.backend.get_or_create_transient_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
TaskPersistence::Persistent => {
RawVc::TaskOutput(self.backend.get_or_create_persistent_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
};
self.schedule_local_task(task_type, persistence)
}
pub fn trait_call(
......@@ -769,35 +727,14 @@ impl<B: Backend + 'static> TurboTasks<B> {
}
// create a wrapper task to resolve all inputs
let task_type = CachedTaskType::ResolveTrait {
let task_type = LocalTaskType::ResolveTrait {
trait_type,
method_name: trait_fn_name,
this,
arg,
};
#[cfg(feature = "local_resolution")]
return self.schedule_local_task(task_type, persistence);
#[cfg(not(feature = "local_resolution"))]
return match persistence {
TaskPersistence::LocalCells => {
todo!("bgw: local tasks");
}
TaskPersistence::Transient => {
RawVc::TaskOutput(self.backend.get_or_create_transient_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
TaskPersistence::Persistent => {
RawVc::TaskOutput(self.backend.get_or_create_persistent_task(
task_type,
current_task("turbo_function calls"),
self,
))
}
};
self.schedule_local_task(task_type, persistence)
}
#[track_caller]
......@@ -892,11 +829,11 @@ impl<B: Backend + 'static> TurboTasks<B> {
tokio::task::spawn(future);
}
#[cfg(feature = "local_resolution")]
fn schedule_local_task(
&self,
ty: CachedTaskType,
// if this is a `CachedTaskType::Resolve*`, we'll spawn another task with this persistence
ty: LocalTaskType,
// if this is a `LocalTaskType::Resolve*`, we may spawn another task with this persistence,
// if this is a `LocalTaskType::Native`, persistence is unused (there's no caching).
persistence: TaskPersistence,
) -> RawVc {
use crate::OutputContent;
......
use std::{borrow::Cow, fmt, fmt::Write, sync::Arc};
use anyhow::{anyhow, Result};
use crate::{
backend::{Backend, CachedTaskType, TaskExecutionSpec},
backend::{Backend, TaskExecutionSpec, TypedCellContent},
event::Event,
registry, OutputContent, TaskPersistence, TurboTasksBackendApi,
registry,
trait_helpers::{get_trait_method, has_trait, traits},
FunctionId, MagicAny, OutputContent, RawVc, TaskPersistence, TraitTypeId, TurboTasksBackendApi,
ValueTypeId,
};
/// A potentially in-flight local task stored in `CurrentGlobalTaskState::local_tasks`.
......@@ -12,12 +19,12 @@ pub enum LocalTask {
pub fn get_local_task_execution_spec<'a>(
turbo_tasks: &'_ dyn TurboTasksBackendApi<impl Backend + 'static>,
ty: &'a CachedTaskType,
// if this is a `CachedTaskType::Resolve*`, we'll spawn another task with this persistence
ty: &'a LocalTaskType,
// if this is a `LocalTaskType::Resolve*`, we'll spawn another task with this persistence
persistence: TaskPersistence,
) -> TaskExecutionSpec<'a> {
match ty {
CachedTaskType::Native {
LocalTaskType::Native {
fn_type: native_fn_id,
this,
arg,
......@@ -30,7 +37,7 @@ pub fn get_local_task_execution_spec<'a>(
drop(entered);
TaskExecutionSpec { future, span }
}
CachedTaskType::ResolveNative {
LocalTaskType::ResolveNative {
fn_type: native_fn_id,
this,
arg,
......@@ -38,7 +45,7 @@ pub fn get_local_task_execution_spec<'a>(
let func = registry::get_function(*native_fn_id);
let span = func.resolve_span(TaskPersistence::LocalCells);
let entered = span.enter();
let future = Box::pin(CachedTaskType::run_resolve_native(
let future = Box::pin(LocalTaskType::run_resolve_native(
*native_fn_id,
*this,
&**arg,
......@@ -48,7 +55,7 @@ pub fn get_local_task_execution_spec<'a>(
drop(entered);
TaskExecutionSpec { future, span }
}
CachedTaskType::ResolveTrait {
LocalTaskType::ResolveTrait {
trait_type: trait_type_id,
method_name: name,
this,
......@@ -57,7 +64,7 @@ pub fn get_local_task_execution_spec<'a>(
let trait_type = registry::get_trait(*trait_type_id);
let span = trait_type.resolve_span(name);
let entered = span.enter();
let future = Box::pin(CachedTaskType::run_resolve_trait(
let future = Box::pin(LocalTaskType::run_resolve_trait(
*trait_type_id,
name.clone(),
*this,
......@@ -70,3 +77,179 @@ pub fn get_local_task_execution_spec<'a>(
}
}
}
pub enum LocalTaskType {
/// A normal task execution a native (rust) function
Native {
fn_type: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
},
/// A resolve task, which resolves arguments and calls the function with resolve arguments. The
/// inner function call will be a `PersistentTaskType` or `LocalTaskType::Native`.
ResolveNative {
fn_type: FunctionId,
this: Option<RawVc>,
arg: Box<dyn MagicAny>,
},
/// A trait method resolve task. It resolves the first (`self`) argument and looks up the trait
/// method on that value. Then it calls that method. The method call will do a cache lookup and
/// might resolve arguments before.
ResolveTrait {
trait_type: TraitTypeId,
method_name: Cow<'static, str>,
this: RawVc,
arg: Box<dyn MagicAny>,
},
}
impl fmt::Display for LocalTaskType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.get_name())
}
}
impl LocalTaskType {
/// Returns the name of the function in the code. Trait methods are
/// formatted as `TraitName::method_name`.
///
/// Equivalent to [`ToString::to_string`], but potentially more efficient as
/// it can return a `&'static str` in many cases.
pub fn get_name(&self) -> Cow<'static, str> {
match self {
Self::Native {
fn_type: native_fn,
this: _,
arg: _,
} => Cow::Borrowed(&registry::get_function(*native_fn).name),
Self::ResolveNative {
fn_type: native_fn,
this: _,
arg: _,
} => format!("*{}", registry::get_function(*native_fn).name).into(),
Self::ResolveTrait {
trait_type: trait_id,
method_name: fn_name,
this: _,
arg: _,
} => format!("*{}::{}", registry::get_trait(*trait_id).name, fn_name).into(),
}
}
pub fn try_get_function_id(&self) -> Option<FunctionId> {
match self {
Self::Native { fn_type, .. } | Self::ResolveNative { fn_type, .. } => Some(*fn_type),
Self::ResolveTrait { .. } => None,
}
}
pub async fn run_resolve_native<B: Backend + 'static>(
fn_id: FunctionId,
mut this: Option<RawVc>,
arg: &dyn MagicAny,
persistence: TaskPersistence,
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
) -> Result<RawVc> {
if let Some(this) = this.as_mut() {
*this = this.resolve().await?;
}
let arg = registry::get_function(fn_id).arg_meta.resolve(arg).await?;
Ok(if let Some(this) = this {
turbo_tasks.this_call(fn_id, this, arg, persistence)
} else {
turbo_tasks.native_call(fn_id, arg, persistence)
})
}
pub async fn run_resolve_trait<B: Backend + 'static>(
trait_type: TraitTypeId,
name: Cow<'static, str>,
this: RawVc,
arg: &dyn MagicAny,
persistence: TaskPersistence,
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
) -> Result<RawVc> {
let this = this.resolve().await?;
let TypedCellContent(this_ty, _) = this.into_read().await?;
let native_fn = Self::resolve_trait_method_from_value(trait_type, this_ty, name)?;
let arg = registry::get_function(native_fn)
.arg_meta
.resolve(arg)
.await?;
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, persistence))
}
fn resolve_trait_method_from_value(
trait_type: TraitTypeId,
value_type: ValueTypeId,
name: Cow<'static, str>,
) -> Result<FunctionId> {
match get_trait_method(trait_type, value_type, name) {
Ok(native_fn) => Ok(native_fn),
Err(name) => {
if !has_trait(value_type, trait_type) {
let traits = traits(value_type).iter().fold(String::new(), |mut out, t| {
let _ = write!(out, " {}", t);
out
});
Err(anyhow!(
"{} doesn't implement {} (only{})",
registry::get_value_type(value_type),
registry::get_trait(trait_type),
traits,
))
} else {
Err(anyhow!(
"{} implements trait {}, but method {} is missing",
registry::get_value_type(value_type),
registry::get_trait(trait_type),
name
))
}
}
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::{self as turbo_tasks, TaskId, Vc};
#[turbo_tasks::function]
fn mock_func_task() -> Vc<()> {
Vc::cell(())
}
#[turbo_tasks::value_trait]
trait MockTrait {
fn mock_method_task() -> Vc<()>;
}
#[test]
fn test_get_name() {
crate::register();
assert_eq!(
LocalTaskType::Native {
fn_type: *MOCK_FUNC_TASK_FUNCTION_ID,
this: None,
arg: Box::new(()),
}
.get_name(),
"mock_func_task",
);
assert_eq!(
LocalTaskType::ResolveTrait {
trait_type: *MOCKTRAIT_TRAIT_TYPE_ID,
method_name: "mock_method_task".into(),
this: RawVc::TaskOutput(unsafe { TaskId::new_unchecked(1) }),
arg: Box::new(()),
}
.get_name(),
"*MockTrait::mock_method_task",
);
}
}
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