Unverified Commit fb55cfb0 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Reduce the size of OperationVc from 8 bytes to 4 (#95614)

Reduce the size, i don't really expect much of a win, but this is low
hanging fruit and removes a few branches as well.

Consolidate the implementation of CollectibleSource, no need for
OperationVc to delegate to RawVc here.
parent 90dd06c2
...@@ -2,6 +2,7 @@ use std::{ ...@@ -2,6 +2,7 @@ use std::{
fmt::Debug, fmt::Debug,
future::Future, future::Future,
hash::Hash, hash::Hash,
marker::PhantomData,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
}; };
...@@ -13,8 +14,9 @@ use serde::{Deserialize, Serialize}; ...@@ -13,8 +14,9 @@ use serde::{Deserialize, Serialize};
pub use turbo_tasks_macros::OperationValue; pub use turbo_tasks_macros::OperationValue;
use crate::{ use crate::{
CollectiblesSource, RawVc, ReadVcFuture, ResolvedVc, TaskInput, UpcastStrict, Vc, VcValueTrait, CollectiblesSource, RawVc, ReadVcFuture, ResolvedVc, TaskId, TaskInput, UpcastStrict, Vc,
VcValueTraitCast, VcValueType, marker_trait::impl_auto_marker_trait, trace::TraceRawVcs, VcValueTrait, VcValueTraitCast, VcValueType, marker_trait::impl_auto_marker_trait,
trace::TraceRawVcs, turbo_tasks,
}; };
/// A future returned by [`OperationVc::resolve`] that connects an [`OperationVc<T>`] and resolves /// A future returned by [`OperationVc::resolve`] that connects an [`OperationVc<T>`] and resolves
...@@ -91,14 +93,14 @@ impl<T: ?Sized> Unpin for ResolveOperationVcFuture<T> {} ...@@ -91,14 +93,14 @@ impl<T: ?Sized> Unpin for ResolveOperationVcFuture<T> {}
/// [`ReadRef`]: crate::ReadRef /// [`ReadRef`]: crate::ReadRef
#[must_use] #[must_use]
#[derive(Serialize, Deserialize, Encode, Decode)] #[derive(Serialize, Deserialize, Encode, Decode)]
#[serde(transparent, bound = "")]
#[bincode(bounds = "T: ?Sized")] #[bincode(bounds = "T: ?Sized")]
#[repr(transparent)]
pub struct OperationVc<T> pub struct OperationVc<T>
where where
T: ?Sized, T: ?Sized,
{ {
pub(crate) node: Vc<T>, task: TaskId,
_t: PhantomData<T>,
} }
impl<T: ?Sized> OperationVc<T> { impl<T: ?Sized> OperationVc<T> {
...@@ -108,12 +110,15 @@ impl<T: ?Sized> OperationVc<T> { ...@@ -108,12 +110,15 @@ impl<T: ?Sized> OperationVc<T> {
#[doc(hidden)] #[doc(hidden)]
#[deprecated = "This is an internal function. Use #[turbo_tasks::function(operation)] instead."] #[deprecated = "This is an internal function. Use #[turbo_tasks::function(operation)] instead."]
pub fn cell_private(node: Vc<T>) -> Self { pub fn cell_private(node: Vc<T>) -> Self {
debug_assert!( let task = node.node.as_task_output().expect(
node.node.as_task_output().is_some(),
"OperationVc::cell_private must be called on the immediate return value of a task \ "OperationVc::cell_private must be called on the immediate return value of a task \
function" function",
); );
Self { node }
Self {
task,
_t: PhantomData,
}
} }
/// Marks this operation's underlying function call as a child of the current task, and returns /// Marks this operation's underlying function call as a child of the current task, and returns
...@@ -124,13 +129,15 @@ impl<T: ?Sized> OperationVc<T> { ...@@ -124,13 +129,15 @@ impl<T: ?Sized> OperationVc<T> {
/// operation is needed as `OperationVc` types can be stored outside of the call graph as part /// operation is needed as `OperationVc` types can be stored outside of the call graph as part
/// of [`State`][crate::State]s. /// of [`State`][crate::State]s.
pub fn connect(self) -> Vc<T> { pub fn connect(self) -> Vc<T> {
self.node.node.connect(); let tt = turbo_tasks();
self.node tt.connect_task(self.task);
Self::into_raw(self).into()
} }
/// Returns the `RawVc` corresponding to this `Vc`. /// Returns the `RawVc` corresponding to this `OperationVc`.
pub fn into_raw(vc: Self) -> RawVc { /// inverse of [`RawVc::as_task_output`]
vc.node.node fn into_raw(vc: Self) -> RawVc {
RawVc::task_output(vc.task)
} }
/// Upcasts the given `OperationVc<T>` to a `OperationVc<Box<dyn K>>`. /// Upcasts the given `OperationVc<T>` to a `OperationVc<Box<dyn K>>`.
...@@ -143,7 +150,8 @@ impl<T: ?Sized> OperationVc<T> { ...@@ -143,7 +150,8 @@ impl<T: ?Sized> OperationVc<T> {
K: VcValueTrait + ?Sized, K: VcValueTrait + ?Sized,
{ {
OperationVc { OperationVc {
node: Vc::upcast(vc.node), task: vc.task,
_t: PhantomData,
} }
} }
...@@ -202,7 +210,7 @@ where ...@@ -202,7 +210,7 @@ where
T: ?Sized, T: ?Sized,
{ {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.node.hash(state); self.task.hash(state);
} }
} }
...@@ -211,7 +219,7 @@ where ...@@ -211,7 +219,7 @@ where
T: ?Sized, T: ?Sized,
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.node == other.node self.task == other.task
} }
} }
...@@ -223,7 +231,7 @@ where ...@@ -223,7 +231,7 @@ where
{ {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OperationVc") f.debug_struct("OperationVc")
.field("node", &self.node.node) .field("task", &self.task)
.finish() .finish()
} }
} }
...@@ -235,7 +243,7 @@ where ...@@ -235,7 +243,7 @@ where
T: ?Sized + Send + Sync, T: ?Sized + Send + Sync,
{ {
fn is_transient(&self) -> bool { fn is_transient(&self) -> bool {
self.node.is_transient() self.task.is_transient()
} }
} }
...@@ -246,11 +254,12 @@ where ...@@ -246,11 +254,12 @@ where
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(raw: RawVc) -> Result<Self> { fn try_from(raw: RawVc) -> Result<Self> {
if raw.as_task_output().is_none() { let Some(task) = raw.as_task_output() else {
anyhow::bail!("Given RawVc {raw:?} is not a TaskOutput"); anyhow::bail!("Given RawVc {raw:?} is not a TaskOutput");
} };
Ok(Self { Ok(Self {
node: Vc::from(raw), task,
_t: PhantomData,
}) })
} }
} }
...@@ -260,7 +269,7 @@ where ...@@ -260,7 +269,7 @@ where
T: ?Sized, T: ?Sized,
{ {
fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) { fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) {
self.node.trace_raw_vcs(trace_context); Self::into_raw(*self).trace_raw_vcs(trace_context);
} }
} }
...@@ -269,15 +278,26 @@ where ...@@ -269,15 +278,26 @@ where
T: ?Sized, T: ?Sized,
{ {
fn drop_collectibles<Vt: VcValueTrait>(self) { fn drop_collectibles<Vt: VcValueTrait>(self) {
self.node.node.drop_collectibles::<Vt>(); let tt = turbo_tasks();
let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
tt.unemit_collectibles(Vt::get_trait_type_id(), &map);
} }
fn take_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> { fn take_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> {
self.node.node.take_collectibles() let tt = turbo_tasks();
let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
tt.unemit_collectibles(Vt::get_trait_type_id(), &map);
map.into_iter()
.filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
.collect()
} }
fn peek_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> { fn peek_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> {
self.node.node.peek_collectibles() let tt = turbo_tasks();
let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
map.into_iter()
.filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
.collect()
} }
} }
......
...@@ -8,13 +8,12 @@ use std::{ ...@@ -8,13 +8,12 @@ use std::{
}; };
use anyhow::Result; use anyhow::Result;
use auto_hash_map::AutoSet;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
CollectiblesSource, ReadCellOptions, ReadConsistency, ReadOutputOptions, ResolvedVc, TaskId, ReadCellOptions, ReadConsistency, ReadOutputOptions, TaskId, TaskPersistence, TraitTypeId,
TaskPersistence, TraitTypeId, ValueTypeId, VcValueTrait, ValueTypeId,
backend::TypedCellContent, backend::TypedCellContent,
event::EventListener, event::EventListener,
id::{ExecutionId, LocalTaskId, TASK_ID_MAX}, id::{ExecutionId, LocalTaskId, TASK_ID_MAX},
...@@ -446,14 +445,6 @@ impl RawVc { ...@@ -446,14 +445,6 @@ impl RawVc {
Ok(local_output) Ok(local_output)
} }
pub(crate) fn connect(&self) {
let Some(task_id) = self.as_task_output() else {
panic!("RawVc::connect() must only be called on a RawVc::TaskOutput");
};
let tt = turbo_tasks();
tt.connect_task(task_id);
}
pub fn try_get_task_id(&self) -> Option<TaskId> { pub fn try_get_task_id(&self) -> Option<TaskId> {
(!self.is_local_output()).then(|| self.read_task_id()) (!self.is_local_output()).then(|| self.read_task_id())
} }
...@@ -483,50 +474,6 @@ impl RawVc { ...@@ -483,50 +474,6 @@ impl RawVc {
} }
} }
/// This implementation of `CollectiblesSource` assumes that `self` is a `RawVc::TaskOutput`.
impl CollectiblesSource for RawVc {
fn peek_collectibles<T: VcValueTrait + ?Sized>(self) -> AutoSet<ResolvedVc<T>> {
let Some(task_id) = self.as_task_output() else {
panic!(
"<RawVc as CollectiblesSource>::peek_collectibles() must only be called on a \
RawVc::TaskOutput"
);
};
let tt = turbo_tasks();
let map = tt.read_task_collectibles(task_id, T::get_trait_type_id());
map.into_iter()
.filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
.collect()
}
fn take_collectibles<T: VcValueTrait + ?Sized>(self) -> AutoSet<ResolvedVc<T>> {
let Some(task_id) = self.as_task_output() else {
panic!(
"<RawVc as CollectiblesSource>::take_collectibles() must only be called on a \
RawVc::TaskOutput"
);
};
let tt = turbo_tasks();
let map = tt.read_task_collectibles(task_id, T::get_trait_type_id());
tt.unemit_collectibles(T::get_trait_type_id(), &map);
map.into_iter()
.filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
.collect()
}
fn drop_collectibles<T: VcValueTrait + ?Sized>(self) {
let Some(task_id) = self.as_task_output() else {
panic!(
"<RawVc as CollectiblesSource>::drop_collectibles() must only be called on a \
RawVc::TaskOutput"
);
};
let tt = turbo_tasks();
let map = tt.read_task_collectibles(task_id, T::get_trait_type_id());
tt.unemit_collectibles(T::get_trait_type_id(), &map);
}
}
/// Polls a pending [`EventListener`] slot. Returns [`Poll::Pending`] if the event has not yet /// Polls a pending [`EventListener`] slot. Returns [`Poll::Pending`] if the event has not yet
/// fired. On [`Poll::Ready`], clears the slot so it is not polled again. /// fired. On [`Poll::Ready`], clears the slot so it is not polled again.
fn poll_listener( fn poll_listener(
......
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