Skip to content

Commit

Permalink
refactor(turbo-tasks): Use an enum for strongly_consistent/`ReadCon…
Browse files Browse the repository at this point in the history
…sistency` instead of a bool
  • Loading branch information
bgw committed Aug 14, 2024
1 parent eeea535 commit cfba2dc
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 82 deletions.
5 changes: 3 additions & 2 deletions crates/next-build-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use next_api::{
project::{ProjectContainer, ProjectOptions},
route::{Endpoint, Route},
};
use turbo_tasks::{RcStr, TransientInstance, TurboTasks, Vc};
use turbo_tasks::{RcStr, ReadConsistency, TransientInstance, TurboTasks, Vc};
use turbo_tasks_malloc::TurboMalloc;
use turbo_tasks_memory::MemoryBackend;

Expand Down Expand Up @@ -261,7 +261,8 @@ async fn hmr(tt: &TurboTasks<MemoryBackend>, project: Vc<ProjectContainer>) -> R
Ok(Vc::<()>::cell(()))
}
});
tt.wait_task_completion(task, true).await?;
tt.wait_task_completion(task, ReadConsistency::Strong)
.await?;
let e = start.elapsed();
if e.as_millis() > 10 {
tracing::info!("HMR: {:?} {:?}", ident, e);
Expand Down
13 changes: 9 additions & 4 deletions turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use serde::Deserialize;
use serde::Serialize;
use tokio::sync::mpsc::channel;
use turbo_tasks::{
backend::Backend, util::FormatDuration, RcStr, TaskId, TransientInstance, TransientValue,
TurboTasks, UpdateInfo, Value, Vc,
backend::Backend, util::FormatDuration, RcStr, ReadConsistency, TaskId, TransientInstance,
TransientValue, TurboTasks, UpdateInfo, Value, Vc,
};
use turbo_tasks_fs::{
glob::Glob, DirectoryEntry, DiskFileSystem, FileSystem, FileSystemPath, ReadGlobResult,
Expand Down Expand Up @@ -407,7 +407,10 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
let start = Instant::now();
let finish = |tt: Arc<TurboTasks<B>>, root_task: TaskId| async move {
if watch {
if let Err(e) = tt.wait_task_completion(root_task, true).await {
if let Err(e) = tt
.wait_task_completion(root_task, ReadConsistency::Strong)
.await
{
println!("{}", e);
}
let UpdateInfo {
Expand All @@ -431,7 +434,9 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
println!("updated {} tasks in {}", tasks, FormatDuration(duration));
}
} else {
let result = tt.wait_task_completion(root_task, true).await;
let result = tt
.wait_task_completion(root_task, ReadConsistency::Strong)
.await;
let dur = start.elapsed();
let UpdateInfo {
duration, tasks, ..
Expand Down
6 changes: 4 additions & 2 deletions turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use anyhow::Result;
use sha2::{Digest, Sha256};
use turbo_tasks::{util::FormatDuration, RcStr, TurboTasks, UpdateInfo, Vc};
use turbo_tasks::{util::FormatDuration, RcStr, ReadConsistency, TurboTasks, UpdateInfo, Vc};
use turbo_tasks_fs::{
register, DirectoryContent, DirectoryEntry, DiskFileSystem, FileContent, FileSystem,
FileSystemPath,
Expand Down Expand Up @@ -41,7 +41,9 @@ async fn main() -> Result<()> {
Ok::<Vc<()>, _>(Default::default())
})
});
tt.wait_task_completion(task, true).await.unwrap();
tt.wait_task_completion(task, ReadConsistency::Strong)
.await
.unwrap();
println!("done in {}", FormatDuration(start.elapsed()));

loop {
Expand Down
6 changes: 4 additions & 2 deletions turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use anyhow::Result;
use sha2::{Digest, Sha256};
use turbo_tasks::{util::FormatDuration, RcStr, TurboTasks, UpdateInfo, Vc};
use turbo_tasks::{util::FormatDuration, RcStr, ReadConsistency, TurboTasks, UpdateInfo, Vc};
use turbo_tasks_fs::{
glob::Glob, register, DirectoryEntry, DiskFileSystem, FileContent, FileSystem, FileSystemPath,
ReadGlobResult,
Expand Down Expand Up @@ -40,7 +40,9 @@ async fn main() -> Result<()> {
Ok::<Vc<()>, _>(Default::default())
})
});
tt.wait_task_completion(task, true).await.unwrap();
tt.wait_task_completion(task, ReadConsistency::Strong)
.await
.unwrap();
println!("done in {}", FormatDuration(start.elapsed()));

loop {
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks-memory/benches/scope_stress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use criterion::{BenchmarkId, Criterion};
use turbo_tasks::{Completion, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks::{Completion, ReadConsistency, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_memory::MemoryBackend;

use super::register;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn scope_stress(c: &mut Criterion) {
rectangle(a, b).strongly_consistent().await?;
Ok::<Vc<()>, _>(Default::default())
});
tt.wait_task_completion(task, false).await
tt.wait_task_completion(task, ReadConsistency::Weak).await
}
})
.try_join()
Expand Down
6 changes: 4 additions & 2 deletions turbopack/crates/turbo-tasks-memory/benches/stress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use criterion::{BenchmarkId, Criterion};
use turbo_tasks::{TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks::{ReadConsistency, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_memory::MemoryBackend;

use super::register;
Expand Down Expand Up @@ -43,7 +43,9 @@ pub fn fibonacci(c: &mut Criterion) {
(0..size).map(|i| fib(i, i)).try_join().await?;
Ok::<Vc<()>, _>(Default::default())
});
tt.wait_task_completion(task, false).await.unwrap();
tt.wait_task_completion(task, ReadConsistency::Weak)
.await
.unwrap();
tt
}
})
Expand Down
16 changes: 8 additions & 8 deletions turbopack/crates/turbo-tasks-memory/src/memory_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use turbo_tasks::{
},
event::EventListener,
util::{IdFactoryWithReuse, NoMoveVec},
CellId, FunctionId, RawVc, TaskId, TaskIdSet, TraitTypeId, TurboTasksBackendApi, Unused,
ValueTypeId, TRANSIENT_TASK_BIT,
CellId, FunctionId, RawVc, ReadConsistency, TaskId, TaskIdSet, TraitTypeId,
TurboTasksBackendApi, Unused, ValueTypeId, TRANSIENT_TASK_BIT,
};

use crate::{
Expand Down Expand Up @@ -115,13 +115,13 @@ impl MemoryBackend {
fn try_get_output<T, F: FnOnce(&mut Output) -> Result<T>>(
&self,
id: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
note: impl Fn() -> String + Sync + Send + 'static,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
func: F,
) -> Result<Result<T, EventListener>> {
self.with_task(id, |task| {
task.get_or_wait_output(strongly_consistent, func, note, self, turbo_tasks)
task.get_or_wait_output(consistency, func, note, self, turbo_tasks)
})
}

Expand Down Expand Up @@ -517,15 +517,15 @@ impl Backend for MemoryBackend {
&self,
task: TaskId,
reader: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) -> Result<Result<RawVc, EventListener>> {
if task == reader {
bail!("reading it's own output is not possible");
}
self.try_get_output(
task,
strongly_consistent,
consistency,
move || format!("reading task output from {reader}"),
turbo_tasks,
|output| {
Expand All @@ -538,12 +538,12 @@ impl Backend for MemoryBackend {
fn try_read_task_output_untracked(
&self,
task: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) -> Result<Result<RawVc, EventListener>> {
self.try_get_output(
task,
strongly_consistent,
consistency,
|| "reading task output untracked".to_string(),
turbo_tasks,
|output| output.read_untracked(),
Expand Down
10 changes: 4 additions & 6 deletions turbopack/crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use turbo_prehash::PreHashed;
use turbo_tasks::{
backend::{CachedTaskType, CellContent, TaskCollectiblesMap, TaskExecutionSpec},
event::{Event, EventListener},
get_invalidator, registry, CellId, Invalidator, RawVc, TaskId, TaskIdSet, TraitTypeId,
TurboTasksBackendApi, ValueTypeId,
get_invalidator, registry, CellId, Invalidator, RawVc, ReadConsistency, TaskId, TaskIdSet,
TraitTypeId, TurboTasksBackendApi, ValueTypeId,
};

use crate::{
Expand Down Expand Up @@ -1621,17 +1621,15 @@ impl Task {

pub(crate) fn get_or_wait_output<T, F: FnOnce(&mut Output) -> Result<T>>(
&self,
strongly_consistent: bool,
consistency: ReadConsistency,
func: F,
note: impl Fn() -> String + Sync + Send + 'static,
backend: &MemoryBackend,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) -> Result<Result<T, EventListener>> {
let mut aggregation_context = TaskAggregationContext::new(turbo_tasks, backend);
if strongly_consistent {
let mut state = if consistency == ReadConsistency::Strong {
prepare_aggregation_data(&aggregation_context, &self.id);
}
let mut state = if strongly_consistent {
let mut aggregation = aggregation_data(&aggregation_context, &self.id);
if aggregation.unfinished > 0 {
if aggregation.root_type.is_none() {
Expand Down
10 changes: 5 additions & 5 deletions turbopack/crates/turbo-tasks-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use turbo_tasks::{
registry,
test_helpers::with_turbo_tasks_for_testing,
util::{SharedError, StaticOrArc},
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, TaskId, TaskPersistence, TraitTypeId,
TurboTasksApi, TurboTasksCallApi,
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, ReadConsistency, TaskId,
TaskPersistence, TraitTypeId, TurboTasksApi, TurboTasksCallApi,
};

pub use crate::run::{run, run_without_cache_check, Registration};
Expand Down Expand Up @@ -184,7 +184,7 @@ impl TurboTasksApi for VcStorage {
fn try_read_task_output(
&self,
id: TaskId,
_strongly_consistent: bool,
_consistency: ReadConsistency,
) -> Result<Result<RawVc, EventListener>> {
let tasks = self.tasks.lock().unwrap();
let i = *id - 1;
Expand All @@ -201,9 +201,9 @@ impl TurboTasksApi for VcStorage {
fn try_read_task_output_untracked(
&self,
task: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
) -> Result<Result<RawVc, EventListener>> {
self.try_read_task_output(task, strongly_consistent)
self.try_read_task_output(task, consistency)
}

fn try_read_task_cell(
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use crate::id::{BackendJobId, ExecutionId};
use crate::{
event::EventListener,
magic_any::MagicAny,
manager::TurboTasksBackendApi,
manager::{ReadConsistency, TurboTasksBackendApi},
raw_vc::CellId,
registry,
task::shared_reference::TypedSharedReference,
Expand Down Expand Up @@ -484,7 +484,7 @@ pub trait Backend: Sync + Send {
&self,
task: TaskId,
reader: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> Result<Result<RawVc, EventListener>>;

Expand All @@ -493,7 +493,7 @@ pub trait Backend: Sync + Send {
fn try_read_task_output_untracked(
&self,
task: TaskId,
strongly_consistent: bool,
consistency: ReadConsistency,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> Result<Result<RawVc, EventListener>>;

Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub use magic_any::MagicAny;
pub use manager::{
dynamic_call, dynamic_this_call, emit, get_invalidator, mark_finished, mark_stateful,
prevent_gc, run_once, run_once_with_reason, spawn_blocking, spawn_thread, trait_call,
turbo_tasks, CurrentCellRef, Invalidator, TaskPersistence, TurboTasks, TurboTasksApi,
TurboTasksBackendApi, TurboTasksCallApi, Unused, UpdateInfo,
turbo_tasks, CurrentCellRef, Invalidator, ReadConsistency, TaskPersistence, TurboTasks,
TurboTasksApi, TurboTasksBackendApi, TurboTasksCallApi, Unused, UpdateInfo,
};
pub use native_function::{FunctionMeta, NativeFunction};
pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};
Expand Down
Loading

0 comments on commit cfba2dc

Please sign in to comment.