Skip to content

Commit

Permalink
feat(turbopack): Switch turbo-tasks-memory/print_task_invalidation
Browse files Browse the repository at this point in the history
…compile-time feature to an environment variable (vercel#72300)

With this PR, `turbo-tasks-memory` (the new backend doesn't support this yet) can print invalidation information (basically just function names) to stdout when run with the following environment variable:

```
NEXT_TURBOPACK_PRINT_TASK_INVALIDATION=1
```

![Screenshot 2024-11-04 at 5.05.10 PM.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/3d334ef6-184c-40df-9769-fd0373de00fb.png)

This was previously behind a compilation feature flag, however:

- We have to generate all the support code regardless of if the flag is set, so it doesn't seem like it would save us anything on binary size.
- The branch should be pretty cheap to check (and probably easy for the CPU to predict), so runtime performance when this flag isn't set should be negligible.
- It's a pretty big lift to ask users to try custom builds.

**Why?** The hope is that this can help debug cases where users get stuck in a fast refresh update loop: https://vercel.slack.com/archives/C03S8ED1DKM/p1730737898652749

This is similar to vercel#72300, except that prints the *reason*, this prints the *tasks* that were actually invalidated. Printing the tasks works even if no reason was supplied, so it can probably be helpful in debugging those cases.

Closes PACK-3375
  • Loading branch information
bgw authored Nov 5, 2024
1 parent dbad89b commit f5f1581
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
10 changes: 6 additions & 4 deletions crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::HashMap, future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration,
collections::HashMap, env, future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration,
};

use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -133,9 +133,11 @@ pub fn create_turbo_tasks(
)?),
))
} else {
NextTurboTasks::Memory(TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(
memory_limit,
)))
let mut backend = turbo_tasks_memory::MemoryBackend::new(memory_limit);
if env::var_os("NEXT_TURBOPACK_PRINT_TASK_INVALIDATION").is_some() {
backend.print_task_invalidation(true);
}
NextTurboTasks::Memory(TurboTasks::new(backend))
})
}

Expand Down
1 change: 0 additions & 1 deletion turbopack/crates/turbo-tasks-memory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ turbo-tasks-build = { workspace = true }

[features]
track_unfinished = []
print_task_invalidation = []
default = []

[[bench]]
Expand Down
16 changes: 13 additions & 3 deletions turbopack/crates/turbo-tasks-memory/src/memory_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct MemoryBackend {
gc_queue: Option<GcQueue>,
idle_gc_active: AtomicBool,
task_statistics: TaskStatisticsApi,
pub(crate) print_task_invalidation: bool,
}

impl Default for MemoryBackend {
Expand All @@ -71,7 +72,7 @@ impl Default for MemoryBackend {
}

impl MemoryBackend {
pub fn new(memory_limit: usize) -> Self {
pub fn new(memory_limit_bytes: usize) -> Self {
let shard_amount =
(std::thread::available_parallelism().map_or(1, usize::from) * 32).next_power_of_two();
Self {
Expand All @@ -84,13 +85,22 @@ impl MemoryBackend {
Default::default(),
shard_amount,
),
memory_limit: AtomicUsize::new(memory_limit),
gc_queue: (memory_limit != usize::MAX).then(GcQueue::new),
memory_limit: AtomicUsize::new(memory_limit_bytes),
gc_queue: (memory_limit_bytes != usize::MAX).then(GcQueue::new),
idle_gc_active: AtomicBool::new(false),
task_statistics: TaskStatisticsApi::default(),
print_task_invalidation: false,
}
}

/// A debug feature that prints detailed task invalidation information to stdout if enabled.
///
/// To enable this in next.js, use the `NEXT_TURBOPACK_PRINT_TASK_INVALIDATION` environment
/// variable.
pub fn print_task_invalidation(&mut self, value: bool) {
self.print_task_invalidation = value;
}

fn connect_task_child(
&self,
parent: TaskId,
Expand Down
6 changes: 2 additions & 4 deletions turbopack/crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,7 @@ impl Task {
InProgress(..) => match result {
Ok(Ok(result)) => {
if state.output != result {
if cfg!(feature = "print_task_invalidation")
&& state.output.content.is_some()
{
if backend.print_task_invalidation && state.output.content.is_some() {
println!(
"Task {{ id: {}, name: {} }} invalidates:",
*self.id, self.ty
Expand Down Expand Up @@ -1146,7 +1144,7 @@ impl Task {
drop(state);
change_job.apply(&aggregation_context);

if cfg!(feature = "print_task_invalidation") {
if backend.print_task_invalidation {
println!("invalidated Task {{ id: {}, name: {} }}", *self.id, self.ty);
}
turbo_tasks.schedule(self.id);
Expand Down

0 comments on commit f5f1581

Please sign in to comment.