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,12 +179,10 @@ pub async fn make_production_chunks( ...@@ -170,12 +179,10 @@ 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 =
if let Some((best_i1, best_i2, best_value)) = best_combination.as_ref() {
let other = selection.swap_remove(*best_i2); let other = selection.swap_remove(*best_i2);
let mut candidate = selection.swap_remove(*best_i1); let mut candidate = selection.swap_remove(*best_i1);
for unused in selection {
chunks_to_merge.push(unused);
}
// Merge other into candidate // Merge other into candidate
let MergeCandidate { let MergeCandidate {
size, size,
...@@ -187,46 +194,60 @@ pub async fn make_production_chunks( ...@@ -187,46 +194,60 @@ pub async fn make_production_chunks(
candidate.chunk_groups = candidate.chunk_groups =
merge_chunk_groups(&candidate.chunk_groups, &chunk_groups); merge_chunk_groups(&candidate.chunk_groups, &chunk_groups);
// Merged chunk either goes back to the heap or // Merged candidate is pushed back into the queue
// is considered for merging again
if candidate.size > merge_threshold {
heap.push(ChunkCandidate {
size: candidate.size,
chunk_items: candidate.chunk_items,
chunk_groups: candidate.chunk_groups,
});
} else {
chunks_to_merge.push(candidate); chunks_to_merge.push(candidate);
}
*best_value
} else { } else {
// No merges possible u64::MAX
};
for unused in selection { 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 {
size: unused.size,
chunk_items: unused.chunk_items,
chunk_groups: unused.chunk_groups,
});
} else {
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 {
size: other_size,
chunk_items: other_chunk_items,
chunk_groups: _,
} = chunk;
size += other_size;
chunk_items.extend(other_chunk_items);
}
heap.push(ChunkCandidate { heap.push(ChunkCandidate {
size, size,
chunk_items, chunk_items,
chunk_groups,
});
} else {
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 {
size: remainer_size,
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