Unverified Commit 468a40fa authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Define `Effect` as a trait instead of a closure (#89080)

This switches effects (special `Collectible`s used to defer file writes) from using a closure representation to using a trait representation.

This makes constructing effects a little more annoying, but it ensures that we'll be able to correctly implement `TraceRawVcs` on these in a subsequent PR.

Changes to `turbo-tasks/src/effect.rs` were done by hand, an LLM was used for updating the two callsites.
parent 43f90177
This diff is collapsed.
use std::{
any::{Any, TypeId},
error::Error as StdError,
future::Future,
mem::replace,
panic,
......@@ -19,31 +20,66 @@ use crate::{
self as turbo_tasks, CollectiblesSource, ReadRef, ResolvedVc, TryJoinIterExt, emit,
event::{Event, EventListener},
spawn,
util::SharedError,
};
const APPLY_EFFECTS_CONCURRENCY_LIMIT: usize = 1024;
/// A trait to emit a task effect as collectible. This trait only has one
/// implementation, `EffectInstance` and no other implementation is allowed.
/// The trait is private to this module so that no other implementation can be
/// added.
#[turbo_tasks::value_trait]
trait Effect {}
pub trait Effect: Send + Sync + 'static {
type Error: StdError + Send + Sync + 'static;
/// A future that represents the effect of a task. The future is executed when
/// the effect is applied.
type EffectFuture = Pin<Box<dyn Future<Output = Result<()>> + Send + Sync + 'static>>;
/// A function that is called once at the top level of the program's execution after everything
/// has "settled".
///
/// This function is executed outside of the turbo-tasks context, and therefore cannot read any
/// `Vc`s or call any turbo-task functions. The effect can store [`ResolvedVc`]s (or any other
/// `Vc` type), but should not read or resolve their contents.
fn apply(self) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
/// The inner state of an effect instance if it has not been applied yet.
struct EffectInner {
future: EffectFuture,
/// The error type that an effect can return. We use `dyn std::error::Error` (instead of
/// [`anyhow::Error`] or [`SharedError`]) to encourage use of structured error types that can
/// potentially be transformed into `Issue`s.
///
/// We can't require that the returned error implements `Issue`:
/// - `Issue` uses `FileSystemPath`
/// - `turbo-tasks-fs` returns effect errors that should be transformed into `Issue`s.
/// - It logically doesn't make sense to define `Issue` in `turbo-tasks-fs`, `Issue` can't be
/// defined in a base crate either because it would form a circular crate dependency.
///
/// So instead, we leave it up to the caller to figure out how to downcast these errors themselves.
///
/// [`SharedError`]: crate::util::SharedError
type EffectError = Arc<dyn StdError + Send + Sync + 'static>;
// Private wrapper trait to allow dynamic dispatch of an `Effect`. This is similar to the pattern
// that the dynosaur crate uses: https://github.com/spastorino/dynosaur
trait DynEffect: Send + Sync + 'static {
fn dyn_apply(self: Box<Self>) -> Pin<Box<dyn Future<Output = Result<(), EffectError>> + Send>>;
}
impl<T> DynEffect for T
where
T: Effect,
{
fn dyn_apply(self: Box<Self>) -> Pin<Box<dyn Future<Output = Result<(), EffectError>> + Send>> {
Box::pin(async move {
self.apply()
.await
.map_err(|err| Arc::new(err) as EffectError)
})
}
}
/// A trait to emit a task effect as collectible. This trait only has one implementation,
/// `EffectInstance` and no other implementation is allowed. The trait is private to this module so
/// that no other implementation can be added.
#[turbo_tasks::value_trait]
trait EffectCollectible {}
enum EffectState {
NotStarted(EffectInner),
NotStarted(Box<dyn DynEffect>),
Started(Event),
Finished(Result<(), SharedError>),
Finished(Result<(), EffectError>),
}
/// The Effect instance collectible that is emitted for effects.
......@@ -54,11 +90,11 @@ struct EffectInstance {
}
impl EffectInstance {
fn new(future: impl Future<Output = Result<()>> + Send + Sync + 'static) -> Self {
fn new(effect: impl Effect) -> Self {
Self {
inner: Mutex::new(EffectState::NotStarted(EffectInner {
future: Box::pin(future),
})),
inner: Mutex::new(EffectState::NotStarted(
Box::new(effect) as Box<dyn DynEffect>
)),
}
}
......@@ -66,7 +102,7 @@ impl EffectInstance {
loop {
enum State {
Started(EventListener),
NotStarted(EffectInner),
NotStarted(Box<dyn DynEffect>),
}
let state = {
let mut guard = self.inner.lock();
......@@ -93,10 +129,11 @@ impl EffectInstance {
State::Started(listener) => {
listener.await;
}
State::NotStarted(EffectInner { future }) => {
let join_handle = spawn(ApplyEffectsContext::in_current_scope(future));
State::NotStarted(effect) => {
let join_handle =
spawn(ApplyEffectsContext::in_current_scope(effect.dyn_apply()));
let result = match join_handle.await {
Err(err) => Err(SharedError::new(err)),
Err(err) => Err(err),
Ok(()) => Ok(()),
};
let event = {
......@@ -117,19 +154,19 @@ impl EffectInstance {
}
#[turbo_tasks::value_impl]
impl Effect for EffectInstance {}
impl EffectCollectible for EffectInstance {}
/// Schedules an effect to be applied. The passed future is executed once `apply_effects` is called.
/// Emits an effect to be applied. The effect is executed once `apply_effects` is called.
///
/// The effect will only executed once. The passed future is executed outside of the current task
/// and can't read any Vcs. These need to be read before. ReadRefs can be passed into the future.
/// The effect will only executed once. The effect is executed outside of the current task
/// and can't read any Vcs. These need to be read before. ReadRefs can be passed into the effect.
///
/// Effects are executed in parallel, so they might need to use async locking to avoid problems.
/// Order of execution of multiple effects is not defined. You must not use multiple conflicting
/// effects to avoid non-deterministic behavior.
pub fn effect(future: impl Future<Output = Result<()>> + Send + Sync + 'static) {
emit::<Box<dyn Effect>>(ResolvedVc::upcast(
EffectInstance::new(future).resolved_cell(),
pub fn emit_effect(effect: impl Effect) {
emit::<Box<dyn EffectCollectible>>(ResolvedVc::upcast(
EffectInstance::new(effect).resolved_cell(),
));
}
......@@ -151,7 +188,7 @@ pub fn effect(future: impl Future<Output = Result<()>> + Send + Sync + 'static)
/// apply_effects(operation).await?;
/// ```
pub async fn apply_effects(source: impl CollectiblesSource) -> Result<()> {
let effects: AutoSet<ResolvedVc<Box<dyn Effect>>> = source.take_collectibles();
let effects: AutoSet<ResolvedVc<Box<dyn EffectCollectible>>> = source.take_collectibles();
if effects.is_empty() {
return Ok(());
}
......@@ -194,7 +231,7 @@ pub async fn apply_effects(source: impl CollectiblesSource) -> Result<()> {
/// result_with_effects.effects.apply().await?;
/// ```
pub async fn get_effects(source: impl CollectiblesSource) -> Result<Effects> {
let effects: AutoSet<ResolvedVc<Box<dyn Effect>>> = source.take_collectibles();
let effects: AutoSet<ResolvedVc<Box<dyn EffectCollectible>>> = source.take_collectibles();
let effects = effects
.into_iter()
.map(|effect| async move {
......
......@@ -78,7 +78,7 @@ pub use crate::{
collectibles::CollectiblesSource,
completion::{Completion, Completions},
display::{ValueToString, ValueToStringRef},
effect::{ApplyEffectsContext, Effects, apply_effects, effect, get_effects},
effect::{ApplyEffectsContext, Effect, Effects, apply_effects, emit_effect, get_effects},
error::PrettyPrintError,
id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId},
invalidation::{
......
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