Unverified Commit 26543f1e authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] allow to merge into big enough chunk candidate when 100% overlap (#76321)

### What?

Instead of not looking for further merge candiates when the min chunk size has reached, continue to merge with small chunks that are in all of these chunk groups.
parent 9df56c2c
...@@ -156,6 +156,15 @@ pub async fn make_production_chunks( ...@@ -156,6 +156,15 @@ pub async fn make_production_chunks(
if value <= 1 { if value <= 1 {
continue; continue;
} }
// If the candidate is already big enough, avoid shrinking the sharing
if candidate.size > merge_threshold
&& value != candidate.chunk_groups_len()
{
continue;
}
if other.size > merge_threshold && value != other.chunk_groups_len() {
continue;
}
if let Some((best_i1, best_i2, best_value)) = best_combination.as_mut() if let Some((best_i1, best_i2, best_value)) = best_combination.as_mut()
{ {
if value > *best_value { if value > *best_value {
...@@ -170,63 +179,75 @@ pub async fn make_production_chunks( ...@@ -170,63 +179,75 @@ pub async fn make_production_chunks(
selection.push(candidate); selection.push(candidate);
} }
if let Some((best_i1, best_i2, _)) = best_combination.as_ref() { let best_value =
let other = selection.swap_remove(*best_i2); if let Some((best_i1, best_i2, best_value)) = best_combination.as_ref() {
let mut candidate = selection.swap_remove(*best_i1); let other = selection.swap_remove(*best_i2);
for unused in selection { let mut candidate = selection.swap_remove(*best_i1);
chunks_to_merge.push(unused); // Merge other into candidate
} let MergeCandidate {
// Merge other into candidate size,
let MergeCandidate { chunk_items,
size, chunk_groups,
chunk_items, } = other;
chunk_groups, candidate.size += size;
} = other; candidate.chunk_items.extend(chunk_items);
candidate.size += size; candidate.chunk_groups =
candidate.chunk_items.extend(chunk_items); merge_chunk_groups(&candidate.chunk_groups, &chunk_groups);
candidate.chunk_groups =
merge_chunk_groups(&candidate.chunk_groups, &chunk_groups); // Merged candidate is pushed back into the queue
chunks_to_merge.push(candidate);
// Merged chunk either goes back to the heap or
// is considered for merging again *best_value
if candidate.size > merge_threshold { } else {
u64::MAX
};
for unused in selection {
// Candiates from selection that are already big enough move into the
// heap again when no more merges are expected.
// Since we can only merge into big enough candates when overlap ==
// chunk_groups_len we can use that as condition.
if unused.size > merge_threshold && unused.chunk_groups_len() > best_value {
heap.push(ChunkCandidate { heap.push(ChunkCandidate {
size: candidate.size, size: unused.size,
chunk_items: candidate.chunk_items, chunk_items: unused.chunk_items,
chunk_groups: candidate.chunk_groups, chunk_groups: unused.chunk_groups,
}); });
} else { } else {
chunks_to_merge.push(candidate);
}
} else {
// No merges possible
for unused in selection {
chunks_to_merge.push(unused); chunks_to_merge.push(unused);
} }
}
if best_combination.is_none() {
// No merges possible
break; break;
} }
} }
// Left-over chunks are merged together forming the remainer chunk, which includes let mut remainer_size = 0;
// all modules that are not sharable let mut remainer_chunk_items = Vec::new();
if let Some(MergeCandidate { for MergeCandidate {
mut size, size,
mut chunk_items, chunk_items,
chunk_groups: _, chunk_groups,
}) = chunks_to_merge.pop() } in chunks_to_merge.into_iter()
{ {
for chunk in chunks_to_merge { if size > merge_threshold {
let MergeCandidate { heap.push(ChunkCandidate {
size: other_size, size,
chunk_items: other_chunk_items, chunk_items,
chunk_groups: _, chunk_groups,
} = chunk; });
size += other_size; } else {
chunk_items.extend(other_chunk_items); remainer_size += size;
remainer_chunk_items.extend(chunk_items);
} }
}
// Left-over chunks are merged together forming the remainer chunk, which includes
// all modules that are not sharable
if !remainer_chunk_items.is_empty() {
heap.push(ChunkCandidate { heap.push(ChunkCandidate {
size, size: remainer_size,
chunk_items, chunk_items: remainer_chunk_items,
chunk_groups: None, 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