Unverified Commit 001f5de4 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

perf(turbo-tasks): Use the type information we already have for all ResolvedVc…

perf(turbo-tasks): Use the type information we already have for all ResolvedVc casts, expose synchronous versions of functions (#75055)

We already have type information for `ResolvedVc`, so we can avoid going through the codepath in `Vc`, which can fail and requires a cell read.

I did this for sidecasts in https://github.com/vercel/next.js/pull/74844 to unblock synchronous graph traversals. This does the same for the rest of the functions.

I expect any perf impact to be pretty small (casting isn't a particularly hot codepath), but it's also an easy change to make, and (once callsites are migrated) will lead to a nicer API.

Right now I'm continuing to expose the async fallible versions of these functions. I'll port the callsites (looks like there's 92 of them) to the synchronous infallible version in a later PR.
parent 5de5b9a0
...@@ -292,7 +292,7 @@ impl RawVc { ...@@ -292,7 +292,7 @@ impl RawVc {
} }
} }
/// For a type that's already resolved, synchronously check if it implements a trait using the /// For a cell that's already resolved, synchronously check if it implements a trait using the
/// type information in `RawVc::TaskCell` (we don't actualy need to read the cell!). /// type information in `RawVc::TaskCell` (we don't actualy need to read the cell!).
pub(crate) fn resolved_has_trait(&self, trait_id: TraitTypeId) -> bool { pub(crate) fn resolved_has_trait(&self, trait_id: TraitTypeId) -> bool {
match self { match self {
...@@ -302,6 +302,15 @@ impl RawVc { ...@@ -302,6 +302,15 @@ impl RawVc {
_ => unreachable!("resolved_has_trait must be called with a RawVc::TaskCell"), _ => unreachable!("resolved_has_trait must be called with a RawVc::TaskCell"),
} }
} }
/// For a cell that's already resolved, synchronously check if it is a given type using the type
/// information in `RawVc::TaskCell` (we don't actualy need to read the cell!).
pub(crate) fn resolved_is_type(&self, type_id: ValueTypeId) -> bool {
match self {
RawVc::TaskCell(_task_id, cell_id) => cell_id.type_id == type_id,
_ => unreachable!("resolved_is_type must be called with a RawVc::TaskCell"),
}
}
} }
impl CollectiblesSource for RawVc { impl CollectiblesSource for RawVc {
......
...@@ -255,9 +255,22 @@ where ...@@ -255,9 +255,22 @@ where
where where
K: Upcast<T> + VcValueTrait + ?Sized, K: Upcast<T> + VcValueTrait + ?Sized,
{ {
Ok(Vc::try_resolve_downcast(this.node) // TODO: Expose a synchronous API instead of this async one that returns `Result<Option<_>>`
.await? Ok(Self::try_downcast_sync(this))
.map(|node| ResolvedVc { node })) }
/// Attempts to downcast the given `ResolvedVc<Box<dyn T>>` to a `ResolvedVc<K>`, where `K`
/// is of the form `Box<dyn L>`, and `L` is a value trait.
///
/// Returns `None` if the underlying value type is not a `K`.
///
/// See also: [`Vc::try_resolve_downcast`].
pub fn try_downcast_sync<K>(this: Self) -> Option<ResolvedVc<K>>
where
K: Upcast<T> + VcValueTrait + ?Sized,
{
// this is just a more type-safe version of a sidecast
Self::try_sidecast_sync(this)
} }
/// Attempts to downcast the given `Vc<Box<dyn T>>` to a `Vc<K>`, where `K` is a value type. /// Attempts to downcast the given `Vc<Box<dyn T>>` to a `Vc<K>`, where `K` is a value type.
...@@ -269,9 +282,28 @@ where ...@@ -269,9 +282,28 @@ where
where where
K: Upcast<T> + VcValueType, K: Upcast<T> + VcValueType,
{ {
Ok(Vc::try_resolve_downcast_type(this.node) // TODO: Expose a synchronous API instead of this async one that returns `Result<Option<_>>`
.await? Ok(Self::try_downcast_type_sync(this))
.map(|node| ResolvedVc { node })) }
/// Attempts to downcast the given `Vc<Box<dyn T>>` to a `Vc<K>`, where `K` is a value type.
///
/// Returns `None` if the underlying value type is not a `K`.
///
/// See also: [`Vc::try_resolve_downcast_type`].
pub fn try_downcast_type_sync<K>(this: Self) -> Option<ResolvedVc<K>>
where
K: Upcast<T> + VcValueType,
{
let raw_vc = this.node.node;
raw_vc
.resolved_is_type(<K as VcValueType>::get_value_type_id())
.then_some(ResolvedVc {
node: Vc {
node: raw_vc,
_t: PhantomData,
},
})
} }
} }
......
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