Unverified Commit 37f90d15 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Replace VcStorage with real TurboTasks in tests/benches

 (#93955)

`VcStorage` is a partial in-memory implementor of `TurboTasksApi`/`TurboTasksCallApi` in `turbo-tasks-testing`. It gave a handful of tests/benchmarks a way to allocate cells and call functions without spinning up a real backend, at the cost of stubbing roughly half of the trait surface with `unreachable!()`/`unimplemented!()`.

This PR migrates the four call sites to construct a real `TurboTasks` over a `noop_backing_storage()`, then deletes `VcStorage` and the test-only `with_turbo_tasks_for_testing`/`current_task_for_testing` helpers it was the sole consumer of. After this PR, `TurboTasks<B>` is the only concrete implementor of `dyn TurboTasksApi`.

### Migrated call sites

- **`turbo-tasks-fs` `with_extension` and `file_stem` tests** — direct `tt.run_once(async move { … })` replacement, matching the existing `test_try_from_sys_path` test in the same file.
- **`turbopack-ecmascript` `analyzer::tests::fixture`** — the body is now in a `#[turbo_tasks::function(operation, root)] async fn fixture_op(input: RcStr)` invoked via `fixture_op(input).read_strongly_consistent().await`. The synchronous SWC parser+resolver setup is wrapped in `GLOBALS.set(&globals, || …)` returning owned `(eval_context, var_graph)`, after which the async resolve/link work proceeds without GLOBALS — mirroring the production pattern in `analyze_ecmascript_module_internal`. `run_test` is no longer needed.
- **`turbopack-ecmascript` `bench_link`** — restructured to mirror `turbo-tasks-backend/benches/overhead.rs`: `iter_custom` with `TurboTasks` constructed per measurement batch (`storage_mode: None`, `dependency_tracking: false`), `Instant::now()` placed after construction so it isn't timed. `BenchInput.var_graph` is now `Arc<VarGraph>` so the future can be `Send + 'static`. Quick run shows no change in performance for `link/typeof`.

### Deleted

- `VcStorage` and its trait impls (`turbo-tasks-testing/src/lib.rs` shrinks from 339 lines to 9).
- `with_turbo_tasks_for_testing`, `current_task_for_testing`, and the `test_helpers` re-export module — all unused after `VcStorage` is gone.

### Verification

- `cargo check --workspace --tests --benches` — clean.
- `cargo clippy` on changed crates — clean.
- `cargo test -p turbo-tasks-fs --lib` — 110/110 pass.
- `cargo test -p turbopack-ecmascript --lib` — 352/352 pass (including all 60 analyzer fixture tests).
- `cargo bench -p turbopack-ecmascript --bench analyzer -- "link/typeof" --quick` — runs; no change in performance detected.

<!-- NEXT_JS_LLM_PR -->
parent 0846789f
......@@ -2968,7 +2968,11 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn with_extension() {
turbo_tasks_testing::VcStorage::with(async {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async move {
let fs = Vc::upcast::<Box<dyn FileSystem>>(VirtualFileSystem::new())
.to_resolved()
.await?;
......@@ -3003,7 +3007,11 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn file_stem() {
turbo_tasks_testing::VcStorage::with(async {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async move {
let fs = Vc::upcast::<Box<dyn FileSystem>>(VirtualFileSystem::new())
.to_resolved()
.await?;
......
......@@ -3,336 +3,7 @@
pub mod retry;
mod run;
use std::{
future::Future,
mem::replace,
panic::AssertUnwindSafe,
pin::Pin,
sync::{Arc, Mutex, Weak},
};
use anyhow::{Result, anyhow, bail};
use futures::FutureExt;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use tokio::sync::mpsc::Receiver;
use turbo_tasks::{
CellId, DynTaskInputs, ExecutionId, InvalidationReason, LocalTaskId, RawVc, ReadCellOptions,
ReadOutputOptions, StackDynTaskInputs, TaskId, TaskPersistence, TraitTypeId, TurboTasksApi,
TurboTasksCallApi,
backend::{CellContent, TaskCollectiblesMap, TypedCellContent, VerificationMode},
event::{Event, EventListener},
message_queue::CompilationEvent,
test_helpers::with_turbo_tasks_for_testing,
util::{SharedError, StaticOrArc},
};
pub use crate::run::{
Registration, TestInstance, run, run_once, run_once_without_cache_check, run_with_tt,
run_without_cache_check, test_instance,
};
enum Task {
Spawned(Event),
Finished(Result<RawVc, SharedError>),
}
#[derive(Default)]
pub struct VcStorage {
this: Weak<Self>,
cells: Mutex<FxHashMap<(TaskId, CellId), CellContent>>,
tasks: Mutex<Vec<Task>>,
}
impl VcStorage {
fn dynamic_call(
&self,
func: &'static turbo_tasks::macro_helpers::NativeFunction,
this_arg: Option<RawVc>,
arg: Box<dyn DynTaskInputs>,
) -> RawVc {
let this = self.this.upgrade().unwrap();
let handle = tokio::runtime::Handle::current();
let future = func.execute(this_arg, &*arg);
let i = {
let mut tasks = self.tasks.lock().unwrap();
let i = tasks.len();
tasks.push(Task::Spawned(Event::new(move || {
move || format!("Task({i})::event")
})));
i
};
let task_id = TaskId::try_from(u32::try_from(i + 1).unwrap()).unwrap();
let execution_id = ExecutionId::try_from(u16::try_from(i + 1).unwrap()).unwrap();
handle.spawn(with_turbo_tasks_for_testing(
this.clone(),
task_id,
execution_id,
async move {
let result = AssertUnwindSafe(future).catch_unwind().await;
// Convert the unwind panic to an anyhow error that can be cloned.
let result = match result {
Ok(Ok(raw_vc)) => Ok(raw_vc.to_non_local().await),
_ => result,
};
let result = result
.map_err(|any| match any.downcast::<String>() {
Ok(owned) => anyhow!(owned),
Err(any) => match any.downcast::<&'static str>() {
Ok(str) => anyhow!(str),
Err(_) => anyhow!("unknown panic"),
},
})
.and_then(|r| r)
.map_err(SharedError::new);
let mut tasks = this.tasks.lock().unwrap();
if let Task::Spawned(event) = replace(&mut tasks[i], Task::Finished(result)) {
event.notify(usize::MAX);
}
},
));
RawVc::TaskOutput(task_id)
}
}
impl TurboTasksCallApi for VcStorage {
fn dynamic_call(
&self,
func: &'static turbo_tasks::macro_helpers::NativeFunction,
this: Option<RawVc>,
arg: &mut dyn StackDynTaskInputs,
_persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, this, arg.take_box())
}
fn native_call(
&self,
func: &'static turbo_tasks::macro_helpers::NativeFunction,
this: Option<RawVc>,
arg: &mut dyn StackDynTaskInputs,
_persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, this, arg.take_box())
}
fn trait_call(
&self,
_trait_type: &'static turbo_tasks::TraitMethod,
_this: RawVc,
_arg: &mut dyn StackDynTaskInputs,
_persistence: TaskPersistence,
) -> RawVc {
unreachable!()
}
fn run(
&self,
_future: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>,
) -> Pin<
Box<dyn Future<Output = Result<(), turbo_tasks::backend::TurboTasksExecutionError>> + Send>,
> {
unreachable!()
}
fn run_once(
&self,
_future: std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>,
) -> Pin<
Box<dyn futures::Future<Output = Result<(), anyhow::Error>> + std::marker::Send + 'static>,
> {
unreachable!()
}
fn run_once_with_reason(
&self,
_reason: StaticOrArc<dyn InvalidationReason>,
_future: std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>,
) -> Pin<
Box<dyn futures::Future<Output = Result<(), anyhow::Error>> + std::marker::Send + 'static>,
> {
unreachable!()
}
fn start_once_process(
&self,
_future: std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
) {
unreachable!()
}
/// Should not be called on the testing VcStorage. These methods are only implemented for
/// structs with access to a `MessageQueue` like `TurboTasks`.
fn send_compilation_event(&self, _event: Arc<dyn CompilationEvent>) {
unimplemented!()
}
fn get_task_name(&self, task: TaskId) -> String {
format!("Task({})", task)
}
}
impl TurboTasksApi for VcStorage {
fn invalidate(&self, _task: TaskId) {
unreachable!()
}
fn invalidate_with_reason(
&self,
_task: TaskId,
_reason: turbo_tasks::util::StaticOrArc<dyn turbo_tasks::InvalidationReason>,
) {
unreachable!()
}
fn invalidate_serialization(&self, _task: TaskId) {
// ignore
}
fn try_read_task_output(
&self,
id: TaskId,
_options: ReadOutputOptions,
) -> Result<Result<RawVc, EventListener>> {
let tasks = self.tasks.lock().unwrap();
let i = *id - 1;
let task = tasks.get(i as usize).unwrap();
match task {
Task::Spawned(event) => Ok(Err(event.listen())),
Task::Finished(result) => match result {
Ok(vc) => Ok(Ok(*vc)),
Err(err) => bail!(err.clone()),
},
}
}
fn try_read_task_cell(
&self,
task: TaskId,
index: CellId,
_options: ReadCellOptions,
) -> Result<Result<TypedCellContent, EventListener>> {
let map = self.cells.lock().unwrap();
Ok(Ok(if let Some(cell) = map.get(&(task, index)) {
cell.clone()
} else {
Default::default()
}
.into_typed(index.type_id)))
}
fn try_read_own_task_cell(
&self,
current_task: TaskId,
index: CellId,
) -> Result<TypedCellContent> {
self.read_own_task_cell(current_task, index)
}
fn try_read_local_output(
&self,
_execution_id: ExecutionId,
_local_task_id: LocalTaskId,
) -> Result<Result<RawVc, EventListener>> {
unimplemented!()
}
fn emit_collectible(&self, _trait_type: turbo_tasks::TraitTypeId, _collectible: RawVc) {
unimplemented!()
}
fn unemit_collectible(
&self,
_trait_type: turbo_tasks::TraitTypeId,
_collectible: RawVc,
_count: u32,
) {
unimplemented!()
}
fn unemit_collectibles(
&self,
_trait_type: turbo_tasks::TraitTypeId,
_collectibles: &TaskCollectiblesMap,
) {
unimplemented!()
}
fn read_task_collectibles(&self, _task: TaskId, _trait_id: TraitTypeId) -> TaskCollectiblesMap {
unimplemented!()
}
fn read_own_task_cell(&self, task: TaskId, index: CellId) -> Result<TypedCellContent> {
let map = self.cells.lock().unwrap();
Ok(if let Some(cell) = map.get(&(task, index)) {
cell.to_owned()
} else {
Default::default()
}
.into_typed(index.type_id))
}
fn update_own_task_cell(
&self,
task: TaskId,
index: CellId,
content: CellContent,
_updated_key_hashes: Option<SmallVec<[u64; 2]>>,
_content_hash: Option<[u8; 16]>,
_verification_mode: VerificationMode,
) {
let mut map = self.cells.lock().unwrap();
let cell = map.entry((task, index)).or_default();
*cell = content;
}
fn connect_task(&self, _task: TaskId) {
// no-op
}
fn mark_own_task_as_finished(&self, _task: TaskId) {
// no-op
}
fn spawn_detached_for_testing(
&self,
_f: std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
) {
unimplemented!()
}
fn task_statistics(&self) -> &turbo_tasks::task_statistics::TaskStatisticsApi {
unimplemented!()
}
fn stop_and_wait(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
Box::pin(async {})
}
/// Should not be called on the testing VcStorage. These methods are only implemented for
/// structs with access to a `MessageQueue` like `TurboTasks`.
fn subscribe_to_compilation_events(
&self,
_event_types: Option<Vec<String>>,
) -> Receiver<Arc<dyn CompilationEvent>> {
unimplemented!()
}
fn is_tracking_dependencies(&self) -> bool {
false
}
}
impl VcStorage {
pub fn with<T>(f: impl Future<Output = T>) -> impl Future<Output = T> {
with_turbo_tasks_for_testing(
Arc::new_cyclic(|weak| VcStorage {
this: weak.clone(),
..Default::default()
}),
TaskId::MAX,
ExecutionId::MIN,
f,
)
}
}
......@@ -402,7 +402,3 @@ pub use turbo_tasks_macros::task_storage;
pub use turbo_tasks_macros::TaskInput;
pub type TaskIdSet = AutoSet<TaskId, BuildHasherDefault<FxHasher>, 2>;
pub mod test_helpers {
pub use super::manager::{current_task_for_testing, with_turbo_tasks_for_testing};
}
......@@ -1859,26 +1859,6 @@ pub fn turbo_tasks_future_scope<T>(
TURBO_TASKS.scope(tt, f)
}
pub fn with_turbo_tasks_for_testing<T>(
tt: Arc<dyn TurboTasksApi>,
current_task: TaskId,
execution_id: ExecutionId,
f: impl Future<Output = T>,
) -> impl Future<Output = T> {
TURBO_TASKS.scope(
tt,
CURRENT_TASK_STATE.scope(
Arc::new(RwLock::new(CurrentTaskState::new(
current_task,
execution_id,
TaskPriority::initial(),
false, // in_top_level_task
))),
f,
),
)
}
/// Spawns the given future within the context of the current task.
///
/// Beware: this method is not safe to use in production code. It is only
......@@ -1887,10 +1867,6 @@ pub fn spawn_detached_for_testing(f: impl Future<Output = ()> + Send + 'static)
turbo_tasks().spawn_detached_for_testing(Box::pin(f));
}
pub fn current_task_for_testing() -> Option<TaskId> {
CURRENT_TASK_STATE.with(|ts| ts.read().unwrap().task_id)
}
/// Marks the current task as finished. This excludes it from waiting for
/// strongly consistency.
pub fn mark_finished() {
......
use std::{fs, path::PathBuf, sync::Arc, time::Duration};
use std::{
fs,
path::PathBuf,
sync::Arc,
time::{Duration, Instant},
};
use criterion::{Bencher, BenchmarkId, Criterion, criterion_group, criterion_main};
use swc_core::{
......@@ -10,8 +15,8 @@ use swc_core::{
visit::VisitMutWith,
},
};
use turbo_tasks::ResolvedVc;
use turbo_tasks_testing::VcStorage;
use turbo_tasks::{ResolvedVc, TurboTasks};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbopack_core::{
compile_time_info::CompileTimeInfo,
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion},
......@@ -67,12 +72,12 @@ pub fn benchmark(c: &mut Criterion) {
Default::default(),
None,
);
let var_graph = create_graph(
let var_graph = Arc::new(create_graph(
&program,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
true,
);
));
let input = BenchInput {
program,
......@@ -94,7 +99,7 @@ pub fn benchmark(c: &mut Criterion) {
struct BenchInput {
program: Program,
eval_context: EvalContext,
var_graph: VarGraph,
var_graph: Arc<VarGraph>,
}
fn bench_create_graph(b: &mut Bencher, input: &BenchInput) {
......@@ -112,11 +117,20 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
let var_graph = input.var_graph.clone();
b.to_async(rt).iter(|| async {
let var_cache = Default::default();
for value in input.var_graph.values.values() {
VcStorage::with(async {
b.to_async(rt).iter_custom(move |iters| {
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
dependency_tracking: false,
..Default::default()
},
noop_backing_storage(),
));
let var_graph = var_graph.clone();
async move {
tt.run_once(async move {
let compile_time_info = CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
......@@ -131,18 +145,25 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
)
.cell()
.await?;
link(
&input.var_graph,
value.clone(),
&early_visitor,
&(|val| visitor(val, compile_time_info, ImportAttributes::empty_ref())),
&Default::default(),
&var_cache,
)
.await
let start = Instant::now();
for _ in 0..iters {
let var_cache = Default::default();
for value in var_graph.values.values() {
link(
&var_graph,
value.clone(),
&early_visitor,
&(|val| visitor(val, compile_time_info, ImportAttributes::empty_ref())),
&Default::default(),
&var_cache,
)
.await?;
}
}
anyhow::Ok(start.elapsed())
})
.await
.unwrap();
.unwrap()
}
});
}
......
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