Skip to content

Commit

Permalink
Auto merge of rust-lang#77435 - hanmertens:binary_heap_append, r=scot…
Browse files Browse the repository at this point in the history
…tmcm

Always use extend in BinaryHeap::append

This is faster, see rust-lang#77433.

Fixes rust-lang#77433
  • Loading branch information
bors committed Jan 16, 2021
2 parents efdb859 + 32a20f4 commit 410a546
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,16 @@ impl<T: Ord> BinaryHeap<T> {
// and about 2 * (len1 + len2) comparisons in the worst case
// while `extend` takes O(len2 * log(len1)) operations
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
// assuming len1 >= len2.
// assuming len1 >= len2. For larger heaps, the crossover point
// no longer follows this reasoning and was determined empirically.
#[inline]
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
2 * (len1 + len2) < len2 * log2_fast(len1)
let tot_len = len1 + len2;
if tot_len <= 2048 {
2 * tot_len < len2 * log2_fast(len1)
} else {
2 * tot_len < len2 * 11
}
}

if better_to_rebuild(self.len(), other.len()) {
Expand Down

0 comments on commit 410a546

Please sign in to comment.