Turbopack: add on-demand memory reporting for dev server#90271
Turbopack: add on-demand memory reporting for dev server#90271lukesandberg wants to merge 7 commits intocanaryfrom
Conversation
c488246 to
f0f0230
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
Failing test suitesCommit: 7f47044 | About building and testing Next.js
Expand output● turbopack-memory-report › fetch API › should return valid JSON from /__nextjs_turbopack-memory ● turbopack-memory-report › fetch API › should return markdown format when requested ● turbopack-memory-report › CLI › should return valid JSON via next internal turbopack-memory ● turbopack-memory-report › CLI › should return markdown via CLI |
turbopack/crates/turbo-tasks-backend/src/backend/memory_report.rs
Outdated
Show resolved
Hide resolved
turbopack/crates/turbo-tasks-backend/src/backend/memory_report.rs
Outdated
Show resolved
Hide resolved
turbopack/crates/turbo-tasks-backend/src/backend/memory_report.rs
Outdated
Show resolved
Hide resolved
test/development/app-dir/turbopack-memory-report/turbopack-memory-report.test.ts
Outdated
Show resolved
Hide resolved
turbopack/crates/turbo-tasks-backend/src/backend/memory_report.rs
Outdated
Show resolved
Hide resolved
turbopack/crates/turbo-tasks-backend/src/backend/memory_report.rs
Outdated
Show resolved
Hide resolved
turbopack/crates/turbo-tasks-backend/src/backend/storage_schema.rs
Outdated
Show resolved
Hide resolved
Stats from current PR🔴 1 regression
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles: **399 kB** → **399 kB** ✅ -5 B80 files with content-based hashes (individual files not comparable between builds) Server Middleware
Build DetailsBuild Manifests
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
|
| return next() | ||
| } | ||
|
|
||
| const format = searchParams.get('format') ?? 'json' |
There was a problem hiding this comment.
Alternative options:
- This could be done with an
Acceptheader. It doesn't really matter because we're not asking the agent to call this endpoint directly, but agents usually have a WebFetch tool that uses theAcceptheader to request markdown. - We could have a
.jsonpath and a.mdpath

Summary
Adds an on-demand memory reporting system for Turbopack dev servers, enabling developers to inspect memory usage without restarting or attaching external profilers. This implements the design described in PACK-6917.
Layers
Rust core (
turbo-tasks-backend):collect_memory_report()iterates all in-memory tasks and cells, grouping by function name and value type. Size estimation uses bincode encode-and-discard with a reusable scratch buffer. Allocator stats come fromTurboMalloc::memory_usage().NAPI binding:
Project.getMemoryReport(topN?)exposes the Rust report as a JSON string to Node.js.HTTP endpoint:
GET /__nextjs_turbopack-memoryon the dev server. Supportsformat=json|markdownandtop_nquery parameters. The Node.js layer augments the report with process-level stats (RSS, heap, PID, Node version).CLI command:
next internal turbopack-memory [dir] [--format json|markdown] [--server host:por]discovers the running dev server via the.next/dev/lockfile and fetches the report.Documentation: Added a "Turbopack memory reporting" section to the local development guide.
Output formats
Key design decisions
We estimate memory size via encoding, which is trivial to do but horrifically inaccurate. This means we both overcount and undercount memory (because persistent formats are sometimes often compact than memory, think slack space in hash tables) and overcount because we don't respect reference counted datastructures like
RcStr/Rope. A 'correct' approach would be very complex.Test plan
next internal turbopack-memorywith JSON, markdown, top_n)Fixes PACK-6917