Skip to content

Commit

Permalink
Auto merge of #36072 - arthurprs:binary_heap_opt, r=Aatch
Browse files Browse the repository at this point in the history
Optimize BinaryHeap bounds checking

I was experimenting with d-ary binary heaps during the weekend (dead end) and I found that we could get some good improvements by removing bounds checking. Specially due to the panic-safe additional code, llvm can't really optimize them out.

```
 name                         d_ary_heap:: ns/iter  std___heap:: ns/iter  diff ns/iter  diff %
 bench_build_insert           148,610               236,960                     88,350  59.45%
 bench_from_vec               243,846               299,719                     55,873  22.91%
 bench_insert_2000_empty      4,512                 7,517                        3,005  66.60%
 bench_insert_2000_prefilled  28,665                32,605                       3,940  13.74%
 bench_pop_2000               111,515               128,005                     16,490  14.79%
 bench_pop_all                2,759,945             3,317,626                  557,681  20.21%
 peek_mut                     23,186                23,635                         449   1.94%
 pop_modify_push              41,573                43,822                       2,249   5.41%

test d_ary_heap::bench_build_insert          ... bench:     148,610 ns/iter (+/- 10,687)
test d_ary_heap::bench_from_vec              ... bench:     243,846 ns/iter (+/- 16,500)
test d_ary_heap::bench_insert_2000_empty     ... bench:       4,512 ns/iter (+/- 136)
test d_ary_heap::bench_insert_2000_prefilled ... bench:      28,665 ns/iter (+/- 1,347)
test d_ary_heap::bench_pop_2000              ... bench:     111,515 ns/iter (+/- 104,677)
test d_ary_heap::bench_pop_all               ... bench:   2,759,945 ns/iter (+/- 173,838)
test d_ary_heap::peek_mut                    ... bench:      23,186 ns/iter (+/- 106,254)
test d_ary_heap::pop_modify_push             ... bench:      41,573 ns/iter (+/- 3,313)
test std___heap::bench_build_insert          ... bench:     236,960 ns/iter (+/- 16,955)
test std___heap::bench_from_vec              ... bench:     299,719 ns/iter (+/- 6,354)
test std___heap::bench_insert_2000_empty     ... bench:       7,517 ns/iter (+/- 372)
test std___heap::bench_insert_2000_prefilled ... bench:      32,605 ns/iter (+/- 2,433)
test std___heap::bench_pop_2000              ... bench:     128,005 ns/iter (+/- 11,787)
test std___heap::bench_pop_all               ... bench:   3,317,626 ns/iter (+/- 238,968)
test std___heap::peek_mut                    ... bench:      23,635 ns/iter (+/- 1,420)
test std___heap::pop_modify_push             ... bench:      43,822 ns/iter (+/- 3,788)
```

Test code: https://github.com/arthurprs/heap-experiments
  • Loading branch information
bors authored Sep 3, 2016
2 parents a029ea3 + 175d434 commit 01b35d8
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,58 +884,61 @@ struct Hole<'a, T: 'a> {

impl<'a, T> Hole<'a, T> {
/// Create a new Hole at index `pos`.
fn new(data: &'a mut [T], pos: usize) -> Self {
unsafe {
let elt = ptr::read(&data[pos]);
Hole {
data: data,
elt: Some(elt),
pos: pos,
}
///
/// Unsafe because pos must be within the data slice.
#[inline]
unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
debug_assert!(pos < data.len());
let elt = ptr::read(&data[pos]);
Hole {
data: data,
elt: Some(elt),
pos: pos,
}
}

#[inline(always)]
#[inline]
fn pos(&self) -> usize {
self.pos
}

/// Return a reference to the element removed
#[inline(always)]
#[inline]
fn element(&self) -> &T {
self.elt.as_ref().unwrap()
}

/// Return a reference to the element at `index`.
///
/// Panics if the index is out of bounds.
///
/// Unsafe because index must not equal pos.
#[inline(always)]
/// Unsafe because index must be within the data slice and not equal to pos.
#[inline]
unsafe fn get(&self, index: usize) -> &T {
debug_assert!(index != self.pos);
&self.data[index]
debug_assert!(index < self.data.len());
self.data.get_unchecked(index)
}

/// Move hole to new location
///
/// Unsafe because index must not equal pos.
#[inline(always)]
/// Unsafe because index must be within the data slice and not equal to pos.
#[inline]
unsafe fn move_to(&mut self, index: usize) {
debug_assert!(index != self.pos);
let index_ptr: *const _ = &self.data[index];
let hole_ptr = &mut self.data[self.pos];
debug_assert!(index < self.data.len());
let index_ptr: *const _ = self.data.get_unchecked(index);
let hole_ptr = self.data.get_unchecked_mut(self.pos);
ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1);
self.pos = index;
}
}

impl<'a, T> Drop for Hole<'a, T> {
#[inline]
fn drop(&mut self) {
// fill the hole again
unsafe {
let pos = self.pos;
ptr::write(&mut self.data[pos], self.elt.take().unwrap());
ptr::write(self.data.get_unchecked_mut(pos), self.elt.take().unwrap());
}
}
}
Expand Down

0 comments on commit 01b35d8

Please sign in to comment.