Unverified Commit c16109f4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: speedup module merging (#85043)

preallocate hashmaps
parent 3f09e569
...@@ -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::{
......
...@@ -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_lists,
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_lists: 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)
......
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