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()
}
});
}
......
......@@ -3967,23 +3967,26 @@ pub mod test_utils {
#[cfg(test)]
mod tests {
use std::{mem::take, path::PathBuf, time::Instant};
use std::{mem::take, path::PathBuf, sync::Arc, time::Instant};
use parking_lot::Mutex;
use rstest::rstest;
use rustc_hash::FxHashMap;
use swc_core::{
common::{Mark, comments::SingleThreadedComments},
common::{
FilePathMapping, GLOBALS, Globals, Mark, SourceMap, comments::SingleThreadedComments,
},
ecma::{
ast::{EsVersion, Id},
parser::parse_file_as_program,
transforms::base::resolver,
visit::VisitMutWith,
},
testing::{NormalizedOutput, fixture, run_test},
testing::{NormalizedOutput, fixture},
};
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, util::FormatDuration};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TurboTasks, util::FormatDuration};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbopack_core::{
compile_time_info::CompileTimeInfo,
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion},
......@@ -4002,6 +4005,29 @@ mod tests {
#[fixture("tests/analyzer/graph/**/input.js")]
fn fixture(input: PathBuf) {
let input = RcStr::from(input.to_str().unwrap());
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
rt.block_on(async move {
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async move {
fixture_op(input).read_strongly_consistent().await?;
anyhow::Ok(())
})
.await
.unwrap();
});
}
#[turbo_tasks::function(operation, root)]
async fn fixture_op(input: RcStr) -> anyhow::Result<()> {
let input = PathBuf::from(input.as_str());
let graph_snapshot_path = input.with_file_name("graph.snapshot");
let graph_explained_snapshot_path = input.with_file_name("graph-explained.snapshot");
let graph_effects_snapshot_path = input.with_file_name("graph-effects.snapshot");
......@@ -4009,409 +4035,361 @@ mod tests {
let resolved_effects_snapshot_path = input.with_file_name("resolved-effects.snapshot");
let large_marker = input.with_file_name("large");
run_test(false, |cm, handler| {
let r = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
r.block_on(async move {
let fm = cm.load_file(&input).unwrap();
let comments = SingleThreadedComments::default();
let mut m = parse_file_as_program(
&fm,
Default::default(),
EsVersion::latest(),
Some(&comments),
&mut vec![],
)
.map_err(|err| err.into_diagnostic(handler).emit())?;
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
let eval_context = EvalContext::new(
Some(&m),
unresolved_mark,
top_level_mark,
Default::default(),
Some(&comments),
);
let cm: Arc<SourceMap> = Arc::new(SourceMap::new(FilePathMapping::empty()));
let globals = Arc::new(Globals::new());
// Keep all non-`Send` SWC types (`SingleThreadedComments`, `Lrc<SourceFile>`)
// confined to this synchronous block so they don't have to cross an `.await`
// and break the `Send` bound on `tt.run_once`'s future.
let (eval_context, mut var_graph) = GLOBALS.set(&globals, || {
let fm = cm.load_file(&input).unwrap();
let comments = SingleThreadedComments::default();
let mut m = parse_file_as_program(
&fm,
Default::default(),
EsVersion::latest(),
Some(&comments),
&mut vec![],
)
.map_err(|err| anyhow::anyhow!("parse error: {err:?}"))?;
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
let eval_context = EvalContext::new(
Some(&m),
unresolved_mark,
top_level_mark,
Default::default(),
Some(&comments),
);
let mut var_graph = create_graph(
&m,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
true,
);
let var_cache = Default::default();
let mut named_values = var_graph
.values
.clone()
.into_iter()
.map(|((id, ctx), value)| {
let unique = var_graph.values.keys().filter(|(i, _)| &id == i).count() == 1;
if unique {
(id.to_string(), ((id, ctx), value))
} else {
(format!("{id}{ctx:?}"), ((id, ctx), value))
}
})
.collect::<Vec<_>>();
named_values.sort_by(|a, b| a.0.cmp(&b.0));
fn explain_all<'a>(
values: impl IntoIterator<
Item = (&'a String, &'a JsValue, Option<AssignmentScopes>),
>,
) -> String {
values
.into_iter()
.map(|(id, value, assignment_scopes)| {
let non_root_assignments = match assignment_scopes {
Some(AssignmentScopes::AllInModuleEvalScope) => {
" (const after eval)"
}
_ => "",
};
let (explainer, hints) = value.explain(10, 5);
format!("{id}{non_root_assignments} = {explainer}{hints}")
})
.collect::<Vec<_>>()
.join("\n\n")
let var_graph = create_graph(
&m,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
true,
);
anyhow::Ok((eval_context, var_graph))
})?;
let var_cache = Default::default();
let mut named_values = var_graph
.values
.clone()
.into_iter()
.map(|((id, ctx), value)| {
let unique = var_graph.values.keys().filter(|(i, _)| &id == i).count() == 1;
if unique {
(id.to_string(), ((id, ctx), value))
} else {
(format!("{id}{ctx:?}"), ((id, ctx), value))
}
})
.collect::<Vec<_>>();
named_values.sort_by(|a, b| a.0.cmp(&b.0));
fn explain_all<'a>(
values: impl IntoIterator<Item = (&'a String, &'a JsValue, Option<AssignmentScopes>)>,
) -> String {
values
.into_iter()
.map(|(id, value, assignment_scopes)| {
let non_root_assignments = match assignment_scopes {
Some(AssignmentScopes::AllInModuleEvalScope) => " (const after eval)",
_ => "",
};
let (explainer, hints) = value.explain(10, 5);
format!("{id}{non_root_assignments} = {explainer}{hints}")
})
.collect::<Vec<_>>()
.join("\n\n")
}
{
// Dump snapshot of graph
{
// Dump snapshot of graph
let large = large_marker.exists();
let large = large_marker.exists();
if !large {
NormalizedOutput::from(format!(
"{:#?}",
named_values
.iter()
.map(|(name, (_, value))| (name, value))
.collect::<Vec<_>>()
))
.compare_to_file(&graph_snapshot_path)
.unwrap();
}
NormalizedOutput::from(explain_all(named_values.iter().map(
|(name, (id, value))| {
(
name,
value,
eval_context.imports.assignment_scopes.get(id).copied(),
)
},
)))
.compare_to_file(&graph_explained_snapshot_path)
if !large {
NormalizedOutput::from(format!(
"{:#?}",
named_values
.iter()
.map(|(name, (_, value))| (name, value))
.collect::<Vec<_>>()
))
.compare_to_file(&graph_snapshot_path)
.unwrap();
}
NormalizedOutput::from(explain_all(named_values.iter().map(
|(name, (id, value))| {
(
name,
value,
eval_context.imports.assignment_scopes.get(id).copied(),
)
},
)))
.compare_to_file(&graph_explained_snapshot_path)
.unwrap();
if !large {
NormalizedOutput::from(format!("{:#?}", var_graph.effects))
.compare_to_file(&graph_effects_snapshot_path)
.unwrap();
if !large {
NormalizedOutput::from(format!("{:#?}", var_graph.effects))
.compare_to_file(&graph_effects_snapshot_path)
.unwrap();
}
}
}
}
{
// Dump snapshot of resolved
let start = Instant::now();
let mut resolved = Vec::new();
for (name, (id, _)) in named_values.iter().cloned() {
let start = Instant::now();
// Ideally this would use eval_context.imports.get_attributes(span), but the
// span isn't available here
let (res, steps) = resolve(
&var_graph,
JsValue::Variable(id),
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking {} {name} took {} in {} steps",
input.display(),
FormatDuration(time),
steps
);
}
{
// Dump snapshot of resolved
let start = Instant::now();
let mut resolved = Vec::new();
for (name, (id, _)) in named_values.iter().cloned() {
let start = Instant::now();
// Ideally this would use eval_context.imports.get_attributes(span), but the
// span isn't available here
let (res, steps) = resolve(
&var_graph,
JsValue::Variable(id),
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking {} {name} took {} in {} steps",
input.display(),
FormatDuration(time),
steps
);
}
resolved.push((name, res));
}
let time = start.elapsed();
if time.as_millis() > 1 {
println!("linking {} took {}", input.display(), FormatDuration(time));
}
resolved.push((name, res));
}
let time = start.elapsed();
if time.as_millis() > 1 {
println!("linking {} took {}", input.display(), FormatDuration(time));
}
let start = Instant::now();
let explainer =
explain_all(resolved.iter().map(|(name, value)| (name, value, None)));
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"explaining {} took {}",
input.display(),
FormatDuration(time)
);
}
let start = Instant::now();
let explainer = explain_all(resolved.iter().map(|(name, value)| (name, value, None)));
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"explaining {} took {}",
input.display(),
FormatDuration(time)
);
}
NormalizedOutput::from(explainer)
.compare_to_file(&resolved_explained_snapshot_path)
.unwrap();
}
NormalizedOutput::from(explainer)
.compare_to_file(&resolved_explained_snapshot_path)
.unwrap();
}
{
// Dump snapshot of resolved effects
let start = Instant::now();
let mut resolved = Vec::new();
let mut queue = take(&mut var_graph.effects)
.into_iter()
.map(|effect| (0, effect))
.rev()
.collect::<Vec<_>>();
let mut i = 0;
while let Some((parent, effect)) = queue.pop() {
i += 1;
let start = Instant::now();
async fn handle_args(
args: Vec<EffectArg>,
queue: &mut Vec<(usize, Effect)>,
var_graph: &VarGraph,
var_cache: &Mutex<FxHashMap<Id, JsValue>>,
i: usize,
) -> Vec<JsValue> {
let mut new_args = Vec::with_capacity(args.len());
for arg in args {
match arg {
EffectArg::Value(v) => {
new_args.push(
resolve(
var_graph,
v,
ImportAttributes::empty_ref(),
var_cache,
)
.await
.0,
);
}
EffectArg::Closure(v, effects) => {
new_args.push(
resolve(
var_graph,
v,
ImportAttributes::empty_ref(),
var_cache,
)
.await
.0,
);
queue.extend(
effects.effects.into_iter().rev().map(|e| (i, e)),
);
}
EffectArg::Spread => {
new_args
.push(JsValue::unknown_empty(true, rcstr!("spread")));
}
}
{
// Dump snapshot of resolved effects
let start = Instant::now();
let mut resolved = Vec::new();
let mut queue = take(&mut var_graph.effects)
.into_iter()
.map(|effect| (0, effect))
.rev()
.collect::<Vec<_>>();
let mut i = 0;
while let Some((parent, effect)) = queue.pop() {
i += 1;
let start = Instant::now();
async fn handle_args(
args: Vec<EffectArg>,
queue: &mut Vec<(usize, Effect)>,
var_graph: &VarGraph,
var_cache: &Mutex<FxHashMap<Id, JsValue>>,
i: usize,
) -> Vec<JsValue> {
let mut new_args = Vec::with_capacity(args.len());
for arg in args {
match arg {
EffectArg::Value(v) => {
new_args.push(
resolve(var_graph, v, ImportAttributes::empty_ref(), var_cache)
.await
.0,
);
}
new_args
}
let steps = match effect {
Effect::Conditional {
condition, kind, ..
} => {
let (condition, steps) = resolve(
&var_graph,
*condition,
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
resolved.push((format!("{parent} -> {i} conditional"), condition));
match *kind {
ConditionalKind::If { then } => {
queue
.extend(then.effects.into_iter().rev().map(|e| (i, e)));
}
ConditionalKind::Else { r#else } => {
queue.extend(
r#else.effects.into_iter().rev().map(|e| (i, e)),
);
}
ConditionalKind::IfElse { then, r#else }
| ConditionalKind::Ternary { then, r#else } => {
queue.extend(
r#else.effects.into_iter().rev().map(|e| (i, e)),
);
queue
.extend(then.effects.into_iter().rev().map(|e| (i, e)));
}
ConditionalKind::IfElseMultiple { then, r#else } => {
for then in then {
queue.extend(
then.effects.into_iter().rev().map(|e| (i, e)),
);
}
for r#else in r#else {
queue.extend(
r#else.effects.into_iter().rev().map(|e| (i, e)),
);
}
}
ConditionalKind::And { expr }
| ConditionalKind::Or { expr }
| ConditionalKind::NullishCoalescing { expr }
| ConditionalKind::Labeled { body: expr } => {
queue
.extend(expr.effects.into_iter().rev().map(|e| (i, e)));
}
};
steps
EffectArg::Closure(v, effects) => {
new_args.push(
resolve(var_graph, v, ImportAttributes::empty_ref(), var_cache)
.await
.0,
);
queue.extend(effects.effects.into_iter().rev().map(|e| (i, e)));
}
Effect::Call {
func,
args,
new,
span,
..
} => {
let (func, steps) = resolve(
&var_graph,
*func,
eval_context.imports.get_attributes(span),
&var_cache,
)
.await;
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} call"),
if new {
JsValue::new_from_iter(func, new_args)
} else {
JsValue::call_from_iter(func, new_args)
},
));
steps
EffectArg::Spread => {
new_args.push(JsValue::unknown_empty(true, rcstr!("spread")));
}
Effect::FreeVar { var, .. } => {
resolved.push((
format!("{parent} -> {i} free var"),
JsValue::FreeVar(var),
));
0
}
}
new_args
}
let steps = match effect {
Effect::Conditional {
condition, kind, ..
} => {
let (condition, steps) = resolve(
&var_graph,
*condition,
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
resolved.push((format!("{parent} -> {i} conditional"), condition));
match *kind {
ConditionalKind::If { then } => {
queue.extend(then.effects.into_iter().rev().map(|e| (i, e)));
}
Effect::TypeOf { arg, .. } => {
let (arg, steps) = resolve(
&var_graph,
*arg,
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
resolved.push((
format!("{parent} -> {i} typeof"),
JsValue::type_of(Box::new(arg)),
));
steps
ConditionalKind::Else { r#else } => {
queue.extend(r#else.effects.into_iter().rev().map(|e| (i, e)));
}
Effect::MemberCall {
obj, prop, args, ..
} => {
let (obj, obj_steps) = resolve(
&var_graph,
*obj,
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
let (prop, prop_steps) = resolve(
&var_graph,
*prop,
ImportAttributes::empty_ref(),
&var_cache,
)
.await;
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} member call"),
JsValue::member_call_from_iter(obj, prop, new_args),
));
obj_steps + prop_steps
ConditionalKind::IfElse { then, r#else }
| ConditionalKind::Ternary { then, r#else } => {
queue.extend(r#else.effects.into_iter().rev().map(|e| (i, e)));
queue.extend(then.effects.into_iter().rev().map(|e| (i, e)));
}
Effect::DynamicImport { args, .. } => {
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} dynamic import"),
JsValue::call_from_iter(
JsValue::FreeVar("import".into()),
new_args,
),
));
0
ConditionalKind::IfElseMultiple { then, r#else } => {
for then in then {
queue.extend(then.effects.into_iter().rev().map(|e| (i, e)));
}
for r#else in r#else {
queue.extend(r#else.effects.into_iter().rev().map(|e| (i, e)));
}
}
Effect::Unreachable { .. } => {
resolved.push((
format!("{parent} -> {i} unreachable"),
JsValue::unknown_empty(true, rcstr!("unreachable")),
));
0
ConditionalKind::And { expr }
| ConditionalKind::Or { expr }
| ConditionalKind::NullishCoalescing { expr }
| ConditionalKind::Labeled { body: expr } => {
queue.extend(expr.effects.into_iter().rev().map(|e| (i, e)));
}
Effect::ImportMeta { .. }
| Effect::ImportedBinding { .. }
| Effect::Member { .. } => 0,
};
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking effect {} took {} in {} steps",
input.display(),
FormatDuration(time),
steps
);
}
steps
}
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking effects {} took {}",
input.display(),
FormatDuration(time)
);
Effect::Call {
func,
args,
new,
span,
..
} => {
let (func, steps) = resolve(
&var_graph,
*func,
eval_context.imports.get_attributes(span),
&var_cache,
)
.await;
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} call"),
if new {
JsValue::new_from_iter(func, new_args)
} else {
JsValue::call_from_iter(func, new_args)
},
));
steps
}
let start = Instant::now();
let explainer =
explain_all(resolved.iter().map(|(name, value)| (name, value, None)));
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"explaining effects {} took {}",
input.display(),
FormatDuration(time)
);
Effect::FreeVar { var, .. } => {
resolved.push((format!("{parent} -> {i} free var"), JsValue::FreeVar(var)));
0
}
NormalizedOutput::from(explainer)
.compare_to_file(&resolved_effects_snapshot_path)
.unwrap();
Effect::TypeOf { arg, .. } => {
let (arg, steps) =
resolve(&var_graph, *arg, ImportAttributes::empty_ref(), &var_cache)
.await;
resolved.push((
format!("{parent} -> {i} typeof"),
JsValue::type_of(Box::new(arg)),
));
steps
}
Effect::MemberCall {
obj, prop, args, ..
} => {
let (obj, obj_steps) =
resolve(&var_graph, *obj, ImportAttributes::empty_ref(), &var_cache)
.await;
let (prop, prop_steps) =
resolve(&var_graph, *prop, ImportAttributes::empty_ref(), &var_cache)
.await;
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} member call"),
JsValue::member_call_from_iter(obj, prop, new_args),
));
obj_steps + prop_steps
}
Effect::DynamicImport { args, .. } => {
let new_args =
handle_args(args, &mut queue, &var_graph, &var_cache, i).await;
resolved.push((
format!("{parent} -> {i} dynamic import"),
JsValue::call_from_iter(JsValue::FreeVar("import".into()), new_args),
));
0
}
Effect::Unreachable { .. } => {
resolved.push((
format!("{parent} -> {i} unreachable"),
JsValue::unknown_empty(true, rcstr!("unreachable")),
));
0
}
Effect::ImportMeta { .. }
| Effect::ImportedBinding { .. }
| Effect::Member { .. } => 0,
};
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking effect {} took {} in {} steps",
input.display(),
FormatDuration(time),
steps
);
}
}
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"linking effects {} took {}",
input.display(),
FormatDuration(time)
);
}
Ok(())
})
})
.unwrap();
let start = Instant::now();
let explainer = explain_all(resolved.iter().map(|(name, value)| (name, value, None)));
let time = start.elapsed();
if time.as_millis() > 1 {
println!(
"explaining effects {} took {}",
input.display(),
FormatDuration(time)
);
}
NormalizedOutput::from(explainer)
.compare_to_file(&resolved_effects_snapshot_path)
.unwrap();
}
Ok(())
}
async fn resolve(
......@@ -4420,7 +4398,9 @@ mod tests {
attributes: &ImportAttributes,
var_cache: &Mutex<FxHashMap<Id, JsValue>>,
) -> (JsValue, u32) {
turbo_tasks_testing::VcStorage::with(async {
// The caller (`fixture`) runs us inside `tt.run_once`, so a real
// turbo-tasks task context is already established here.
async {
let compile_time_info = CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
......@@ -4456,7 +4436,7 @@ mod tests {
var_cache,
)
.await
})
}
.await
.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