Unverified Commit df1502c7 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Sort batched chunk items globally by path (#98433)

### What?

Globally order all emitted Turbopack ECMAScript chunk items by module
path, including items nested inside batches. Browser and Node.js chunk
emitters now consume the same flattened, ordered result.

### Why?

The previous ordering compared only the first module in each item or
batch group. Remaining modules in a batch stayed together even when
their paths belonged elsewhere in the global order, reducing locality
between similar modules and slightly worsening gzip compression.

For `bench/basic-app`, production JavaScript chunk gzip sizes changed as
follows while raw bytes and chunk counts stayed unchanged:

| Output | Before | After | Change |
|---|---:|---:|---:|
| Browser chunks | 1,619,643 B | 1,619,314 B | -329 B (-0.020%) |
| Server chunks | 792,379 B | 792,186 B | -193 B (-0.024%) |
| Combined | 2,412,022 B | 2,411,500 B | -522 B (-0.022%) |

### How?

The shared ECMAScript chunk-content accessor now flattens resolved item
and batch groups and sorts every module by `(path, module id)`. Keeping
this ordering in the shared layer gives browser and Node.js output
identical deterministic behavior and removes duplicated emitter-side
sorting.

### Verification

- `cargo fmt --all -- --check`
- `cargo check -p turbopack-browser -p turbopack-nodejs`
- `cargo test -p turbopack-browser -p turbopack-nodejs`
- `pnpm build-all`
- Clean baseline and candidate production builds of `bench/basic-app`

<!-- NEXT_JS_LLM -->


<!-- fleet d30d9e3f-6885-4665-ae08-b37fa6151b95 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 156050f1
...@@ -102,20 +102,11 @@ impl EcmascriptBrowserChunkContent { ...@@ -102,20 +102,11 @@ impl EcmascriptBrowserChunkContent {
)?; )?;
let content = this.content.await?; let content = this.content.await?;
let mut chunk_items = content.chunk_item_code_module_ids_and_paths().await?; let chunk_items = content.chunk_item_code_module_ids_and_paths().await?;
// Sort items by their module path so that similar modules stay for (id, item_code, _) in &chunk_items {
// together so that the chunks gzips better. write!(code, "\n{}, ", StringifyJs(id))?;
chunk_items.sort_by(|a, b| { code.push_code(item_code);
a.first() write!(code, ",")?;
.map(|(id, _, path)| (path, id))
.cmp(&b.first().map(|(id, _, path)| (path, id)))
});
for item in &chunk_items {
for (id, item_code, _) in &**item {
write!(code, "\n{}, ", StringifyJs(id))?;
code.push_code(item_code);
write!(code, ",")?;
}
} }
write!(code, "\n]);")?; write!(code, "\n]);")?;
......
...@@ -2,11 +2,14 @@ use std::future::IntoFuture; ...@@ -2,11 +2,14 @@ use std::future::IntoFuture;
use anyhow::Result; use anyhow::Result;
use either::Either; use either::Either;
use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::chunk::{ChunkItem, ChunkItems, batch_info}; use turbopack_core::{
chunk::{ChunkItem, ChunkItems, ModuleId, batch_info},
code_builder::Code,
};
use crate::chunk::{ use crate::chunk::{
CodeModuleIdsAndPaths,
batch::{EcmascriptChunkItemBatchGroup, EcmascriptChunkItemOrBatchWithAsyncInfo}, batch::{EcmascriptChunkItemBatchGroup, EcmascriptChunkItemOrBatchWithAsyncInfo},
batch_group_code_module_ids_and_paths, item_code_module_ids_and_paths, batch_group_code_module_ids_and_paths, item_code_module_ids_and_paths,
}; };
...@@ -51,13 +54,22 @@ impl EcmascriptChunkContent { ...@@ -51,13 +54,22 @@ impl EcmascriptChunkContent {
impl EcmascriptChunkContent { impl EcmascriptChunkContent {
pub async fn chunk_item_code_module_ids_and_paths( pub async fn chunk_item_code_module_ids_and_paths(
&self, &self,
) -> Result<Vec<ReadRef<CodeModuleIdsAndPaths>>> { ) -> Result<Vec<(ModuleId, ReadRef<Code>, RcStr)>> {
batch_info( let chunk_item_groups = batch_info(
&self.batch_groups, &self.batch_groups,
&self.chunk_items, &self.chunk_items,
|batch| batch_group_code_module_ids_and_paths(batch).into_future(), |batch| batch_group_code_module_ids_and_paths(batch).into_future(),
|item| item_code_module_ids_and_paths(item.clone()).into_future(), |item| item_code_module_ids_and_paths(item.clone()).into_future(),
) )
.await .await?;
let mut chunk_items = chunk_item_groups
.iter()
.flat_map(|items| items.iter().cloned())
.collect::<Vec<_>>();
// Sort all items by their module path so that similar modules stay
// together and the chunks gzip better.
chunk_items
.sort_by(|(a_id, _, a_path), (b_id, _, b_path)| (a_path, a_id).cmp(&(b_path, b_id)));
Ok(chunk_items)
} }
} }
...@@ -64,18 +64,11 @@ impl EcmascriptNodeChunkContent { ...@@ -64,18 +64,11 @@ impl EcmascriptNodeChunkContent {
write!(code, "module.exports = [")?; write!(code, "module.exports = [")?;
let content = self.content.await?; let content = self.content.await?;
let mut chunk_items = content.chunk_item_code_module_ids_and_paths().await?; let chunk_items = content.chunk_item_code_module_ids_and_paths().await?;
chunk_items.sort_by(|a, b| { for (id, item_code, _) in &chunk_items {
a.first() write!(code, "\n{}, ", StringifyJs(id))?;
.map(|(id, _, path)| (path, id)) code.push_code(item_code);
.cmp(&b.first().map(|(id, _, path)| (path, id))) write!(code, ",")?;
});
for item in &chunk_items {
for (id, item_code, _) in &**item {
write!(code, "\n{}, ", StringifyJs(id))?;
code.push_code(item_code);
write!(code, ",")?;
}
} }
write!(code, "\n];")?; write!(code, "\n];")?;
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/0_9x_turbopack-tests_tests_snapshot_scope-hoisting_split-shared_input_02x_50fz-7djz._.js", (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/0_9x_turbopack-tests_tests_snapshot_scope-hoisting_split-shared_input_02x_50fz-7djz._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([]);
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$scope$2d$hoisting$2f$split$2d$shared$2f$input$2f$x$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js [test] (ecmascript)");
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$scope$2d$hoisting$2f$split$2d$shared$2f$input$2f$y$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/index.js [test] (ecmascript)");
;
;
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js [test] (ecmascript)", ((__turbopack_context__, module, exports) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js [test] (ecmascript)", ((__turbopack_context__, module, exports) => {
setTimeout(()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/inner.js [test] (ecmascript, async loader)"), 500); setTimeout(()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/inner.js [test] (ecmascript, async loader)"), 500);
...@@ -18,15 +27,6 @@ __turbopack_context__.v((parentImport) => { ...@@ -18,15 +27,6 @@ __turbopack_context__.v((parentImport) => {
setTimeout(()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/middle.js [test] (ecmascript, async loader)"), 1000); setTimeout(()=>__turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/middle.js [test] (ecmascript, async loader)"), 1000);
}), }),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([]);
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$scope$2d$hoisting$2f$split$2d$shared$2f$input$2f$x$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js [test] (ecmascript)");
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$scope$2d$hoisting$2f$split$2d$shared$2f$input$2f$y$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/index.js [test] (ecmascript)");
;
;
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/middle.js [test] (ecmascript, async loader)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/middle.js [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => { __turbopack_context__.v((parentImport) => {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"version": 3, "version": 3,
"sources": [], "sources": [],
"sections": [ "sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js"],"sourcesContent":["setTimeout(() => import('./inner'), 500)\n"],"names":["setTimeout"],"mappings":"AAAAA,WAAW,yKAAyB"}}, {"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/index.js"],"sourcesContent":["import './x/index'\nimport './y/index'\n"],"names":[],"mappings":";AAAA;AACA"}},
{"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/index.js"],"sourcesContent":["setTimeout(() => import('./middle'), 1000)\n"],"names":["setTimeout"],"mappings":"AAAAA,WAAW,0KAA0B"}}, {"offset": {"line": 12, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/x/index.js"],"sourcesContent":["setTimeout(() => import('./inner'), 500)\n"],"names":["setTimeout"],"mappings":"AAAAA,WAAW,yKAAyB"}},
{"offset": {"line": 23, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/index.js"],"sourcesContent":["import './x/index'\nimport './y/index'\n"],"names":[],"mappings":";AAAA;AACA"}}] {"offset": {"line": 27, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/scope-hoisting/split-shared/input/y/index.js"],"sourcesContent":["setTimeout(() => import('./middle'), 1000)\n"],"names":["setTimeout"],"mappings":"AAAAA,WAAW,0KAA0B"}}]
} }
\ No newline at end of file
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