turbo_tasks/vc/lazy_read_ref.rs
1use std::{
2 fmt::{self, Debug},
3 sync::atomic::{AtomicBool, Ordering},
4};
5
6use rustc_hash::FxHashSet;
7
8use crate::{
9 CellId, ReadRef, TaskId,
10 manager::{ProvisionalReadDependency, with_turbo_tasks},
11 vc::{VcReadTarget, VcValueType},
12};
13
14/// The result of [`Vc::lazy_reader`](crate::Vc::lazy_reader): a cell read that can be canceled.
15///
16/// The value is only accessible via [`LazyReadRef::get`]. When the `LazyReadRef` is dropped
17/// without [`get`](LazyReadRef::get) ever being called, the read is canceled and the task no longer
18/// depends on the cell through it. Otherwise it behaves like a normal read (`.await`).
19///
20/// This allows reading a cell that is rarely needed ahead of time, e.g. to pass it into
21/// synchronous code that decides whether it needs the value, without depending on it when it
22/// isn't needed.
23///
24/// It **must** be dropped within the task execution that called
25/// [`lazy_reader`](crate::Vc::lazy_reader), and the same applies to the future returned by
26/// `lazy_reader`. Otherwise completing that execution panics, which aborts the process.
27#[must_use]
28pub struct LazyReadRef<T>
29where
30 T: VcValueType,
31{
32 read_ref: ReadRef<T>,
33 /// `None` if the read can't be canceled (e.g. a read outside of a task execution).
34 dependency: Option<(TaskId, CellId, ProvisionalReadDependency)>,
35 accessed: AtomicBool,
36}
37
38impl<T> LazyReadRef<T>
39where
40 T: VcValueType,
41{
42 /// `dependency` must only be `Some` within the task execution that created it, and must
43 /// already be registered as alive ([`ProvisionalReadDependency::register_alive_ref`]). The
44 /// registration is released when this is dropped within that execution.
45 pub(crate) fn new(
46 read_ref: ReadRef<T>,
47 dependency: Option<(TaskId, CellId, ProvisionalReadDependency)>,
48 ) -> Self {
49 Self {
50 read_ref,
51 dependency,
52 accessed: AtomicBool::new(false),
53 }
54 }
55
56 /// Accesses the value. Once this is called, the task depends on the value like with a normal
57 /// read, even when it's only called conditionally.
58 pub fn get(&self) -> &VcReadTarget<T> {
59 // Only the creating execution reads the flag (when dropping the ref), so relaxed is
60 // enough.
61 self.accessed.store(true, Ordering::Relaxed);
62 &self.read_ref
63 }
64}
65
66impl<T> Drop for LazyReadRef<T>
67where
68 T: VcValueType,
69{
70 fn drop(&mut self) {
71 let Some((task, index, dependency)) = self.dependency.take() else {
72 return;
73 };
74 if !dependency.is_current_execution() {
75 // Dropped outside of the creating execution: a bug in the task function. Don't panic
76 // in `drop`. The registration stays counted, so the creating execution panics when it
77 // completes.
78 return;
79 }
80 dependency.release_alive_ref();
81 if *self.accessed.get_mut() {
82 // Keep the dependency. It's converted to a normal dependency when the execution
83 // completes.
84 return;
85 }
86 // Refining the provisional dependency to no hashed dependencies at all removes it.
87 if let Err(error) = with_turbo_tasks(|tt| {
88 tt.refine_provisional_cell_dependency(
89 task,
90 index,
91 dependency.reader,
92 dependency.id,
93 &FxHashSet::default(),
94 )
95 }) {
96 // The cell's task was garbage collected while this execution used it.
97 dependency.fail_execution(error);
98 }
99 }
100}
101
102impl<T> Debug for LazyReadRef<T>
103where
104 T: VcValueType,
105{
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 // Must not access the value, as that would count as an access.
108 f.debug_struct("LazyReadRef")
109 .field("accessed", &self.accessed.load(Ordering::Relaxed))
110 .finish_non_exhaustive()
111 }
112}