Skip to content

Commit 32a20f4

Browse files
committed
Change rebuild heuristic in BinaryHeap::append
See #77433 for why the new heuristic was chosen. Fixes #77433
1 parent e48eb37 commit 32a20f4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/alloc/src/collections/binary_heap.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,16 @@ impl<T: Ord> BinaryHeap<T> {
630630
// and about 2 * (len1 + len2) comparisons in the worst case
631631
// while `extend` takes O(len2 * log(len1)) operations
632632
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
633-
// assuming len1 >= len2.
633+
// assuming len1 >= len2. For larger heaps, the crossover point
634+
// no longer follows this reasoning and was determined empirically.
634635
#[inline]
635636
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
636-
2 * (len1 + len2) < len2 * log2_fast(len1)
637+
let tot_len = len1 + len2;
638+
if tot_len <= 2048 {
639+
2 * tot_len < len2 * log2_fast(len1)
640+
} else {
641+
2 * tot_len < len2 * 11
642+
}
637643
}
638644

639645
if better_to_rebuild(self.len(), other.len()) {

0 commit comments

Comments
 (0)