Skip to content

Commit 236a76d

Browse files
authored
[turbopack] Remove turbo_tasks::function from ModuleReference getters (vercel#91229)
### What? Refactors the `ModuleReference` trait to make `chunking_type()` and `binding_usage()` methods return direct values instead of `Vc<T>` wrapped values, removing the need for async task functions. Also removes the `get_referenced_asset` task from `EsmAssetReference`, inlining its logic into the callers. ### Why? This change simplifies the API by eliminating unnecessary async overhead for methods that typically return simple, computed values. The previous implementation required `#[turbo_tasks::function]` annotations and `Vc<T>` wrappers even when the methods didn't need to perform async operations or benefit from caching. ### Impact | Metric | Base | Change | Delta | |--------|------|--------|-------| | Hits | 35,678,143 | 35,845,124 | **+166,981** | | Misses | 9,418,378 | 7,910,986 | **-1,507,392** | | Total | 45,096,521 | 43,756,110 | **-1,340,411** | | Task types | 1,306 | 1,277 | **-29** | 29 task types were removed, eliminating **2.6M total task invocations** (1.1M hits + 1.5M misses): - **`chunking_type`** — 21 task types removed across all `ModuleReference` implementors (~952k invocations) - **`binding_usage`** — 6 task types removed (~527k invocations) - **`BindingUsage::all`** — helper task removed (~36k invocations) - **`EsmAssetReference::get_referenced_asset`** — removed and inlined (~1.08M invocations: 628k hits + 451k misses) The removed `get_referenced_asset` hits reappear as +628k hits on `EsmAssetReference::resolve_reference` and `ReferencedAsset::from_resolve_result` (with zero increase in misses), confirming the work is now served from cache through the existing callers. No tasks had increased misses — the removal is clean with no cache invalidation spillover. I also ran some builds to measure latency ``` # This branch $ hyperfine -p 'rm -rf .next' -w 2 -r 10 'pnpm next build --turbopack --experimental-build-mode=compile' Benchmark 1: pnpm next build --turbopack --experimental-build-mode=compile Time (mean ± σ): 52.752 s ± 0.658 s [User: 376.575 s, System: 106.375 s] Range (min … max): 51.913 s … 54.161 s 10 runs # on canary $ hyperfine -p 'rm -rf .next' -w 2 -r 10 'pnpm next build --turbopack --experimental-build-mode=compile' Benchmark 1: pnpm next build --turbopack --experimental-build-mode=compile Time (mean ± σ): 54.675 s ± 1.394 s [User: 389.273 s, System: 114.642 s] Range (min … max): 53.434 s … 58.189 s 10 runs ``` so a solid win of almost 2 seconds MaxRSS also went from 16,474,324,992 bytes to 16,359,309,312 bytes (from one measurement) so a savings of ~100M of max heap size. ### How? - Changed `chunking_type()` method signature from `Vc<ChunkingTypeOption>` to `Option<ChunkingType>` - Changed `binding_usage()` method signature from `Vc<BindingUsage>` to `BindingUsage` - Removed `ChunkingTypeOption` type alias as it's no longer needed - Updated all implementations across the codebase to return direct values instead of wrapped ones - Removed `#[turbo_tasks::function]` annotations from these methods - Updated call sites to use `into_trait_ref().await?` pattern when accessing these methods from `Vc<dyn ModuleReference>` - Removed `EsmAssetReference::get_referenced_asset`, inlining its logic into callers - Added validation for `turbopack-chunking-type` annotation values in import analysis - Fixed cache effectiveness analysis script
1 parent 00067f4 commit 236a76d

31 files changed

Lines changed: 335 additions & 387 deletions

File tree

crates/next-api/src/nft_json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use turbo_rcstr::{RcStr, rcstr};
77
use turbo_tasks::{
88
FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
99
graph::{AdjacencyMap, GraphTraversal, Visit},
10-
turbobail, turbofmt,
10+
turbofmt,
1111
};
1212
use turbo_tasks_fs::{
1313
DirectoryEntry, File, FileContent, FileSystem, FileSystemPath,
@@ -287,7 +287,7 @@ impl Asset for NftJsonAsset {
287287
&*current_path.get_type().await?,
288288
FileSystemEntryType::Symlink
289289
) {
290-
turbobail!(
290+
turbo_tasks::turbobail!(
291291
"Encountered file inside of symlink in NFT list: {current_path} \
292292
is a symlink, but {referenced_chunk_path} was created inside of \
293293
it"

crates/next-core/src/hmr_entry.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use turbopack_core::{
88
asset::{Asset, AssetContent},
99
chunk::{
1010
AsyncModuleInfo, ChunkItem, ChunkableModule, ChunkingContext, ChunkingType,
11-
ChunkingTypeOption, EvaluatableAsset,
11+
EvaluatableAsset,
1212
},
1313
ident::AssetIdent,
1414
module::{Module, ModuleSideEffects},
@@ -164,11 +164,10 @@ impl ModuleReference for HmrEntryModuleReference {
164164
*ModuleResolveResult::module(self.module)
165165
}
166166

167-
#[turbo_tasks::function]
168-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
169-
Vc::cell(Some(ChunkingType::Parallel {
167+
fn chunking_type(&self) -> Option<ChunkingType> {
168+
Some(ChunkingType::Parallel {
170169
inherit_async: false,
171170
hoisted: false,
172-
}))
171+
})
173172
}
174173
}

crates/next-core/src/next_client_reference/css_client_reference/css_client_reference_module.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, ValueToString, Vc};
44
use turbo_tasks_fs::FileContent;
55
use turbopack_core::{
66
asset::{Asset, AssetContent},
7-
chunk::{ChunkGroupType, ChunkingType, ChunkingTypeOption},
7+
chunk::{ChunkGroupType, ChunkingType},
88
ident::AssetIdent,
99
module::{Module, ModuleSideEffects},
1010
reference::{ModuleReference, ModuleReferences},
@@ -99,11 +99,10 @@ impl ModuleReference for CssClientReference {
9999
*ModuleResolveResult::module(self.module)
100100
}
101101

102-
#[turbo_tasks::function]
103-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
104-
Vc::cell(Some(ChunkingType::Isolated {
102+
fn chunking_type(&self) -> Option<ChunkingType> {
103+
Some(ChunkingType::Isolated {
105104
_ty: ChunkGroupType::Evaluated,
106105
merge_tag: Some(rcstr!("client")),
107-
}))
106+
})
108107
}
109108
}

crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_module.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use turbopack_core::{
99
asset::AssetContent,
1010
chunk::{
1111
AsyncModuleInfo, ChunkGroupType, ChunkItem, ChunkType, ChunkableModule, ChunkingContext,
12-
ChunkingType, ChunkingTypeOption,
12+
ChunkingType,
1313
},
1414
code_builder::CodeBuilder,
1515
context::AssetContext,
@@ -384,11 +384,10 @@ impl ModuleReference for EcmascriptClientReference {
384384
*ModuleResolveResult::module(self.module)
385385
}
386386

387-
#[turbo_tasks::function]
388-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
389-
Vc::cell(Some(ChunkingType::Isolated {
387+
fn chunking_type(&self) -> Option<ChunkingType> {
388+
Some(ChunkingType::Isolated {
390389
_ty: self.ty,
391390
merge_tag: self.merge_tag.clone(),
392-
}))
391+
})
393392
}
394393
}

crates/next-core/src/next_server_component/server_component_reference.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
22
use turbopack_core::{
3-
chunk::{ChunkingType, ChunkingTypeOption},
4-
module::Module,
5-
reference::ModuleReference,
6-
resolve::ModuleResolveResult,
3+
chunk::ChunkingType, module::Module, reference::ModuleReference, resolve::ModuleResolveResult,
74
};
85

96
#[turbo_tasks::value]
@@ -27,11 +24,10 @@ impl ModuleReference for NextServerComponentModuleReference {
2724
fn resolve_reference(&self) -> Vc<ModuleResolveResult> {
2825
*ModuleResolveResult::module(self.asset)
2926
}
30-
#[turbo_tasks::function]
31-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
32-
Vc::cell(Some(ChunkingType::Shared {
27+
fn chunking_type(&self) -> Option<ChunkingType> {
28+
Some(ChunkingType::Shared {
3329
inherit_async: true,
3430
merge_tag: None,
35-
}))
31+
})
3632
}
3733
}

crates/next-core/src/next_server_utility/server_utility_reference.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use once_cell::sync::Lazy;
22
use turbo_rcstr::{RcStr, rcstr};
33
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
44
use turbopack_core::{
5-
chunk::{ChunkingType, ChunkingTypeOption},
6-
module::Module,
7-
reference::ModuleReference,
8-
resolve::ModuleResolveResult,
5+
chunk::ChunkingType, module::Module, reference::ModuleReference, resolve::ModuleResolveResult,
96
};
107

118
#[turbo_tasks::value]
@@ -32,11 +29,10 @@ impl ModuleReference for NextServerUtilityModuleReference {
3229
*ModuleResolveResult::module(self.asset)
3330
}
3431

35-
#[turbo_tasks::function]
36-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
37-
Vc::cell(Some(ChunkingType::Shared {
32+
fn chunking_type(&self) -> Option<ChunkingType> {
33+
Some(ChunkingType::Shared {
3834
inherit_async: true,
3935
merge_tag: Some(NEXT_SERVER_UTILITY_MERGE_TAG.clone()),
40-
}))
36+
})
4137
}
4238
}

packages/next/src/build/swc/generated-native.d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ export declare function codeFrameColumns(
111111
location: NapiCodeFrameLocation,
112112
options?: NapiCodeFrameOptions | undefined | null
113113
): string | null
114+
/**
115+
* Convert an array of dash-case feature name strings to a lightningcss
116+
* `Features` bitmask (u32). Called from the webpack lightningcss-loader to
117+
* avoid duplicating the name-to-bit mapping in JavaScript.
118+
*/
119+
export declare function lightningcssFeatureNamesToMaskNapi(
120+
names: Array<string>
121+
): number
114122
export declare function lockfileTryAcquireSync(
115123
path: string,
116124
content?: string | undefined | null
@@ -248,7 +256,7 @@ export interface NapiProjectOptions {
248256
isPersistentCachingEnabled: boolean
249257
/** The version of Next.js that is running. */
250258
nextVersion: RcStr
251-
/** Whether server-side HMR is enabled (requires --experimental-server-fast-refresh). */
259+
/** Whether server-side HMR is enabled (--experimental-server-fast-refresh). */
252260
serverHmr?: boolean
253261
}
254262
/** [NapiProjectOptions] with all fields optional. */
@@ -294,8 +302,6 @@ export interface NapiPartialProjectOptions {
294302
* debugging/profiling purposes.
295303
*/
296304
noMangling?: boolean
297-
/** Whether server-side HMR is enabled (requires --experimental-server-fast-refresh). */
298-
serverHmr?: boolean
299305
}
300306
export interface NapiDefineEnv {
301307
client: Array<NapiOptionEnvVar>

0 commit comments

Comments
 (0)