File size: 9,030 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 |
use std::{
borrow::Cow,
cell::RefCell,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
marker::PhantomData,
path::{Path, PathBuf},
sync::{Arc, Mutex, atomic::*},
time::Duration,
};
use auto_hash_map::{AutoMap, AutoSet};
use either::Either;
use indexmap::{IndexMap, IndexSet};
use smallvec::SmallVec;
use turbo_rcstr::RcStr;
use crate::RawVc;
pub struct TraceRawVcsContext {
list: Vec<RawVc>,
}
impl TraceRawVcsContext {
pub(crate) fn new() -> Self {
Self { list: Vec::new() }
}
pub(crate) fn into_vec(self) -> Vec<RawVc> {
self.list
}
}
/// Trait that allows to walk data to find all [RawVc]s contained.
///
/// This is important for Garbage Collection to mark all Cells and therefore
/// Tasks that are still in use.
///
/// It can also be used to optimize transferring of Tasks, where knowning the
/// referenced Cells/Tasks allows pushing them earlier.
///
/// `#[derive(TraceRawVcs)]` is available.
/// `#[trace_ignore]` can be used on fields to skip tracing for them.
pub trait TraceRawVcs {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext);
fn get_raw_vcs(&self) -> Vec<RawVc> {
let mut trace_context = TraceRawVcsContext::new();
self.trace_raw_vcs(&mut trace_context);
trace_context.into_vec()
}
}
macro_rules! ignore {
($ty:ty) => {
impl TraceRawVcs for $ty {
fn trace_raw_vcs(&self, _context: &mut TraceRawVcsContext) {}
}
};
($ty:ty, $($tys:ty),+) => {
ignore!($ty);
ignore!($($tys),+);
}
}
ignore!(
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, f32, f64, char, bool, isize, usize
);
ignore!(
AtomicI8,
AtomicU8,
AtomicI16,
AtomicU16,
AtomicI32,
AtomicU32,
AtomicI64,
AtomicU64,
AtomicBool,
AtomicUsize
);
ignore!((), str, String, Duration, anyhow::Error, RcStr);
ignore!(Path, PathBuf);
ignore!(serde_json::Value, serde_json::Map<String, serde_json::Value>);
impl<T: ?Sized> TraceRawVcs for PhantomData<T> {
fn trace_raw_vcs(&self, _trace_context: &mut TraceRawVcsContext) {}
}
// based on stdlib's internal `tuple_impls!` macro
macro_rules! impl_trace_tuple {
($T:ident) => {
impl_trace_tuple!(@impl $T);
};
($T:ident $( $U:ident )+) => {
impl_trace_tuple!($( $U )+);
impl_trace_tuple!(@impl $T $( $U )+);
};
(@impl $( $T:ident )+) => {
impl<$($T: TraceRawVcs),+> TraceRawVcs for ($($T,)+) {
#[allow(non_snake_case)]
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
let ($($T,)+) = self;
$(
TraceRawVcs::trace_raw_vcs($T, trace_context);
)+
}
}
};
}
impl_trace_tuple!(E D C B A Z Y X W V U T);
/// Function pointers (the lowercase `fn` type, not `Fn`) don't contain any data, so it's not
/// possible for them to contain a `Vc`.
macro_rules! impl_trace_fn_ptr {
($T:ident) => {
impl_trace_fn_ptr!(@impl $T);
};
($T:ident $( $U:ident )+) => {
impl_trace_fn_ptr!($( $U )+);
impl_trace_fn_ptr!(@impl $T $( $U )+);
};
(@impl $( $T:ident )+) => {
impl<$($T,)+ Return> TraceRawVcs for fn($($T),+) -> Return {
fn trace_raw_vcs(&self, _trace_context: &mut TraceRawVcsContext) {}
}
};
}
impl_trace_fn_ptr!(E D C B A Z Y X W V U T);
impl<T: TraceRawVcs> TraceRawVcs for Option<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
if let Some(item) = self {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs> TraceRawVcs for Vec<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs> TraceRawVcs for Box<[T]> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs, const N: usize> TraceRawVcs for SmallVec<[T; N]> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs, const N: usize> TraceRawVcs for [T; N] {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs, S> TraceRawVcs for HashSet<T, S> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs, S, const I: usize> TraceRawVcs for AutoSet<T, S, I> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs> TraceRawVcs for BTreeSet<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<T: TraceRawVcs, S> TraceRawVcs for IndexSet<T, S> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for item in self.iter() {
TraceRawVcs::trace_raw_vcs(item, trace_context);
}
}
}
impl<K: TraceRawVcs, V: TraceRawVcs, S> TraceRawVcs for HashMap<K, V, S> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for (key, value) in self.iter() {
TraceRawVcs::trace_raw_vcs(key, trace_context);
TraceRawVcs::trace_raw_vcs(value, trace_context);
}
}
}
impl<K: TraceRawVcs, V: TraceRawVcs, S, const I: usize> TraceRawVcs for AutoMap<K, V, S, I> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for (key, value) in self.iter() {
TraceRawVcs::trace_raw_vcs(key, trace_context);
TraceRawVcs::trace_raw_vcs(value, trace_context);
}
}
}
impl<K: TraceRawVcs, V: TraceRawVcs> TraceRawVcs for BTreeMap<K, V> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for (key, value) in self.iter() {
TraceRawVcs::trace_raw_vcs(key, trace_context);
TraceRawVcs::trace_raw_vcs(value, trace_context);
}
}
}
impl<K: TraceRawVcs, V: TraceRawVcs, S> TraceRawVcs for IndexMap<K, V, S> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
for (key, value) in self.iter() {
TraceRawVcs::trace_raw_vcs(key, trace_context);
TraceRawVcs::trace_raw_vcs(value, trace_context);
}
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for Box<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
TraceRawVcs::trace_raw_vcs(&**self, trace_context);
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for Arc<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
TraceRawVcs::trace_raw_vcs(&**self, trace_context);
}
}
impl<B: TraceRawVcs + ToOwned + ?Sized> TraceRawVcs for Cow<'_, B> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
TraceRawVcs::trace_raw_vcs(&**self, trace_context);
}
}
impl TraceRawVcs for RawVc {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
trace_context.list.push(*self);
}
}
impl<T: TraceRawVcs, E: TraceRawVcs> TraceRawVcs for Result<T, E> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
match self {
Ok(o) => o.trace_raw_vcs(trace_context),
Err(e) => e.trace_raw_vcs(trace_context),
}
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for Mutex<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
self.lock().unwrap().trace_raw_vcs(trace_context);
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for RefCell<T> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
self.borrow().trace_raw_vcs(trace_context);
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for &T {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
(**self).trace_raw_vcs(trace_context);
}
}
impl<T: TraceRawVcs + ?Sized> TraceRawVcs for &mut T {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
(**self).trace_raw_vcs(trace_context);
}
}
impl<L: TraceRawVcs, R: TraceRawVcs> TraceRawVcs for Either<L, R> {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
match self {
Either::Left(l) => l.trace_raw_vcs(trace_context),
Either::Right(r) => r.trace_raw_vcs(trace_context),
}
}
}
pub use turbo_tasks_macros::TraceRawVcs;
|