File size: 16,634 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | use std::{fmt::Display, future::Future, pin::Pin, task::Poll};
use anyhow::Result;
use auto_hash_map::AutoSet;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{
CollectiblesSource, ReadCellOptions, ReadConsistency, ResolvedVc, TaskId, TaskPersistence,
TraitTypeId, ValueType, ValueTypeId, VcValueTrait,
backend::{CellContent, TypedCellContent},
event::EventListener,
id::{ExecutionId, LocalTaskId},
manager::{read_local_output, read_task_cell, read_task_output, with_turbo_tasks},
registry::{self, get_value_type},
turbo_tasks,
};
#[derive(Error, Debug)]
pub enum ResolveTypeError {
#[error("no content in the cell")]
NoContent,
#[error("the content in the cell has no type")]
UntypedContent,
#[error("content is not available as task execution failed")]
TaskError { source: anyhow::Error },
#[error("reading the cell content failed")]
ReadError { source: anyhow::Error },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CellId {
pub type_id: ValueTypeId,
pub index: u32,
}
impl Display for CellId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}#{}",
registry::get_value_type(self.type_id).name,
self.index
)
}
}
/// A type-erased representation of [`Vc`][crate::Vc].
///
/// Type erasure reduces the [monomorphization] (and therefore binary size and compilation time)
/// required to support [`Vc`][crate::Vc].
///
/// This type is heavily used within the [`Backend`][crate::backend::Backend] trait, but should
/// otherwise be treated as an internal implementation detail of `turbo-tasks`.
///
/// [monomorphization]: https://doc.rust-lang.org/book/ch10-01-syntax.html#performance-of-code-using-generics
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RawVc {
/// The synchronous return value of a task (after argument resolution). This is the
/// representation used by [`OperationVc`][crate::OperationVc].
TaskOutput(TaskId),
/// A pointer to a specific [`Vc::cell`][crate::Vc::cell] or `.cell()` call within a task. This
/// is the representation used by [`ResolvedVc`].
///
/// [`CellId`] contains the [`ValueTypeId`], which can be useful for efficient downcasting.
TaskCell(TaskId, CellId),
/// The synchronous return value of a local task. This is created when a function is called
/// with unresolved arguments or more explicitly with
/// [`#[turbo_tasks::function(local)]`][crate::function].
///
/// Local outputs are only valid within the context of their parent "non-local" task. Turbo
/// Task's APIs are designed to prevent escapes of local [`Vc`]s, but [`ExecutionId`] is used
/// for a fallback runtime assertion.
LocalOutput(ExecutionId, LocalTaskId, TaskPersistence),
}
impl RawVc {
pub fn is_resolved(&self) -> bool {
match self {
RawVc::TaskOutput(..) => false,
RawVc::TaskCell(..) => true,
RawVc::LocalOutput(..) => false,
}
}
pub fn is_local(&self) -> bool {
match self {
RawVc::TaskOutput(..) => false,
RawVc::TaskCell(..) => false,
RawVc::LocalOutput(..) => true,
}
}
/// Returns `true` if the task this `RawVc` reads from cannot be serialized and will not be
/// stored in the persistent cache.
///
/// See [`TaskPersistence`] for more details.
pub fn is_transient(&self) -> bool {
match self {
RawVc::TaskOutput(task) | RawVc::TaskCell(task, ..) => task.is_transient(),
RawVc::LocalOutput(_, _, persistence) => *persistence == TaskPersistence::Transient,
}
}
pub(crate) fn into_read(self) -> ReadRawVcFuture {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new(self)
}
pub(crate) async fn resolve_trait(
self,
trait_type: TraitTypeId,
) -> Result<Option<RawVc>, ResolveTypeError> {
self.resolve_type_inner(|value_type_id| {
let value_type = get_value_type(value_type_id);
(value_type.has_trait(&trait_type), Some(value_type))
})
.await
}
pub(crate) async fn resolve_value(
self,
value_type: ValueTypeId,
) -> Result<Option<RawVc>, ResolveTypeError> {
self.resolve_type_inner(|cell_value_type| (cell_value_type == value_type, None))
.await
}
/// Helper for `resolve_trait` and `resolve_value`.
///
/// After finding a cell, returns `Ok(Some(...))` when `conditional` returns
/// `true`, and `Ok(None)` when `conditional` returns `false`.
///
/// As an optimization, `conditional` may return the `&'static ValueType` to
/// avoid a potential extra lookup later.
async fn resolve_type_inner(
self,
conditional: impl FnOnce(ValueTypeId) -> (bool, Option<&'static ValueType>),
) -> Result<Option<RawVc>, ResolveTypeError> {
let tt = turbo_tasks();
tt.notify_scheduled_tasks();
let mut current = self;
loop {
match current {
RawVc::TaskOutput(task) => {
current = read_task_output(&*tt, task, ReadConsistency::Eventual)
.await
.map_err(|source| ResolveTypeError::TaskError { source })?;
}
RawVc::TaskCell(task, index) => {
let content = read_task_cell(&*tt, task, index, ReadCellOptions::default())
.await
.map_err(|source| ResolveTypeError::ReadError { source })?;
if let TypedCellContent(value_type, CellContent(Some(_))) = content {
return Ok(if conditional(value_type).0 {
Some(RawVc::TaskCell(task, index))
} else {
None
});
} else {
return Err(ResolveTypeError::NoContent);
}
}
RawVc::LocalOutput(execution_id, local_task_id, ..) => {
current = read_local_output(&*tt, execution_id, local_task_id)
.await
.map_err(|source| ResolveTypeError::TaskError { source })?;
}
}
}
}
/// See [`crate::Vc::resolve`].
pub(crate) async fn resolve(self) -> Result<RawVc> {
self.resolve_inner(ReadConsistency::Eventual).await
}
/// See [`crate::Vc::resolve_strongly_consistent`].
pub(crate) async fn resolve_strongly_consistent(self) -> Result<RawVc> {
self.resolve_inner(ReadConsistency::Strong).await
}
async fn resolve_inner(self, mut consistency: ReadConsistency) -> Result<RawVc> {
let tt = turbo_tasks();
let mut current = self;
let mut notified = false;
loop {
match current {
RawVc::TaskOutput(task) => {
if !notified {
tt.notify_scheduled_tasks();
notified = true;
}
current = read_task_output(&*tt, task, consistency).await?;
// We no longer need to read strongly consistent, as any Vc returned
// from the first task will be inside of the scope of the first
// task. So it's already strongly consistent.
consistency = ReadConsistency::Eventual;
}
RawVc::TaskCell(_, _) => return Ok(current),
RawVc::LocalOutput(execution_id, local_task_id, ..) => {
debug_assert_eq!(consistency, ReadConsistency::Eventual);
current = read_local_output(&*tt, execution_id, local_task_id).await?;
}
}
}
}
/// Convert a potentially local `RawVc` into a non-local `RawVc`. This is a subset of resolution
/// resolution, because the returned `RawVc` can be a `TaskOutput`.
pub(crate) async fn to_non_local(self) -> Result<RawVc> {
let tt = turbo_tasks();
let mut current = self;
loop {
match current {
RawVc::LocalOutput(execution_id, local_task_id, ..) => {
current = read_local_output(&*tt, execution_id, local_task_id).await?;
}
non_local => return Ok(non_local),
}
}
}
pub(crate) fn connect(&self) {
let RawVc::TaskOutput(task_id) = self 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> {
match self {
RawVc::TaskOutput(t) | RawVc::TaskCell(t, ..) => Some(*t),
RawVc::LocalOutput(..) => None,
}
}
pub fn try_get_type_id(&self) -> Option<ValueTypeId> {
match self {
RawVc::TaskCell(_, CellId { type_id, .. }) => Some(*type_id),
RawVc::TaskOutput(..) | RawVc::LocalOutput(..) => None,
}
}
/// For a cell that's already resolved, synchronously check if it implements a trait using the
/// type information in `RawVc::TaskCell` (we don't actually need to read the cell!).
pub(crate) fn resolved_has_trait(&self, trait_id: TraitTypeId) -> bool {
match self {
RawVc::TaskCell(_task_id, cell_id) => {
get_value_type(cell_id.type_id).has_trait(&trait_id)
}
_ => 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 actually 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"),
}
}
}
/// 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 RawVc::TaskOutput(task_id) = self else {
panic!(
"<RawVc as CollectiblesSource>::peek_collectibles() must only be called on a \
RawVc::TaskOutput"
);
};
let tt = turbo_tasks();
tt.notify_scheduled_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 RawVc::TaskOutput(task_id) = self else {
panic!(
"<RawVc as CollectiblesSource>::take_collectibles() must only be called on a \
RawVc::TaskOutput"
);
};
let tt = turbo_tasks();
tt.notify_scheduled_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()
}
}
pub struct ReadRawVcFuture {
consistency: ReadConsistency,
current: RawVc,
untracked: bool,
read_cell_options: ReadCellOptions,
listener: Option<EventListener>,
}
impl ReadRawVcFuture {
pub(crate) fn new(vc: RawVc) -> Self {
ReadRawVcFuture {
consistency: ReadConsistency::Eventual,
current: vc,
untracked: false,
read_cell_options: ReadCellOptions::default(),
listener: None,
}
}
pub fn strongly_consistent(mut self) -> Self {
self.consistency = ReadConsistency::Strong;
self
}
/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
pub fn untracked(mut self) -> Self {
self.untracked = true;
self
}
pub fn final_read_hint(mut self) -> Self {
self.read_cell_options.final_read_hint = true;
self
}
}
impl Future for ReadRawVcFuture {
type Output = Result<TypedCellContent>;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
with_turbo_tasks(|tt| {
tt.notify_scheduled_tasks();
// SAFETY: we are not moving this
let this = unsafe { self.get_unchecked_mut() };
'outer: loop {
if let Some(listener) = &mut this.listener {
// SAFETY: listener is from previous pinned this
let listener = unsafe { Pin::new_unchecked(listener) };
if listener.poll(cx).is_pending() {
return Poll::Pending;
}
this.listener = None;
}
let mut listener = match this.current {
RawVc::TaskOutput(task) => {
let read_result = if this.untracked {
tt.try_read_task_output_untracked(task, this.consistency)
} else {
tt.try_read_task_output(task, this.consistency)
};
match read_result {
Ok(Ok(vc)) => {
// We no longer need to read strongly consistent, as any Vc returned
// from the first task will be inside of the scope of the first
// task. So it's already strongly consistent.
this.consistency = ReadConsistency::Eventual;
this.current = vc;
continue 'outer;
}
Ok(Err(listener)) => listener,
Err(err) => return Poll::Ready(Err(err)),
}
}
RawVc::TaskCell(task, index) => {
let read_result = if this.untracked {
tt.try_read_task_cell_untracked(task, index, this.read_cell_options)
} else {
tt.try_read_task_cell(task, index, this.read_cell_options)
};
match read_result {
Ok(Ok(content)) => {
// SAFETY: Constructor ensures that T and U are binary identical
return Poll::Ready(Ok(content));
}
Ok(Err(listener)) => listener,
Err(err) => return Poll::Ready(Err(err)),
}
}
RawVc::LocalOutput(execution_id, local_output_id, ..) => {
debug_assert_eq!(this.consistency, ReadConsistency::Eventual);
let read_result = tt.try_read_local_output(execution_id, local_output_id);
match read_result {
Ok(Ok(vc)) => {
this.current = vc;
continue 'outer;
}
Ok(Err(listener)) => listener,
Err(err) => return Poll::Ready(Err(err)),
}
}
};
// SAFETY: listener is from previous pinned this
match unsafe { Pin::new_unchecked(&mut listener) }.poll(cx) {
Poll::Ready(_) => continue,
Poll::Pending => {
this.listener = Some(listener);
return Poll::Pending;
}
};
}
})
}
}
impl Unpin for ReadRawVcFuture {}
|