Skip to content

Commit 410a546

Browse files
committed
Auto merge of rust-lang#77435 - hanmertens:binary_heap_append, r=scottmcm
Always use extend in BinaryHeap::append This is faster, see rust-lang#77433. Fixes rust-lang#77433
2 parents efdb859 + 32a20f4 commit 410a546

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Diff for: 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)