next.js
175fd422 - [turbopack] Shrink ValueToStringify futures by dropping async-fn envelopes (#94215)

Commit
2 hours ago
[turbopack] Shrink ValueToStringify futures by dropping async-fn envelopes (#94215) ### What? Shrinks a few hot Turbopack futures by removing redundant async state-machine envelopes and oversized captured values. No behavior change. ### Why? Turbo-tasks boxes every task's future, so a future's size is a heap allocation paid on each task execution. The changes target: - `EcmascriptModuleContent::new`, which runs once per module and has the largest affected future; - the per-task-execution future of the turbo-tasks executor. ### How? 1. **`ValueToStringify` (`turbo-tasks/src/display.rs`)**: the `to_stringify` impls used by `turbofmt!` / `turbobail!` wrapped their inner future in an `async move { … .await? … }` whose only job was mapping the output. That generated a coroutine holding the inner future twice (as an upvar and as the awaitee). These impls now use `std::future::ready(..)`, `.map_ok(..)` and `.map(..)` directly. 2. **`with_consumed_parse_result` (`turbopack-ecmascript/src/lib.rs`)**: the success closure took `Either<EvalContext, &EvalContext>` by value. That `Either` is sized to `EvalContext` and is held across every suspend point. It now takes a reference. 3. **Same closure / `CodeGenResultComments::Single`**: `Either<ImmutableComments, Arc<ImmutableComments>>` becomes `Arc<ImmutableComments>`. Consumers only ever borrow the comments, so the parsed `Arc` is moved through as-is. 4. **Moving `exports_ids` out without a clone**: the closure receives `&mut ParsedEvalContext`, one pointer. - On the uniquely-owned path it is `Owned(&mut EvalContext)`, borrowing the context in place inside the unwrapped `ParseResult`. There `imports.exports_ids` is moved out with `take`, as before (2). - On the shared path it is `Shared(&EvalContext)`, and the map is cloned, as before. - Borrowing in place also removes a separate `EvalContext` local that was held across awaits. 5. **`abort_on_panic` (`turbo-tasks/src/manager.rs`)**: it wraps every task execution. As an `async fn`, it held the wrapped future twice (as the argument and inside the awaited `CatchUnwind`). It now returns `AssertUnwindSafe(f).catch_unwind().map(..)` directly. The abort path and its diagnostics are unchanged. ### Measurements Setup: x86_64 Linux, `nightly-2026-11-13` (rustc `8eb778d04`). Baseline is canary `93acd74b9`; the candidate source tree is identical to this PR's head. Both are `cargo build --release -p turbopack-cli` with the workspace release profile (thin LTO) and `-Zprint-type-sizes` added to the workspace rustflags. A repeated baseline build was byte-identical. **Binary size** | | canary | this PR | Δ | |---|---:|---:|---:| | `turbopack-cli` (stripped) | 63,960,136 B | 63,942,472 B | **−17,664 B** | | `.text` | 44,384,352 B | 44,371,232 B | −13,120 B | | `.eh_frame` | 5,040,520 B | 5,039,496 B | −1,024 B | **Future layouts** (`-Zprint-type-sizes`) - Among async state machines whose printed name appears in both builds (4473 names), 370 shrink and none grow. Names present in only one build (177 canary-only, 4 here) are not compared. - These are requested `Box` layout sizes; actual bytes after allocator size-class rounding can differ. | Future | canary | this PR | Δ | |---|---:|---:|---:| | `EcmascriptModuleContent::new` task body | 4848 B | 3776 B | −1072 B | | `EcmascriptModuleContent::new_without_analysis` task body | 4808 B | 3736 B | −1072 B | | `process_parse_result()` | 4768 B | 3696 B | −1072 B | | Task executor future (`TurboTasksExecutor::execute`, scheduled task) | 608 B | 320 B | −288 B | | Task executor future (`TurboTasksExecutor::execute`, local task) | 648 B | 384 B | −264 B | | `AssetIdent::to_string` task body | 448 B | 280 B | −168 B | | `DiskFileSystem::write` task body | 472 B | 304 B | −168 B | <!-- NEXT_JS_LLM --> <!-- fleet 06149d4e-3f62-4bd7-95d6-51256662fda3 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Author
Parents
Loading