Unverified Commit 9a3d5a5e authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] merged chunks need to have min overlap of 2 merge remainers into chunk (#76320)

### What?

only merge small chunks when they occur in multiple overlapping chunk groups.
All unmergable small chunks will go into a leftover chunk which combines all non-shared chunks.
parent 337cdb70
......@@ -104,7 +104,7 @@ pub async fn make_production_chunks(
smallest.size
};
let too_many_chunks = max_chunk_count_per_group != 0
&& heap.len() + chunks_to_merge_size / merge_threshold
&& heap.len() + chunks_to_merge_size / merge_threshold + 1
> max_chunk_count_per_group;
let too_small_chunk = min_chunk_size != 0 && smallest.size < min_chunk_size;
if too_many_chunks || too_small_chunk {
......@@ -152,6 +152,10 @@ pub async fn make_production_chunks(
// Check all combination with the new candidate
for (i, other) in selection.iter().enumerate() {
let value = overlap(&candidate.chunk_groups, &other.chunk_groups);
// It need to have at least two chunk groups in common
if value <= 1 {
continue;
}
if let Some((best_i1, best_i2, best_value)) = best_combination.as_mut()
{
if value > *best_value {
......@@ -194,15 +198,36 @@ pub async fn make_production_chunks(
} else {
chunks_to_merge.push(candidate);
}
} else {
// No merges possible
for unused in selection {
chunks_to_merge.push(unused);
}
break;
}
}
// Left-over chunks go back to the heap
for chunk in chunks_to_merge {
// Left-over chunks are merged together forming the remainer chunk, which includes
// all modules that are not sharable
if let Some(MergeCandidate {
mut size,
mut chunk_items,
chunk_groups: _,
}) = chunks_to_merge.pop()
{
for chunk in chunks_to_merge {
let MergeCandidate {
size: other_size,
chunk_items: other_chunk_items,
chunk_groups: _,
} = chunk;
size += other_size;
chunk_items.extend(other_chunk_items);
}
heap.push(ChunkCandidate {
size: chunk.size,
chunk_items: chunk.chunk_items,
chunk_groups: chunk.chunk_groups,
size,
chunk_items,
chunk_groups: None,
});
}
}
......
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