Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c16109f4
Unverified
Commit
c16109f4
authored
Oct 21, 2025
by
Niklas Mischkulnig
Committed by
GitHub
Oct 21, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Turbopack: speedup module merging (#85043)
preallocate hashmaps
parent
3f09e569
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
87 deletions
+142
-87
parallel.rs
turbopack/crates/turbo-tasks/src/parallel.rs
+27
-1
merged_modules.rs
.../crates/turbopack-core/src/module_graph/merged_modules.rs
+115
-86
No files found.
turbopack/crates/turbo-tasks/src/parallel.rs
View file @
c16109f4
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
use
crate
::{
use
crate
::{
scope
::
scope_and_block
,
scope
::
scope_and_block
,
util
::{
good_chunk_size
,
into_chunks
},
util
::{
Chunk
,
good_chunk_size
,
into_chunks
},
};
};
struct
Chunked
{
struct
Chunked
{
...
@@ -231,6 +231,32 @@ where
...
@@ -231,6 +231,32 @@ where
.collect
()
.collect
()
}
}
pub
fn
map_collect_chunked_owned
<
'l
,
Item
,
PerItemResult
,
Result
>
(
items
:
Vec
<
Item
>
,
f
:
impl
Fn
(
Chunk
<
Item
>
)
->
PerItemResult
+
Send
+
Sync
,
)
->
Result
where
Item
:
Send
+
Sync
,
PerItemResult
:
Send
+
Sync
+
'l
,
Result
:
FromIterator
<
PerItemResult
>
,
{
let
Some
(
Chunked
{
chunk_size
,
chunk_count
,
})
=
get_chunked
(
items
.len
())
else
{
let
len
=
items
.len
();
return
Result
::
from_iter
(
into_chunks
(
items
,
len
)
.map
(
f
));
};
let
f
=
&
f
;
scope_and_block
(
chunk_count
,
|
scope
|
{
for
chunk
in
into_chunks
(
items
,
chunk_size
)
{
scope
.spawn
(
move
||
f
(
chunk
))
}
})
.collect
()
}
#[cfg(test)]
#[cfg(test)]
mod
tests
{
mod
tests
{
use
std
::{
use
std
::{
...
...
turbopack/crates/turbopack-core/src/module_graph/merged_modules.rs
View file @
c16109f4
...
@@ -279,8 +279,9 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -279,8 +279,9 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
{
{
struct
ChunkGroupResult
{
struct
ChunkGroupResult
{
chunk_group_idx
:
usize
,
first_chunk_group_idx
:
usize
,
lists
:
Vec
<
Vec
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>>>
,
#[allow(clippy
::
type_complexity)]
list_lists
:
Vec
<
Vec
<
Vec
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>>>>
,
lists_reverse_indices
:
lists_reverse_indices
:
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>
,
FxIndexSet
<
ListOccurrence
>>
,
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>
,
FxIndexSet
<
ListOccurrence
>>
,
#[allow(clippy
::
type_complexity)]
#[allow(clippy
::
type_complexity)]
...
@@ -289,11 +290,29 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -289,11 +290,29 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
FxIndexSet
<
ResolvedVc
<
Box
<
dyn
Module
>>>
,
FxIndexSet
<
ResolvedVc
<
Box
<
dyn
Module
>>>
,
>
,
>
,
}
}
let
span
=
tracing
::
info_span!
(
"map chunk groups"
)
.entered
();
let
result
=
turbo_tasks
::
parallel
::
map_collect_owned
::
<
_
,
_
,
Result
<
Vec
<
_
>>>
(
let
result
=
turbo_tasks
::
parallel
::
map_collect_
chunked_
owned
::
<
_
,
_
,
Result
<
Vec
<
_
>>>
(
// TODO without collect
// TODO without collect
chunk_group_info
.chunk_groups
.iter
()
.enumerate
()
.collect
(),
chunk_group_info
.chunk_groups
.iter
()
.enumerate
()
.collect
(),
|(
chunk_group_idx
,
chunk_group
)|
{
|
chunk
|
{
let
mut
list_lists
=
vec!
[];
let
mut
lists_reverse_indices
:
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>
,
FxIndexSet
<
ListOccurrence
>
,
>
=
FxIndexMap
::
default
();
#[allow(clippy
::
type_complexity)]
let
mut
intra_group_references_rev
:
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
Module
>>
,
FxIndexSet
<
ResolvedVc
<
Box
<
dyn
Module
>>>
,
>
=
FxIndexMap
::
default
();
let
mut
chunk
=
chunk
.peekable
();
let
first_chunk_group_idx
=
chunk
.peek
()
.unwrap
()
.
0
;
for
(
chunk_group_idx
,
chunk_group
)
in
chunk
{
let
mut
lists
=
vec!
[];
// A partition of all modules in the chunk into several execution traces
// A partition of all modules in the chunk into several execution traces
// (orderings), stored in the top-level lists and referenced here by
// (orderings), stored in the top-level lists and referenced here by
// index.
// index.
...
@@ -308,17 +327,6 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -308,17 +327,6 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
// leading to the list `a, b` which is not execution order.
// leading to the list `a, b` which is not execution order.
let
mut
visited
=
FxHashSet
::
default
();
let
mut
visited
=
FxHashSet
::
default
();
let
mut
lists
=
vec!
[];
let
mut
lists_reverse_indices
:
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
MergeableModule
>>
,
FxIndexSet
<
ListOccurrence
>
,
>
=
FxIndexMap
::
default
();
#[allow(clippy
::
type_complexity)]
let
mut
intra_group_references_rev
:
FxIndexMap
<
ResolvedVc
<
Box
<
dyn
Module
>>
,
FxIndexSet
<
ResolvedVc
<
Box
<
dyn
Module
>>>
,
>
=
FxIndexMap
::
default
();
module_graph
.traverse_edges_from_entries_dfs
(
module_graph
.traverse_edges_from_entries_dfs
(
chunk_group
.entries
(),
chunk_group
.entries
(),
&
mut
(),
&
mut
(),
...
@@ -339,7 +347,9 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -339,7 +347,9 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
if
mergeable
.contains
(
&
module
)
{
if
mergeable
.contains
(
&
module
)
{
let
mergeable_module
=
let
mergeable_module
=
ResolvedVc
::
try_downcast
::
<
Box
<
dyn
MergeableModule
>>
(
module
)
ResolvedVc
::
try_downcast
::
<
Box
<
dyn
MergeableModule
>>
(
module
,
)
.unwrap
();
.unwrap
();
match
chunk_lists
.entry
(
bitmap
)
{
match
chunk_lists
.entry
(
bitmap
)
{
Entry
::
Vacant
(
e
)
=>
{
Entry
::
Vacant
(
e
)
=>
{
...
@@ -373,7 +383,8 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -373,7 +383,8 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
}
}
if
let
Some
((
parent
,
_
))
=
parent_info
{
if
let
Some
((
parent
,
_
))
=
parent_info
{
let
same_bitmap
=
module_merged_groups
.get
(
&
parent
.module
)
.unwrap
()
let
same_bitmap
=
module_merged_groups
.get
(
&
parent
.module
)
.unwrap
()
==
module_merged_groups
.get
(
&
module
)
.unwrap
();
==
module_merged_groups
.get
(
&
module
)
.unwrap
();
if
same_bitmap
{
if
same_bitmap
{
...
@@ -386,25 +397,43 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
...
@@ -386,25 +397,43 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
Ok
(())
Ok
(())
},
},
)
?
;
)
?
;
list_lists
.push
(
lists
);
}
Ok
(
ChunkGroupResult
{
Ok
(
ChunkGroupResult
{
chunk_group_idx
,
first_
chunk_group_idx
,
lists
,
list
_list
s
,
lists_reverse_indices
,
lists_reverse_indices
,
intra_group_references_rev
,
intra_group_references_rev
,
})
})
},
},
)
?
;
)
?
;
lists
=
vec!
[
Default
::
default
();
result
.len
()
+
1
];
drop
(
span
);
let
_span
=
tracing
::
info_span!
(
"merging chunk group lists"
)
.entered
();
lists_reverse_indices
.reserve_exact
(
result
.iter
()
.map
(|
r
|
r
.lists_reverse_indices
.len
())
.sum
());
intra_group_references_rev
.reserve_exact
(
result
.iter
()
.map
(|
r
|
r
.intra_group_references_rev
.len
())
.sum
(),
);
lists
=
vec!
[
Default
::
default
();
chunk_group_info
.chunk_groups
.len
()
+
1
];
LISTS_COMMON_IDX
=
result
.len
();
LISTS_COMMON_IDX
=
result
.len
();
for
ChunkGroupResult
{
for
ChunkGroupResult
{
chunk_group_idx
,
first_
chunk_group_idx
,
lists
:
result_lists
,
list
_list
s
:
result_lists
,
lists_reverse_indices
:
result_lists_reverse_indices
,
lists_reverse_indices
:
result_lists_reverse_indices
,
intra_group_references_rev
:
result_intra_group_references_rev
,
intra_group_references_rev
:
result_intra_group_references_rev
,
}
in
result
}
in
result
{
{
lists
[
chunk_group_idx
]
=
result_lists
;
lists
.splice
(
first_chunk_group_idx
..
(
first_chunk_group_idx
+
result_lists
.len
()),
result_lists
,
);
for
(
module
,
occurrences
)
in
result_lists_reverse_indices
{
for
(
module
,
occurrences
)
in
result_lists_reverse_indices
{
lists_reverse_indices
lists_reverse_indices
.entry
(
module
)
.entry
(
module
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment