Commit 03307b11 authored by Tobias Koppers's avatar Tobias Koppers

Revert "Revert "Turbopack: avoid using rayon in favor of tokio tasks (#82256)…"

This reverts commit 50524de0.
parent 0af39705
...@@ -9252,7 +9252,6 @@ dependencies = [ ...@@ -9252,7 +9252,6 @@ dependencies = [
"parking_lot", "parking_lot",
"pot", "pot",
"rand 0.9.0", "rand 0.9.0",
"rayon",
"regex", "regex",
"ringmap", "ringmap",
"rstest", "rstest",
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use turbo_persistence::{MetaFileEntryInfo, TurboPersistence}; use turbo_persistence::{MetaFileEntryInfo, SerialScheduler, TurboPersistence};
fn main() -> Result<()> { fn main() -> Result<()> {
// Get CLI argument // Get CLI argument
...@@ -16,7 +16,7 @@ fn main() -> Result<()> { ...@@ -16,7 +16,7 @@ fn main() -> Result<()> {
bail!("The provided path does not exist: {}", path.display()); bail!("The provided path does not exist: {}", path.display());
} }
let db = TurboPersistence::open_read_only(path)?; let db: TurboPersistence<SerialScheduler> = TurboPersistence::open_read_only(path)?;
let meta_info = db let meta_info = db
.meta_info() .meta_info()
.context("Failed to retrieve meta information")?; .context("Failed to retrieve meta information")?;
......
...@@ -22,7 +22,6 @@ memmap2 = "0.9.5" ...@@ -22,7 +22,6 @@ memmap2 = "0.9.5"
parking_lot = { workspace = true } parking_lot = { workspace = true }
qfilter = { version = "0.2.4", features = ["serde"] } qfilter = { version = "0.2.4", features = ["serde"] }
quick_cache = { workspace = true } quick_cache = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
smallvec = { workspace = true } smallvec = { workspace = true }
thread_local = { workspace = true } thread_local = { workspace = true }
...@@ -32,6 +31,7 @@ zstd = { version = "0.13.2", features = ["zdict_builder"] } ...@@ -32,6 +31,7 @@ zstd = { version = "0.13.2", features = ["zdict_builder"] }
[dev-dependencies] [dev-dependencies]
rand = { workspace = true, features = ["small_rng"] } rand = { workspace = true, features = ["small_rng"] }
rayon = { workspace = true }
tempfile = { workspace = true } tempfile = { workspace = true }
[lints] [lints]
......
use std::mem::take;
use crate::{ use crate::{
ValueBuffer, ValueBuffer,
collector_entry::{CollectorEntry, CollectorEntryValue, EntryKey}, collector_entry::{CollectorEntry, CollectorEntryValue, EntryKey},
...@@ -111,4 +113,11 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> { ...@@ -111,4 +113,11 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
self.total_value_size = 0; self.total_value_size = 0;
self.entries.drain(..) self.entries.drain(..)
} }
/// Clears the collector and drops the capacity
pub fn drop_contents(&mut self) {
drop(take(&mut self.entries));
self.total_key_size = 0;
self.total_value_size = 0;
}
} }
...@@ -13,19 +13,21 @@ mod db; ...@@ -13,19 +13,21 @@ mod db;
mod key; mod key;
mod lookup_entry; mod lookup_entry;
mod merge_iter; mod merge_iter;
mod meta_file;
mod meta_file_builder;
mod parallel_scheduler;
mod sst_filter;
mod static_sorted_file; mod static_sorted_file;
mod static_sorted_file_builder; mod static_sorted_file_builder;
mod value_buf;
mod write_batch; mod write_batch;
mod meta_file;
mod meta_file_builder;
mod sst_filter;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod value_buf;
pub use arc_slice::ArcSlice; pub use arc_slice::ArcSlice;
pub use db::{CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence}; pub use db::{CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence};
pub use key::{KeyBase, QueryKey, StoreKey}; pub use key::{KeyBase, QueryKey, StoreKey};
pub use parallel_scheduler::{ParallelScheduler, SerialScheduler};
pub use value_buf::ValueBuffer; pub use value_buf::ValueBuffer;
pub use write_batch::WriteBatch; pub use write_batch::WriteBatch;
pub trait ParallelScheduler: Clone + Sync + Send {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync;
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static;
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static;
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static;
fn parallel_map_collect<'l, Item, PerItemResult, Result>(
&self,
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>;
fn parallel_map_collect_owned<Item, PerItemResult, Result>(
&self,
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync,
Result: FromIterator<PerItemResult>;
}
#[derive(Clone, Copy, Default)]
pub struct SerialScheduler;
impl ParallelScheduler for SerialScheduler {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync,
{
for item in items {
f(item);
}
}
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn parallel_map_collect<'l, Item, PerItemResult, Result>(
&self,
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
items.iter().map(f).collect()
}
fn parallel_map_collect_owned<Item, PerItemResult, Result>(
&self,
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync,
Result: FromIterator<PerItemResult>,
{
items.into_iter().map(f).collect()
}
}
...@@ -40,7 +40,6 @@ once_cell = { workspace = true } ...@@ -40,7 +40,6 @@ once_cell = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
pot = "3.0.0" pot = "3.0.0"
rand = { workspace = true } rand = { workspace = true }
rayon = { workspace = true }
ringmap = { workspace = true, features = ["serde"] } ringmap = { workspace = true, features = ["serde"] }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
......
...@@ -1267,7 +1267,6 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1267,7 +1267,6 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return task_id; return task_id;
} }
self.track_cache_miss(&task_type);
let tx = self let tx = self
.should_restore() .should_restore()
.then(|| self.backing_storage.start_read_transaction()) .then(|| self.backing_storage.start_read_transaction())
...@@ -1279,6 +1278,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1279,6 +1278,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
.forward_lookup_task_cache(tx.as_ref(), &task_type) .forward_lookup_task_cache(tx.as_ref(), &task_type)
.expect("Failed to lookup task id") .expect("Failed to lookup task id")
} { } {
self.track_cache_hit(&task_type);
let _ = self.task_cache.try_insert(Arc::new(task_type), task_id); let _ = self.task_cache.try_insert(Arc::new(task_type), task_id);
task_id task_id
} else { } else {
...@@ -1287,12 +1287,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1287,12 +1287,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let task_id = if let Err(existing_task_id) = let task_id = if let Err(existing_task_id) =
self.task_cache.try_insert(task_type.clone(), task_id) self.task_cache.try_insert(task_type.clone(), task_id)
{ {
self.track_cache_hit(&task_type);
// Safety: We just created the id and failed to insert it. // Safety: We just created the id and failed to insert it.
unsafe { unsafe {
self.persisted_task_id_factory.reuse(task_id); self.persisted_task_id_factory.reuse(task_id);
} }
existing_task_id existing_task_id
} else { } else {
self.track_cache_miss(&task_type);
task_id task_id
}; };
if let Some(log) = &self.persisted_task_cache_log { if let Some(log) = &self.persisted_task_cache_log {
...@@ -1327,10 +1329,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1327,10 +1329,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return task_id; return task_id;
} }
self.track_cache_miss(&task_type);
let task_type = Arc::new(task_type); let task_type = Arc::new(task_type);
let task_id = self.transient_task_id_factory.get(); let task_id = self.transient_task_id_factory.get();
if let Err(existing_task_id) = self.task_cache.try_insert(task_type, task_id) { if let Err(existing_task_id) = self.task_cache.try_insert(task_type.clone(), task_id) {
self.track_cache_hit(&task_type);
// Safety: We just created the id and failed to insert it. // Safety: We just created the id and failed to insert it.
unsafe { unsafe {
self.transient_task_id_factory.reuse(task_id); self.transient_task_id_factory.reuse(task_id);
...@@ -1339,6 +1341,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1339,6 +1341,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return existing_task_id; return existing_task_id;
} }
self.track_cache_miss(&task_type);
self.connect_child(parent_task, task_id, turbo_tasks); self.connect_child(parent_task, task_id, turbo_tasks);
task_id task_id
......
...@@ -6,9 +6,8 @@ use std::{ ...@@ -6,9 +6,8 @@ use std::{
}; };
use bitfield::bitfield; use bitfield::bitfield;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_tasks::{FxDashMap, TaskId}; use turbo_tasks::{FxDashMap, TaskId, parallel};
use crate::{ use crate::{
backend::dynamic_storage::DynamicStorage, backend::dynamic_storage::DynamicStorage,
...@@ -664,48 +663,43 @@ impl Storage { ...@@ -664,48 +663,43 @@ impl Storage {
// The number of shards is much larger than the number of threads, so the effect of the // The number of shards is much larger than the number of threads, so the effect of the
// locks held is negligible. // locks held is negligible.
self.modified parallel::map_collect::<_, _, Vec<_>>(self.modified.shards(), |shard| {
.shards() let mut direct_snapshots: Vec<(TaskId, Box<InnerStorageSnapshot>)> = Vec::new();
.par_iter() let mut modified: SmallVec<[TaskId; 4]> = SmallVec::new();
.with_max_len(1) {
.map(|shard| { // Take the snapshots from the modified map
let mut direct_snapshots: Vec<(TaskId, Box<InnerStorageSnapshot>)> = Vec::new(); let guard = shard.write();
let mut modified: SmallVec<[TaskId; 4]> = SmallVec::new(); // Safety: guard must outlive the iterator.
{ for bucket in unsafe { guard.iter() } {
// Take the snapshots from the modified map // Safety: the guard guarantees that the bucket is not removed and the ptr
let guard = shard.write(); // is valid.
// Safety: guard must outlive the iterator. let (key, shared_value) = unsafe { bucket.as_mut() };
for bucket in unsafe { guard.iter() } { let modified_state = shared_value.get_mut();
// Safety: the guard guarantees that the bucket is not removed and the ptr match modified_state {
// is valid. ModifiedState::Modified => {
let (key, shared_value) = unsafe { bucket.as_mut() }; modified.push(*key);
let modified_state = shared_value.get_mut(); }
match modified_state { ModifiedState::Snapshot(snapshot) => {
ModifiedState::Modified => { if let Some(snapshot) = snapshot.take() {
modified.push(*key); direct_snapshots.push((*key, snapshot));
}
ModifiedState::Snapshot(snapshot) => {
if let Some(snapshot) = snapshot.take() {
direct_snapshots.push((*key, snapshot));
}
} }
} }
} }
// Safety: guard must outlive the iterator.
drop(guard);
} }
// Safety: guard must outlive the iterator.
drop(guard);
}
SnapshotShard { SnapshotShard {
direct_snapshots, direct_snapshots,
modified, modified,
storage: self, storage: self,
guard: Some(guard.clone()), guard: Some(guard.clone()),
process, process,
preprocess, preprocess,
process_snapshot, process_snapshot,
} }
}) })
.collect::<Vec<_>>()
} }
/// Start snapshot mode. /// Start snapshot mode.
......
use std::{ use std::{cmp::max, path::PathBuf, sync::Arc, thread::available_parallelism};
cmp::max,
path::PathBuf,
sync::Arc,
thread::{JoinHandle, available_parallelism, spawn},
};
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use parking_lot::Mutex; use parking_lot::Mutex;
use tokio::{runtime::Handle, task::block_in_place};
use turbo_persistence::{ use turbo_persistence::{
ArcSlice, CompactConfig, KeyBase, StoreKey, TurboPersistence, ValueBuffer, ArcSlice, CompactConfig, KeyBase, StoreKey, TurboPersistence, ValueBuffer,
}; };
use turbo_tasks::{JoinHandle, spawn};
use crate::database::{ use crate::database::{
key_value_database::{KeySpace, KeyValueDatabase}, key_value_database::{KeySpace, KeyValueDatabase},
turbo::parallel_scheduler::TurboTasksParallelScheduler,
write_batch::{BaseWriteBatch, ConcurrentWriteBatch, WriteBatch, WriteBuffer}, write_batch::{BaseWriteBatch, ConcurrentWriteBatch, WriteBatch, WriteBuffer},
}; };
mod parallel_scheduler;
const MB: u64 = 1024 * 1024; const MB: u64 = 1024 * 1024;
const COMPACT_CONFIG: CompactConfig = CompactConfig { const COMPACT_CONFIG: CompactConfig = CompactConfig {
min_merge_count: 3, min_merge_count: 3,
...@@ -28,7 +28,7 @@ const COMPACT_CONFIG: CompactConfig = CompactConfig { ...@@ -28,7 +28,7 @@ const COMPACT_CONFIG: CompactConfig = CompactConfig {
}; };
pub struct TurboKeyValueDatabase { pub struct TurboKeyValueDatabase {
db: Arc<TurboPersistence>, db: Arc<TurboPersistence<TurboTasksParallelScheduler>>,
compact_join_handle: Mutex<Option<JoinHandle<Result<()>>>>, compact_join_handle: Mutex<Option<JoinHandle<Result<()>>>>,
is_ci: bool, is_ci: bool,
is_short_session: bool, is_short_session: bool,
...@@ -84,7 +84,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase { ...@@ -84,7 +84,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
) -> Result<WriteBatch<'_, Self::SerialWriteBatch<'_>, Self::ConcurrentWriteBatch<'_>>> { ) -> Result<WriteBatch<'_, Self::SerialWriteBatch<'_>, Self::ConcurrentWriteBatch<'_>>> {
// Wait for the compaction to finish // Wait for the compaction to finish
if let Some(join_handle) = self.compact_join_handle.lock().take() { if let Some(join_handle) = self.compact_join_handle.lock().take() {
join_handle.join().unwrap()?; join(join_handle)?;
} }
// Start a new write batch // Start a new write batch
Ok(WriteBatch::concurrent(TurboWriteBatch { Ok(WriteBatch::concurrent(TurboWriteBatch {
...@@ -100,7 +100,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase { ...@@ -100,7 +100,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
fn shutdown(&self) -> Result<()> { fn shutdown(&self) -> Result<()> {
// Wait for the compaction to finish // Wait for the compaction to finish
if let Some(join_handle) = self.compact_join_handle.lock().take() { if let Some(join_handle) = self.compact_join_handle.lock().take() {
join_handle.join().unwrap()?; join(join_handle)?;
} }
// Compact the database on shutdown // Compact the database on shutdown
self.db.compact(&CompactConfig { self.db.compact(&CompactConfig {
...@@ -118,8 +118,8 @@ impl KeyValueDatabase for TurboKeyValueDatabase { ...@@ -118,8 +118,8 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
} }
pub struct TurboWriteBatch<'a> { pub struct TurboWriteBatch<'a> {
batch: turbo_persistence::WriteBatch<WriteBuffer<'static>, 5>, batch: turbo_persistence::WriteBatch<WriteBuffer<'static>, TurboTasksParallelScheduler, 5>,
db: &'a Arc<TurboPersistence>, db: &'a Arc<TurboPersistence<TurboTasksParallelScheduler>>,
compact_join_handle: Option<&'a Mutex<Option<JoinHandle<Result<()>>>>>, compact_join_handle: Option<&'a Mutex<Option<JoinHandle<Result<()>>>>>,
} }
...@@ -144,7 +144,7 @@ impl<'a> BaseWriteBatch<'a> for TurboWriteBatch<'a> { ...@@ -144,7 +144,7 @@ impl<'a> BaseWriteBatch<'a> for TurboWriteBatch<'a> {
if let Some(compact_join_handle) = self.compact_join_handle { if let Some(compact_join_handle) = self.compact_join_handle {
// Start a new compaction in the background // Start a new compaction in the background
let db = self.db.clone(); let db = self.db.clone();
let handle = spawn(move || { let handle = spawn(async move {
db.compact(&CompactConfig { db.compact(&CompactConfig {
max_merge_segment_count: available_parallelism() max_merge_segment_count: available_parallelism()
.map_or(4, |c| max(4, c.get() / 2)), .map_or(4, |c| max(4, c.get() / 2)),
...@@ -220,3 +220,7 @@ impl<'l> From<WriteBuffer<'l>> for ValueBuffer<'l> { ...@@ -220,3 +220,7 @@ impl<'l> From<WriteBuffer<'l>> for ValueBuffer<'l> {
} }
} }
} }
fn join(handle: JoinHandle<Result<()>>) -> Result<()> {
block_in_place(|| Handle::current().block_on(handle))
}
use turbo_persistence::ParallelScheduler;
use turbo_tasks::parallel;
#[derive(Clone, Copy, Default)]
pub struct TurboTasksParallelScheduler;
impl ParallelScheduler for TurboTasksParallelScheduler {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync,
{
parallel::for_each(items, f);
}
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static,
{
parallel::try_for_each(items, f)
}
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
parallel::try_for_each_mut(items, f)
}
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
parallel::try_for_each_owned(items, f)
}
fn parallel_map_collect<'l, T, I, R>(
&self,
items: &'l [T],
f: impl Fn(&'l T) -> I + Send + Sync,
) -> R
where
T: Sync,
I: Send + Sync + 'l,
R: FromIterator<I>,
{
parallel::map_collect(items, f)
}
fn parallel_map_collect_owned<T, I, R>(
&self,
items: Vec<T>,
f: impl Fn(T) -> I + Send + Sync,
) -> R
where
T: Send + Sync,
I: Send + Sync,
R: FromIterator<I>,
{
parallel::map_collect_owned(items, f)
}
}
...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn all_in_one() { async fn all_in_one() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<u32> = Vc::cell(4242); let a: Vc<u32> = Vc::cell(4242);
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn basic() { async fn basic() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let output1 = func_without_args(); let output1 = func_without_args();
......
...@@ -24,7 +24,7 @@ struct TaskSpec { ...@@ -24,7 +24,7 @@ struct TaskSpec {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
struct TasksSpec(Vec<TaskSpec>); struct TasksSpec(Vec<TaskSpec>);
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn graph_bug() { async fn graph_bug() {
// see https://github.com/vercel/next.js/pull/79451 // see https://github.com/vercel/next.js/pull/79451
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
......
...@@ -33,7 +33,7 @@ pub struct TaskSpec { ...@@ -33,7 +33,7 @@ pub struct TaskSpec {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
struct Iteration(State<usize>); struct Iteration(State<usize>);
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn graph_bug() { async fn graph_bug() {
run(&REGISTRATION, move || async move { run(&REGISTRATION, move || async move {
let spec = vec![ let spec = vec![
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn functions() { async fn functions() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
assert_eq!(*fn_plain().await?, 42); assert_eq!(*fn_plain().await?, 42);
...@@ -53,7 +53,7 @@ async fn async_fn_vc_arg(n: Vc<u32>) -> Result<Vc<u32>> { ...@@ -53,7 +53,7 @@ async fn async_fn_vc_arg(n: Vc<u32>) -> Result<Vc<u32>> {
Ok(Vc::cell(*n.await?)) Ok(Vc::cell(*n.await?))
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn methods() { async fn methods() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
assert_eq!(*Value::static_method().await?, 42); assert_eq!(*Value::static_method().await?, 42);
...@@ -106,7 +106,7 @@ impl Value { ...@@ -106,7 +106,7 @@ impl Value {
} }
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn trait_methods() { async fn trait_methods() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
assert_eq!(*Value::static_trait_method().await?, 42); assert_eq!(*Value::static_trait_method().await?, 42);
......
...@@ -14,7 +14,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -14,7 +14,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn transitive_emitting() { async fn transitive_emitting() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!("")); let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!(""));
...@@ -32,7 +32,7 @@ async fn transitive_emitting() { ...@@ -32,7 +32,7 @@ async fn transitive_emitting() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn transitive_emitting_indirect() { async fn transitive_emitting_indirect() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!("")); let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!(""));
...@@ -50,7 +50,7 @@ async fn transitive_emitting_indirect() { ...@@ -50,7 +50,7 @@ async fn transitive_emitting_indirect() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn multi_emitting() { async fn multi_emitting() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_multi_emitting_function(); let result_op = my_multi_emitting_function();
...@@ -68,7 +68,7 @@ async fn multi_emitting() { ...@@ -68,7 +68,7 @@ async fn multi_emitting() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn taking_collectibles() { async fn taking_collectibles() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_collecting_function(); let result_op = my_collecting_function();
...@@ -84,7 +84,7 @@ async fn taking_collectibles() { ...@@ -84,7 +84,7 @@ async fn taking_collectibles() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn taking_collectibles_extra_layer() { async fn taking_collectibles_extra_layer() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_collecting_function_indirect(); let result_op = my_collecting_function_indirect();
...@@ -100,7 +100,7 @@ async fn taking_collectibles_extra_layer() { ...@@ -100,7 +100,7 @@ async fn taking_collectibles_extra_layer() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn taking_collectibles_parallel() { async fn taking_collectibles_parallel() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!("a")); let result_op = my_transitive_emitting_function(rcstr!(""), rcstr!("a"));
...@@ -142,7 +142,7 @@ async fn taking_collectibles_parallel() { ...@@ -142,7 +142,7 @@ async fn taking_collectibles_parallel() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn taking_collectibles_with_resolve() { async fn taking_collectibles_with_resolve() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let result_op = my_transitive_emitting_function_with_resolve(rcstr!("resolve")); let result_op = my_transitive_emitting_function_with_resolve(rcstr!("resolve"));
......
...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn primitive_debug() { async fn primitive_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<u32> = Vc::cell(42); let a: Vc<u32> = Vc::cell(42);
...@@ -20,7 +20,7 @@ async fn primitive_debug() { ...@@ -20,7 +20,7 @@ async fn primitive_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn transparent_debug() { async fn transparent_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<Transparent> = Transparent(42).cell(); let a: Vc<Transparent> = Transparent(42).cell();
...@@ -32,7 +32,7 @@ async fn transparent_debug() { ...@@ -32,7 +32,7 @@ async fn transparent_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn enum_none_debug() { async fn enum_none_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<Enum> = Enum::None.cell(); let a: Vc<Enum> = Enum::None.cell();
...@@ -44,7 +44,7 @@ async fn enum_none_debug() { ...@@ -44,7 +44,7 @@ async fn enum_none_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn enum_transparent_debug() { async fn enum_transparent_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<Enum> = Enum::Transparent(Transparent(42).resolved_cell()).cell(); let a: Vc<Enum> = Enum::Transparent(Transparent(42).resolved_cell()).cell();
...@@ -60,7 +60,7 @@ async fn enum_transparent_debug() { ...@@ -60,7 +60,7 @@ async fn enum_transparent_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn enum_inner_vc_debug() { async fn enum_inner_vc_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<Enum> = Enum::Enum(Enum::None.resolved_cell()).cell(); let a: Vc<Enum> = Enum::Enum(Enum::None.resolved_cell()).cell();
...@@ -76,7 +76,7 @@ async fn enum_inner_vc_debug() { ...@@ -76,7 +76,7 @@ async fn enum_inner_vc_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn struct_unit_debug() { async fn struct_unit_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<StructUnit> = StructUnit.cell(); let a: Vc<StructUnit> = StructUnit.cell();
...@@ -87,7 +87,7 @@ async fn struct_unit_debug() { ...@@ -87,7 +87,7 @@ async fn struct_unit_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn struct_transparent_debug() { async fn struct_transparent_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<StructWithTransparent> = StructWithTransparent { let a: Vc<StructWithTransparent> = StructWithTransparent {
...@@ -106,7 +106,7 @@ async fn struct_transparent_debug() { ...@@ -106,7 +106,7 @@ async fn struct_transparent_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn struct_vec_debug() { async fn struct_vec_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<StructWithVec> = StructWithVec { vec: vec![] }.cell(); let a: Vc<StructWithVec> = StructWithVec { vec: vec![] }.cell();
...@@ -135,7 +135,7 @@ async fn struct_vec_debug() { ...@@ -135,7 +135,7 @@ async fn struct_vec_debug() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn struct_ignore_debug() { async fn struct_ignore_debug() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: Vc<StructWithIgnore> = StructWithIgnore { let a: Vc<StructWithIgnore> = StructWithIgnore {
......
...@@ -15,7 +15,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -15,7 +15,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_spawns_detached() -> anyhow::Result<()> { async fn test_spawns_detached() -> anyhow::Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
// HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs`, just // HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs`, just
...@@ -82,7 +82,7 @@ async fn spawns_detached( ...@@ -82,7 +82,7 @@ async fn spawns_detached(
Vc::cell(()) Vc::cell(())
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_spawns_detached_changing() -> anyhow::Result<()> { async fn test_spawns_detached_changing() -> anyhow::Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
// HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs` // HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs`
......
...@@ -11,7 +11,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -11,7 +11,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn dirty_in_progress() { async fn dirty_in_progress() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let cases = [ let cases = [
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn recompute() { async fn recompute() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = ChangingInput { let input = ChangingInput {
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn filtered_trait_method_args() -> Result<()> { async fn filtered_trait_method_args() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let uses_arg = UsesArg.cell(); let uses_arg = UsesArg.cell();
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn hidden_mutate() { async fn hidden_mutate() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = create_input().resolve().await?; let input = create_input().resolve().await?;
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_local_task_id() -> Result<()> { async fn test_local_task_id() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let local_vc = get_local_task_id(); let local_vc = get_local_task_id();
......
...@@ -26,7 +26,7 @@ fn use_operations() -> Vc<i32> { ...@@ -26,7 +26,7 @@ fn use_operations() -> Vc<i32> {
forty_two.connect() forty_two.connect()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_use_operations() -> Result<()> { async fn test_use_operations() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
assert_eq!(*use_operations().await?, 42); assert_eq!(*use_operations().await?, 42);
......
...@@ -25,7 +25,7 @@ static FILE_PATH_REGEX: LazyLock<Regex> = ...@@ -25,7 +25,7 @@ static FILE_PATH_REGEX: LazyLock<Regex> =
// //
// This test depends on the process-wide global panic handler. This test must be run in its own // This test depends on the process-wide global panic handler. This test must be run in its own
// process in isolation of any other tests. // process in isolation of any other tests.
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_panic_hook() { async fn test_panic_hook() {
let prev_hook = take_hook(); let prev_hook = take_hook();
set_hook(Box::new(move |info| { set_hook(Box::new(move |info| {
......
...@@ -142,7 +142,7 @@ fn check_skip() -> bool { ...@@ -142,7 +142,7 @@ fn check_skip() -> bool {
false false
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_many_children() { async fn many_calls_to_many_children() {
if check_skip() { if check_skip() {
return; return;
...@@ -157,7 +157,7 @@ async fn many_calls_to_many_children() { ...@@ -157,7 +157,7 @@ async fn many_calls_to_many_children() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_uncached_many_children() { async fn many_calls_to_uncached_many_children() {
if check_skip() { if check_skip() {
return; return;
...@@ -189,7 +189,7 @@ fn run_big_graph_test(counts: Vec<u32>) -> impl Future<Output = Result<()>> + Se ...@@ -189,7 +189,7 @@ fn run_big_graph_test(counts: Vec<u32>) -> impl Future<Output = Result<()>> + Se
) )
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_1() { async fn many_calls_to_big_graph_1() {
if check_skip() { if check_skip() {
return; return;
...@@ -199,7 +199,7 @@ async fn many_calls_to_big_graph_1() { ...@@ -199,7 +199,7 @@ async fn many_calls_to_big_graph_1() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_2() { async fn many_calls_to_big_graph_2() {
if check_skip() { if check_skip() {
return; return;
...@@ -211,7 +211,7 @@ async fn many_calls_to_big_graph_2() { ...@@ -211,7 +211,7 @@ async fn many_calls_to_big_graph_2() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_3() { async fn many_calls_to_big_graph_3() {
if check_skip() { if check_skip() {
return; return;
...@@ -221,7 +221,7 @@ async fn many_calls_to_big_graph_3() { ...@@ -221,7 +221,7 @@ async fn many_calls_to_big_graph_3() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_4() { async fn many_calls_to_big_graph_4() {
if check_skip() { if check_skip() {
return; return;
...@@ -231,7 +231,7 @@ async fn many_calls_to_big_graph_4() { ...@@ -231,7 +231,7 @@ async fn many_calls_to_big_graph_4() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_5() { async fn many_calls_to_big_graph_5() {
if check_skip() { if check_skip() {
return; return;
...@@ -243,7 +243,7 @@ async fn many_calls_to_big_graph_5() { ...@@ -243,7 +243,7 @@ async fn many_calls_to_big_graph_5() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_6() { async fn many_calls_to_big_graph_6() {
if check_skip() { if check_skip() {
return; return;
...@@ -255,7 +255,7 @@ async fn many_calls_to_big_graph_6() { ...@@ -255,7 +255,7 @@ async fn many_calls_to_big_graph_6() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_7() { async fn many_calls_to_big_graph_7() {
if check_skip() { if check_skip() {
return; return;
...@@ -270,7 +270,7 @@ async fn many_calls_to_big_graph_7() { ...@@ -270,7 +270,7 @@ async fn many_calls_to_big_graph_7() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_8() { async fn many_calls_to_big_graph_8() {
if check_skip() { if check_skip() {
return; return;
...@@ -282,7 +282,7 @@ async fn many_calls_to_big_graph_8() { ...@@ -282,7 +282,7 @@ async fn many_calls_to_big_graph_8() {
.unwrap(); .unwrap();
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn many_calls_to_big_graph_9() { async fn many_calls_to_big_graph_9() {
if check_skip() { if check_skip() {
return; return;
......
...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn random_change() { async fn random_change() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let state = make_state(); let state = make_state();
......
...@@ -10,7 +10,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -10,7 +10,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn read_ref() { async fn read_ref() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let counter = Counter::cell(Counter { let counter = Counter::cell(Counter {
......
...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -8,7 +8,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn recompute() { async fn recompute() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = ChangingInput { let input = ChangingInput {
...@@ -58,7 +58,7 @@ async fn recompute() { ...@@ -58,7 +58,7 @@ async fn recompute() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn immutable_analysis() { async fn immutable_analysis() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = ChangingInput { let input = ChangingInput {
......
...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -9,7 +9,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn recompute() { async fn recompute() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = ChangingInput::new(1).resolve().await?; let input = ChangingInput::new(1).resolve().await?;
......
...@@ -23,7 +23,7 @@ fn assert_resolved(input: ResolvedVc<u32>) { ...@@ -23,7 +23,7 @@ fn assert_resolved(input: ResolvedVc<u32>) {
assert!(input_vc.is_resolved()); assert!(input_vc.is_resolved());
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_conversion() -> Result<()> { async fn test_conversion() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let unresolved: Vc<u32> = Vc::cell(42); let unresolved: Vc<u32> = Vc::cell(42);
...@@ -38,7 +38,7 @@ async fn test_conversion() -> Result<()> { ...@@ -38,7 +38,7 @@ async fn test_conversion() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_cell_construction() -> Result<()> { async fn test_cell_construction() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let a: ResolvedVc<u32> = ResolvedVc::cell(42); let a: ResolvedVc<u32> = ResolvedVc::cell(42);
...@@ -50,7 +50,7 @@ async fn test_cell_construction() -> Result<()> { ...@@ -50,7 +50,7 @@ async fn test_cell_construction() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_resolved_vc_as_arg() -> Result<()> { async fn test_resolved_vc_as_arg() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let unresolved: Vc<u32> = returns_int(42); let unresolved: Vc<u32> = returns_int(42);
...@@ -62,7 +62,7 @@ async fn test_resolved_vc_as_arg() -> Result<()> { ...@@ -62,7 +62,7 @@ async fn test_resolved_vc_as_arg() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_into_future() -> Result<()> { async fn test_into_future() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let mut resolved = ResolvedVc::cell(42); let mut resolved = ResolvedVc::cell(42);
...@@ -78,7 +78,7 @@ async fn test_into_future() -> Result<()> { ...@@ -78,7 +78,7 @@ async fn test_into_future() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_sidecast() -> Result<()> { async fn test_sidecast() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let concrete_value = ImplementsAAndB.resolved_cell(); let concrete_value = ImplementsAAndB.resolved_cell();
......
...@@ -11,7 +11,7 @@ static REGISTRATION: Registration = register!(); ...@@ -11,7 +11,7 @@ static REGISTRATION: Registration = register!();
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
struct Wrapper(Vec<u32>); struct Wrapper(Vec<u32>);
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_shrink_to_fit() -> Result<()> { async fn test_shrink_to_fit() -> Result<()> {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
// `Vec::shrink_to_fit` is implicitly called when a cell is constructed. // `Vec::shrink_to_fit` is implicitly called when a cell is constructed.
......
...@@ -13,7 +13,7 @@ use turbo_tasks_testing::{Registration, register, run_without_cache_check}; ...@@ -13,7 +13,7 @@ use turbo_tasks_testing::{Registration, register, run_without_cache_check};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_simple_task() -> Result<()> { async fn test_simple_task() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
...@@ -39,7 +39,7 @@ async fn test_simple_task() -> Result<()> { ...@@ -39,7 +39,7 @@ async fn test_simple_task() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_await_same_vc_multiple_times() -> Result<()> { async fn test_await_same_vc_multiple_times() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
...@@ -61,7 +61,7 @@ async fn test_await_same_vc_multiple_times() -> Result<()> { ...@@ -61,7 +61,7 @@ async fn test_await_same_vc_multiple_times() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_vc_receiving_task() -> Result<()> { async fn test_vc_receiving_task() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
...@@ -93,7 +93,7 @@ async fn test_vc_receiving_task() -> Result<()> { ...@@ -93,7 +93,7 @@ async fn test_vc_receiving_task() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_trait_methods() -> Result<()> { async fn test_trait_methods() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
...@@ -130,7 +130,7 @@ async fn test_trait_methods() -> Result<()> { ...@@ -130,7 +130,7 @@ async fn test_trait_methods() -> Result<()> {
.await .await
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_dyn_trait_methods() -> Result<()> { async fn test_dyn_trait_methods() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
...@@ -174,7 +174,7 @@ async fn test_dyn_trait_methods() -> Result<()> { ...@@ -174,7 +174,7 @@ async fn test_dyn_trait_methods() -> Result<()> {
} }
// creates Vcs, but doesn't ever execute them // creates Vcs, but doesn't ever execute them
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_no_execution() -> Result<()> { async fn test_no_execution() -> Result<()> {
run_without_cache_check(&REGISTRATION, async move { run_without_cache_check(&REGISTRATION, async move {
enable_stats(); enable_stats();
......
...@@ -18,7 +18,7 @@ Adder::add_method (read cell of type turbo-tasks@TODO::::primitives::u64) ...@@ -18,7 +18,7 @@ Adder::add_method (read cell of type turbo-tasks@TODO::::primitives::u64)
unknown transient task (read cell of type turbo-tasks@TODO::::primitives::u16) unknown transient task (read cell of type turbo-tasks@TODO::::primitives::u16)
unknown transient task (read cell of type turbo-tasks@TODO::::primitives::u32)"; unknown transient task (read cell of type turbo-tasks@TODO::::primitives::u32)";
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_trace_transient() { async fn test_trace_transient() {
let result = run_without_cache_check(&REGISTRATION, async { let result = run_without_cache_check(&REGISTRATION, async {
read_incorrect_task_input_operation(IncorrectTaskInput( read_incorrect_task_input_operation(IncorrectTaskInput(
......
...@@ -10,7 +10,7 @@ use turbo_tasks_testing::{Registration, register, run}; ...@@ -10,7 +10,7 @@ use turbo_tasks_testing::{Registration, register, run};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn trait_ref() { async fn trait_ref() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let counter = Counter::cell(Counter { let counter = Counter::cell(Counter {
......
...@@ -9,7 +9,7 @@ static REGISTRATION: Registration = register!(); ...@@ -9,7 +9,7 @@ static REGISTRATION: Registration = register!();
// Test that with `cell = "shared"`, the cell will be re-used as long as the // Test that with `cell = "shared"`, the cell will be re-used as long as the
// value is equal. // value is equal.
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_trait_ref_shared_cell_mode() { async fn test_trait_ref_shared_cell_mode() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = CellIdSelector { let input = CellIdSelector {
...@@ -44,7 +44,7 @@ async fn test_trait_ref_shared_cell_mode() { ...@@ -44,7 +44,7 @@ async fn test_trait_ref_shared_cell_mode() {
// Test that with `cell = "new"`, the cell will is never re-used, even if the // Test that with `cell = "new"`, the cell will is never re-used, even if the
// value is equal. // value is equal.
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_trait_ref_new_cell_mode() { async fn test_trait_ref_new_cell_mode() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let input = CellIdSelector { let input = CellIdSelector {
......
...@@ -10,7 +10,7 @@ static REGISTRATION: Registration = register!(); ...@@ -10,7 +10,7 @@ static REGISTRATION: Registration = register!();
const EXPECTED_MSG: &str = const EXPECTED_MSG: &str =
"Collectible is transient, transient collectibles cannot be emitted from persistent tasks"; "Collectible is transient, transient collectibles cannot be emitted from persistent tasks";
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_transient_emit_from_persistent() { async fn test_transient_emit_from_persistent() {
let result = run_without_cache_check(&REGISTRATION, async { let result = run_without_cache_check(&REGISTRATION, async {
emit_incorrect_task_input_operation(IncorrectTaskInput(U32Wrapper(123).resolved_cell())) emit_incorrect_task_input_operation(IncorrectTaskInput(U32Wrapper(123).resolved_cell()))
......
...@@ -7,7 +7,7 @@ use turbo_tasks_testing::{Registration, register, run_without_cache_check}; ...@@ -7,7 +7,7 @@ use turbo_tasks_testing::{Registration, register, run_without_cache_check};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn test_transient_vc() -> Result<()> { async fn test_transient_vc() -> Result<()> {
run_without_cache_check(&REGISTRATION, async { run_without_cache_check(&REGISTRATION, async {
test_transient_operation(TransientValue::new(123)) test_transient_operation(TransientValue::new(123))
......
...@@ -18,7 +18,7 @@ static REGISTRATION: Registration = register!(turbo_tasks_fetch::register); ...@@ -18,7 +18,7 @@ static REGISTRATION: Registration = register!(turbo_tasks_fetch::register);
/// acquire and hold this lock to prevent potential flakiness. /// acquire and hold this lock to prevent potential flakiness.
static GLOBAL_TEST_LOCK: TokioMutex<()> = TokioMutex::const_new(()); static GLOBAL_TEST_LOCK: TokioMutex<()> = TokioMutex::const_new(());
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn basic_get() { async fn basic_get() {
let _guard = GLOBAL_TEST_LOCK.lock().await; let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
...@@ -49,7 +49,7 @@ async fn basic_get() { ...@@ -49,7 +49,7 @@ async fn basic_get() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn sends_user_agent() { async fn sends_user_agent() {
let _guard = GLOBAL_TEST_LOCK.lock().await; let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
...@@ -85,7 +85,7 @@ async fn sends_user_agent() { ...@@ -85,7 +85,7 @@ async fn sends_user_agent() {
// This is temporary behavior. // This is temporary behavior.
// TODO: Implement invalidation that respects Cache-Control headers. // TODO: Implement invalidation that respects Cache-Control headers.
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn invalidation_does_not_invalidate() { async fn invalidation_does_not_invalidate() {
let _guard = GLOBAL_TEST_LOCK.lock().await; let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
...@@ -130,7 +130,7 @@ fn get_issue_context() -> Vc<FileSystemPath> { ...@@ -130,7 +130,7 @@ fn get_issue_context() -> Vc<FileSystemPath> {
DiskFileSystem::new(rcstr!("root"), rcstr!("/")).root() DiskFileSystem::new(rcstr!("root"), rcstr!("/")).root()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn errors_on_failed_connection() { async fn errors_on_failed_connection() {
let _guard = GLOBAL_TEST_LOCK.lock().await; let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
...@@ -161,7 +161,7 @@ async fn errors_on_failed_connection() { ...@@ -161,7 +161,7 @@ async fn errors_on_failed_connection() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn errors_on_404() { async fn errors_on_404() {
let _guard = GLOBAL_TEST_LOCK.lock().await; let _guard = GLOBAL_TEST_LOCK.lock().await;
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
...@@ -196,7 +196,7 @@ async fn errors_on_404() { ...@@ -196,7 +196,7 @@ async fn errors_on_404() {
.unwrap() .unwrap()
} }
#[tokio::test] #[tokio::test(flavor = "multi_thread")]
async fn client_cache() { async fn client_cache() {
// a simple fetch that should always succeed // a simple fetch that should always succeed
async fn simple_fetch(path: &str, client: FetchClient) -> anyhow::Result<()> { async fn simple_fetch(path: &str, client: FetchClient) -> anyhow::Result<()> {
......
...@@ -46,7 +46,6 @@ use dunce::simplified; ...@@ -46,7 +46,6 @@ use dunce::simplified;
use indexmap::IndexSet; use indexmap::IndexSet;
use jsonc_parser::{ParseOptions, parse_to_serde_value}; use jsonc_parser::{ParseOptions, parse_to_serde_value};
use mime::Mime; use mime::Mime;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
...@@ -56,7 +55,7 @@ use turbo_rcstr::{RcStr, rcstr}; ...@@ -56,7 +55,7 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
ApplyEffectsContext, Completion, InvalidationReason, Invalidator, NonLocalValue, ReadRef, ApplyEffectsContext, Completion, InvalidationReason, Invalidator, NonLocalValue, ReadRef,
ResolvedVc, TaskInput, ValueToString, Vc, debug::ValueDebugFormat, effect, ResolvedVc, TaskInput, ValueToString, Vc, debug::ValueDebugFormat, effect,
mark_session_dependent, mark_stateful, trace::TraceRawVcs, mark_session_dependent, mark_stateful, parallel, trace::TraceRawVcs,
}; };
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher, hash_xxh3_hash64}; use turbo_tasks_hash::{DeterministicHash, DeterministicHasher, hash_xxh3_hash64};
use turbo_unix_path::{ use turbo_unix_path::{
...@@ -309,19 +308,14 @@ impl DiskFileSystemInner { ...@@ -309,19 +308,14 @@ impl DiskFileSystemInner {
fn invalidate(&self) { fn invalidate(&self) {
let _span = tracing::info_span!("invalidate filesystem", name = &*self.root).entered(); let _span = tracing::info_span!("invalidate filesystem", name = &*self.root).entered();
let span = tracing::Span::current();
let handle = tokio::runtime::Handle::current();
let invalidator_map = take(&mut *self.invalidator_map.lock().unwrap()); let invalidator_map = take(&mut *self.invalidator_map.lock().unwrap());
let dir_invalidator_map = take(&mut *self.dir_invalidator_map.lock().unwrap()); let dir_invalidator_map = take(&mut *self.dir_invalidator_map.lock().unwrap());
let iter = invalidator_map let invalidators = invalidator_map
.into_par_iter() .into_iter()
.chain(dir_invalidator_map.into_par_iter()) .chain(dir_invalidator_map)
.flat_map(|(_, invalidators)| invalidators.into_par_iter()); .flat_map(|(_, invalidators)| invalidators.into_keys())
iter.for_each(|(i, _)| { .collect::<Vec<_>>();
let _span = span.clone().entered(); parallel::for_each_owned(invalidators, |invalidator| invalidator.invalidate());
let _guard = handle.enter();
i.invalidate()
});
} }
/// Invalidates every tracked file in the filesystem. /// Invalidates every tracked file in the filesystem.
...@@ -332,23 +326,19 @@ impl DiskFileSystemInner { ...@@ -332,23 +326,19 @@ impl DiskFileSystemInner {
reason: impl Fn(&Path) -> R + Sync, reason: impl Fn(&Path) -> R + Sync,
) { ) {
let _span = tracing::info_span!("invalidate filesystem", name = &*self.root).entered(); let _span = tracing::info_span!("invalidate filesystem", name = &*self.root).entered();
let span = tracing::Span::current();
let handle = tokio::runtime::Handle::current();
let invalidator_map = take(&mut *self.invalidator_map.lock().unwrap()); let invalidator_map = take(&mut *self.invalidator_map.lock().unwrap());
let dir_invalidator_map = take(&mut *self.dir_invalidator_map.lock().unwrap()); let dir_invalidator_map = take(&mut *self.dir_invalidator_map.lock().unwrap());
let iter = invalidator_map let invalidators = invalidator_map
.into_par_iter() .into_iter()
.chain(dir_invalidator_map.into_par_iter()) .chain(dir_invalidator_map)
.flat_map(|(path, invalidators)| { .flat_map(|(path, invalidators)| {
let _span = span.clone().entered();
let reason_for_path = reason(&path); let reason_for_path = reason(&path);
invalidators invalidators
.into_par_iter() .into_keys()
.map(move |i| (reason_for_path.clone(), i)) .map(move |i| (reason_for_path.clone(), i))
}); })
iter.for_each(|(reason, (invalidator, _))| { .collect::<Vec<_>>();
let _span = span.clone().entered(); parallel::for_each_owned(invalidators, |(reason, invalidator)| {
let _guard = handle.enter();
invalidator.invalidate_with_reason(reason) invalidator.invalidate_with_reason(reason)
}); });
} }
......
...@@ -16,13 +16,12 @@ use notify::{ ...@@ -16,13 +16,12 @@ use notify::{
Config, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher, Config, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher,
event::{MetadataKind, ModifyKind, RenameMode}, event::{MetadataKind, ModifyKind, RenameMode},
}; };
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::instrument; use tracing::instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, InvalidationReason, InvalidationReasonKind, Invalidator, spawn_thread, FxIndexSet, InvalidationReason, InvalidationReasonKind, Invalidator, parallel, spawn_thread,
util::StaticOrArc, util::StaticOrArc,
}; };
...@@ -397,40 +396,30 @@ impl DiskWatcher { ...@@ -397,40 +396,30 @@ impl DiskWatcher {
// //
// Best is to start_watching before starting to read // Best is to start_watching before starting to read
{ {
let span = tracing::info_span!("invalidate filesystem"); let _span = tracing::info_span!("invalidate filesystem").entered();
let _span = span.clone().entered();
let invalidator_map = take(&mut *fs_inner.invalidator_map.lock().unwrap()); let invalidator_map = take(&mut *fs_inner.invalidator_map.lock().unwrap());
let dir_invalidator_map = take(&mut *fs_inner.dir_invalidator_map.lock().unwrap()); let dir_invalidator_map = take(&mut *fs_inner.dir_invalidator_map.lock().unwrap());
let iter = invalidator_map let iter = invalidator_map.into_iter().chain(dir_invalidator_map);
.into_par_iter()
.chain(dir_invalidator_map.into_par_iter());
let handle = tokio::runtime::Handle::current();
if report_invalidation_reason { if report_invalidation_reason {
iter.flat_map(|(path, invalidators)| { let invalidators = iter
let _span = span.clone().entered(); .flat_map(|(path, invalidators)| {
let reason = WatchStart { let reason = WatchStart {
name: fs_inner.name.clone(), name: fs_inner.name.clone(),
// this path is just used for display purposes // this path is just used for display purposes
path: RcStr::from(path.to_string_lossy()), path: RcStr::from(path.to_string_lossy()),
}; };
invalidators invalidators.into_iter().map(move |i| (reason.clone(), i))
.into_par_iter() })
.map(move |i| (reason.clone(), i)) .collect::<Vec<_>>();
}) parallel::for_each_owned(invalidators, |(reason, (invalidator, _))| {
.for_each(|(reason, (invalidator, _))| { invalidator.invalidate_with_reason(reason);
let _span = span.clone().entered();
let _guard = handle.enter();
invalidator.invalidate_with_reason(reason)
}); });
} else { } else {
iter.flat_map(|(_, invalidators)| { let invalidators = iter
let _span = span.clone().entered(); .flat_map(|(_, invalidators)| invalidators.into_keys())
invalidators.into_par_iter().map(move |i| i) .collect::<Vec<_>>();
}) parallel::for_each_owned(invalidators, |invalidator| {
.for_each(|(invalidator, _)| { invalidator.invalidate();
let _span = span.clone().entered();
let _guard = handle.enter();
invalidator.invalidate()
}); });
} }
} }
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#![feature(never_type)] #![feature(never_type)]
#![feature(downcast_unchecked)] #![feature(downcast_unchecked)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(sync_unsafe_cell)]
#![feature(vec_into_raw_parts)]
pub mod backend; pub mod backend;
mod capture_future; mod capture_future;
...@@ -64,13 +66,14 @@ mod no_move_vec; ...@@ -64,13 +66,14 @@ mod no_move_vec;
mod once_map; mod once_map;
mod output; mod output;
pub mod panic_hooks; pub mod panic_hooks;
pub mod parallel;
pub mod persisted_graph; pub mod persisted_graph;
pub mod primitives; pub mod primitives;
mod raw_vc; mod raw_vc;
mod read_options; mod read_options;
mod read_ref; mod read_ref;
pub mod registry; pub mod registry;
mod scope; pub mod scope;
mod serialization_invalidation; mod serialization_invalidation;
pub mod small_duration; pub mod small_duration;
mod spawn; mod spawn;
...@@ -115,7 +118,6 @@ pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError}; ...@@ -115,7 +118,6 @@ pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};
pub use read_options::ReadCellOptions; pub use read_options::ReadCellOptions;
pub use read_ref::ReadRef; pub use read_ref::ReadRef;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
pub use scope::scope;
pub use serialization_invalidation::SerializationInvalidator; pub use serialization_invalidation::SerializationInvalidator;
pub use shrink_to_fit::ShrinkToFit; pub use shrink_to_fit::ShrinkToFit;
pub use spawn::{JoinHandle, spawn, spawn_blocking, spawn_thread}; pub use spawn::{JoinHandle, spawn, spawn_blocking, spawn_thread};
......
...@@ -1058,27 +1058,30 @@ impl<B: Backend + 'static> TurboTasks<B> { ...@@ -1058,27 +1058,30 @@ impl<B: Backend + 'static> TurboTasks<B> {
} }
pub async fn stop_and_wait(&self) { pub async fn stop_and_wait(&self) {
self.backend.stopping(self); turbo_tasks_future_scope(self.pin(), async move {
self.stopped.store(true, Ordering::Release); self.backend.stopping(self);
{ self.stopped.store(true, Ordering::Release);
let listener = self {
.event let listener = self
.listen_with_note(|| || "wait for stop".to_string()); .event
if self.currently_scheduled_tasks.load(Ordering::Acquire) != 0 { .listen_with_note(|| || "wait for stop".to_string());
listener.await; if self.currently_scheduled_tasks.load(Ordering::Acquire) != 0 {
listener.await;
}
} }
}
{
let listener = self.event_background.listen();
if self
.currently_scheduled_background_jobs
.load(Ordering::Acquire)
!= 0
{ {
listener.await; let listener = self.event_background.listen();
if self
.currently_scheduled_background_jobs
.load(Ordering::Acquire)
!= 0
{
listener.await;
}
} }
} self.backend.stop(self);
self.backend.stop(self); })
.await;
} }
#[track_caller] #[track_caller]
...@@ -1675,6 +1678,10 @@ pub fn turbo_tasks() -> Arc<dyn TurboTasksApi> { ...@@ -1675,6 +1678,10 @@ pub fn turbo_tasks() -> Arc<dyn TurboTasksApi> {
TURBO_TASKS.with(|arc| arc.clone()) TURBO_TASKS.with(|arc| arc.clone())
} }
pub fn try_turbo_tasks() -> Option<Arc<dyn TurboTasksApi>> {
TURBO_TASKS.try_with(|arc| arc.clone()).ok()
}
pub fn with_turbo_tasks<T>(func: impl FnOnce(&Arc<dyn TurboTasksApi>) -> T) -> T { pub fn with_turbo_tasks<T>(func: impl FnOnce(&Arc<dyn TurboTasksApi>) -> T) -> T {
TURBO_TASKS.with(|arc| func(arc)) TURBO_TASKS.with(|arc| func(arc))
} }
......
//! Parallel for each and map using tokio tasks.
//!
//! This avoid the problem of sleeping threads with mimalloc when using rayon in combination with
//! tokio. It also avoid having multiple thread pools.
//! see also https://pwy.io/posts/mimalloc-cigarette/
use std::{sync::LazyLock, thread::available_parallelism};
use crate::{scope::scope_and_block, util::into_chunks};
/// Calculates a good chunk size for parallel processing based on the number of available threads.
/// This is used to ensure that the workload is evenly distributed across the threads.
fn good_chunk_size(len: usize) -> usize {
static GOOD_CHUNK_COUNT: LazyLock<usize> =
LazyLock::new(|| available_parallelism().map_or(16, |c| c.get() * 4));
let min_chunk_count = *GOOD_CHUNK_COUNT;
len.div_ceil(min_chunk_count)
}
pub fn for_each<'l, T, F>(items: &'l [T], f: F)
where
T: Sync,
F: Fn(&'l T) + Send + Sync,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item);
}
return;
}
let chunk_size = good_chunk_size(len);
let f = &f;
let _results = scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item);
}
})
}
});
}
pub fn for_each_owned<T>(items: Vec<T>, f: impl Fn(T) + Send + Sync)
where
T: Send + Sync,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item);
}
return;
}
let chunk_size = good_chunk_size(len);
let f = &f;
let _results = scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move {
// SAFETY: Even when f() panics we drop all items in the chunk.
for item in chunk {
f(item);
}
})
}
});
}
pub fn try_for_each<'l, T, E>(
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn try_for_each_mut<'l, T, E>(
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks_mut(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn try_for_each_owned<T, E>(
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn map_collect<'l, Item, PerItemResult, Result>(
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
let len = items.len();
if len == 0 {
return Result::from_iter(std::iter::empty()); // No items to process, return empty
// collection
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move { chunk.iter().map(f).collect::<Vec<_>>() })
}
})
.flatten()
.collect()
}
pub fn map_collect_owned<'l, Item, PerItemResult, Result>(
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
let len = items.len();
if len == 0 {
return Result::from_iter(std::iter::empty()); // No items to process, return empty
// collection;
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move { chunk.map(f).collect::<Vec<_>>() })
}
})
.flatten()
.collect()
}
#[cfg(test)]
mod tests {
use std::{
panic::{AssertUnwindSafe, catch_unwind},
sync::atomic::{AtomicI32, Ordering},
};
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_for_each() {
let input = vec![1, 2, 3, 4, 5];
let sum = AtomicI32::new(0);
for_each(&input, |&x| {
sum.fetch_add(x, Ordering::SeqCst);
});
assert_eq!(sum.load(Ordering::SeqCst), 15);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_try_for_each() {
let input = vec![1, 2, 3, 4, 5];
let result = try_for_each(&input, |&x| {
if x % 2 == 0 {
Ok(())
} else {
Err(format!("Odd number {x} encountered"))
}
});
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Odd number 1 encountered");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_try_for_each_mut() {
let mut input = vec![1, 2, 3, 4, 5];
let result = try_for_each_mut(&mut input, |x| {
*x += 10;
if *x % 2 == 0 {
Ok(())
} else {
Err(format!("Odd number {} encountered", *x))
}
});
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Odd number 11 encountered");
assert_eq!(input, vec![11, 12, 13, 14, 15]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_for_each_owned() {
let input = vec![1, 2, 3, 4, 5];
let sum = AtomicI32::new(0);
for_each_owned(input, |x| {
sum.fetch_add(x, Ordering::SeqCst);
});
assert_eq!(sum.load(Ordering::SeqCst), 15);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect() {
let input = vec![1, 2, 3, 4, 5];
let result: Vec<_> = map_collect(&input, |&x| x * 2);
assert_eq!(result, vec![2, 4, 6, 8, 10]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect_owned() {
let input = vec![1, 2, 3, 4, 5];
let result: Vec<_> = map_collect_owned(input, |x| x * 2);
assert_eq!(result, vec![2, 4, 6, 8, 10]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect_owned_many() {
let input = vec![1; 1000];
let result: Vec<_> = map_collect_owned(input, |x| x * 2);
assert_eq!(result, vec![2; 1000]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_panic_in_scope() {
let result = catch_unwind(AssertUnwindSafe(|| {
let mut input = vec![1; 1000];
input[744] = 2;
for_each(&input, |x| {
if *x == 2 {
panic!("Intentional panic");
}
});
panic!("Should not get here")
}));
assert!(result.is_err());
assert_eq!(
result.unwrap_err().downcast_ref::<&str>(),
Some(&"Intentional panic")
);
}
}
This diff is collapsed.
use std::{ use std::{
cell::SyncUnsafeCell,
error::Error as StdError, error::Error as StdError,
fmt::{Debug, Display}, fmt::{Debug, Display},
future::Future, future::Future,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::ManuallyDrop,
ops::Deref, ops::Deref,
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
...@@ -259,3 +261,124 @@ impl<F: Future, W: for<'a> Fn(Pin<&mut F>, &mut Context<'a>) -> Poll<F::Output>> ...@@ -259,3 +261,124 @@ impl<F: Future, W: for<'a> Fn(Pin<&mut F>, &mut Context<'a>) -> Poll<F::Output>>
(this.wrapper)(this.future, cx) (this.wrapper)(this.future, cx)
} }
} }
/// Similar to slice::chunks but for owned data. Chunks are Send and Sync to allow to use it for
/// parallelism.
pub fn into_chunks<T>(data: Vec<T>, chunk_size: usize) -> IntoChunks<T> {
let (ptr, length, capacity) = data.into_raw_parts();
// SAFETY: changing a pointer from T to SyncUnsafeCell<ManuallyDrop<..>> is safe as both types
// have repr(transparent).
let ptr = ptr as *mut SyncUnsafeCell<ManuallyDrop<T>>;
// SAFETY: The ptr, length and capacity were from into_raw_parts(). This is the only place where
// we use ptr.
let data =
unsafe { Vec::<SyncUnsafeCell<ManuallyDrop<T>>>::from_raw_parts(ptr, length, capacity) };
IntoChunks {
data: Arc::new(data),
index: 0,
chunk_size,
}
}
pub struct IntoChunks<T> {
data: Arc<Vec<SyncUnsafeCell<ManuallyDrop<T>>>>,
index: usize,
chunk_size: usize,
}
impl<T> Iterator for IntoChunks<T> {
type Item = Chunk<T>;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.data.len() {
let end = self.data.len().min(self.index + self.chunk_size);
let item = Chunk {
data: Arc::clone(&self.data),
index: self.index,
end,
};
self.index = end;
Some(item)
} else {
None
}
}
}
impl<T> IntoChunks<T> {
fn next_item(&mut self) -> Option<T> {
if self.index < self.data.len() {
// SAFETY: We are the only owner of this chunk of data and we make sure that this item
// is no longer dropped by moving the index
let item = unsafe { ManuallyDrop::take(&mut *self.data[self.index].get()) };
self.index += 1;
Some(item)
} else {
None
}
}
}
impl<T> Drop for IntoChunks<T> {
fn drop(&mut self) {
// To avoid leaking memory we need to drop the remaining items
while self.next_item().is_some() {}
}
}
pub struct Chunk<T> {
data: Arc<Vec<SyncUnsafeCell<ManuallyDrop<T>>>>,
index: usize,
end: usize,
}
impl<T> Iterator for Chunk<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.end {
// SAFETY: We are the only owner of this chunk of data and we make sure that this item
// is no longer dropped by moving the index
let item = unsafe { ManuallyDrop::take(&mut *self.data[self.index].get()) };
self.index += 1;
Some(item)
} else {
None
}
}
}
impl<T> Drop for Chunk<T> {
fn drop(&mut self) {
// To avoid leaking memory we need to drop the remaining items
while self.next().is_some() {}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chunk_iterator() {
let data = [(); 10]
.into_iter()
.enumerate()
.map(|(i, _)| Arc::new(i))
.collect::<Vec<_>>();
let mut chunks = into_chunks(data.clone(), 3);
let mut first_chunk = chunks.next().unwrap();
let second_chunk = chunks.next().unwrap();
drop(chunks);
assert_eq!(
second_chunk.into_iter().map(|a| *a).collect::<Vec<_>>(),
vec![3, 4, 5]
);
assert_eq!(*first_chunk.next().unwrap(), 0);
assert_eq!(*first_chunk.next().unwrap(), 1);
drop(first_chunk);
for arc in data {
assert_eq!(Arc::strong_count(&arc), 1);
}
}
}
...@@ -272,7 +272,7 @@ fn test_cases() {} ...@@ -272,7 +272,7 @@ fn test_cases() {}
#[apply(test_cases)] #[apply(test_cases)]
fn node_file_trace_noop_backing_storage(#[case] input: CaseInput) { fn node_file_trace_noop_backing_storage(#[case] input: CaseInput) {
node_file_trace(input, "noop_backing_storage", false, 1, 120, |_| { node_file_trace(input, "noop_backing_storage", 1, 120, |_| {
TurboTasks::new(TurboTasksBackend::new( TurboTasks::new(TurboTasksBackend::new(
turbo_tasks_backend::BackendOptions::default(), turbo_tasks_backend::BackendOptions::default(),
turbo_tasks_backend::noop_backing_storage(), turbo_tasks_backend::noop_backing_storage(),
...@@ -282,7 +282,7 @@ fn node_file_trace_noop_backing_storage(#[case] input: CaseInput) { ...@@ -282,7 +282,7 @@ fn node_file_trace_noop_backing_storage(#[case] input: CaseInput) {
#[apply(test_cases)] #[apply(test_cases)]
fn node_file_trace_persistent(#[case] input: CaseInput) { fn node_file_trace_persistent(#[case] input: CaseInput) {
node_file_trace(input, "persistent_cache", false, 2, 240, |directory_path| { node_file_trace(input, "persistent_cache", 2, 240, |directory_path| {
TurboTasks::new(TurboTasksBackend::new( TurboTasks::new(TurboTasksBackend::new(
turbo_tasks_backend::BackendOptions::default(), turbo_tasks_backend::BackendOptions::default(),
turbo_tasks_backend::default_backing_storage( turbo_tasks_backend::default_backing_storage(
...@@ -302,31 +302,18 @@ fn node_file_trace_persistent(#[case] input: CaseInput) { ...@@ -302,31 +302,18 @@ fn node_file_trace_persistent(#[case] input: CaseInput) {
#[cfg(feature = "bench_against_node_nft")] #[cfg(feature = "bench_against_node_nft")]
#[apply(test_cases)] #[apply(test_cases)]
fn bench_against_node_nft_st(#[case] input: CaseInput) { fn bench_against_node_nft(#[case] input: CaseInput) {
bench_against_node_nft_inner(input, false); bench_against_node_nft_inner(input);
} }
#[cfg(feature = "bench_against_node_nft")] #[cfg(feature = "bench_against_node_nft")]
#[apply(test_cases)] fn bench_against_node_nft_inner(input: CaseInput) {
fn bench_against_node_nft_mt(#[case] input: CaseInput) { node_file_trace(input, "noop_backing_storage", 1, 120, |_| {
bench_against_node_nft_inner(input, true); TurboTasks::new(TurboTasksBackend::new(
} turbo_tasks_backend::BackendOptions::default(),
turbo_tasks_backend::noop_backing_storage(),
#[cfg(feature = "bench_against_node_nft")] ))
fn bench_against_node_nft_inner(input: CaseInput, multi_threaded: bool) { });
node_file_trace(
input,
"noop_backing_storage",
multi_threaded,
1,
120,
|_| {
TurboTasks::new(TurboTasksBackend::new(
turbo_tasks_backend::BackendOptions::default(),
turbo_tasks_backend::noop_backing_storage(),
))
},
);
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
...@@ -401,7 +388,6 @@ fn node_file_trace<B: Backend + 'static>( ...@@ -401,7 +388,6 @@ fn node_file_trace<B: Backend + 'static>(
expected_stderr, expected_stderr,
}: CaseInput, }: CaseInput,
mode: &str, mode: &str,
multi_threaded: bool,
run_count: i32, run_count: i32,
timeout_len: u64, timeout_len: u64,
create_turbo_tasks: impl Fn(&Path) -> Arc<TurboTasks<B>>, create_turbo_tasks: impl Fn(&Path) -> Arc<TurboTasks<B>>,
...@@ -410,15 +396,9 @@ fn node_file_trace<B: Backend + 'static>( ...@@ -410,15 +396,9 @@ fn node_file_trace<B: Backend + 'static>(
LazyLock::new(|| Arc::new(Mutex::new(Vec::new()))); LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
let r = &mut { let r = &mut {
let mut builder = if multi_threaded { let mut builder = tokio::runtime::Builder::new_multi_thread();
tokio::runtime::Builder::new_multi_thread()
} else {
tokio::runtime::Builder::new_current_thread()
};
builder.enable_all(); builder.enable_all();
if !multi_threaded { builder.max_blocking_threads(20);
builder.max_blocking_threads(20);
}
builder.build().unwrap() builder.build().unwrap()
}; };
r.block_on(async move { r.block_on(async move {
...@@ -490,12 +470,7 @@ fn node_file_trace<B: Backend + 'static>( ...@@ -490,12 +470,7 @@ fn node_file_trace<B: Backend + 'static>(
bench_suites_lock.push(BenchSuite { bench_suites_lock.push(BenchSuite {
suite: input suite: input
.trim_start_matches("node-file-trace/integration/") .trim_start_matches("node-file-trace/integration/")
.to_string() .to_string(),
+ (if multi_threaded {
" (multi-threaded)"
} else {
""
}),
is_faster, is_faster,
rust_duration, rust_duration,
node_duration, node_duration,
......
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