Skip to content

Commit 79da205

Browse files
committed
Inlining and comment tweaks
1 parent 5169650 commit 79da205

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/k_smallest.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ where
1414
where
1515
F: FnMut(&T, &T) -> bool,
1616
{
17+
#[inline]
1718
fn children_of(n: usize) -> (usize, usize) {
1819
(2 * n + 1, 2 * n + 2)
1920
}
@@ -47,7 +48,7 @@ where
4748

4849
let mut is_less_than = move |a: &_, b: &_| comparator(a, b) == Ordering::Less;
4950

50-
// Rearrange the into a valid heap by reordering from the second-bottom-most layer up to the root
51+
// Rearrange the storage into a valid heap by reordering from the second-bottom-most layer up to the root
5152
// Slightly faster than ordering on each insert, but only by a factor of lg(k)
5253
// The resulting heap has the **largest** item on top
5354
for i in (0..=(storage.len() / 2)).rev() {
@@ -59,8 +60,7 @@ where
5960
// So feed them into the heap
6061
// Also avoids unexpected behaviour with restartable iterators
6162
iter.for_each(|val| {
62-
// `for_each` is potentially more performant for deeply nested iterators, see its docs.
63-
if is_less_than(&val, &mut storage[0]) {
63+
if is_less_than(&val, &storage[0]) {
6464
// Treating this as an push-and-pop saves having to write a sift-up implementation
6565
// https://en.wikipedia.org/wiki/Binary_heap#Insert_then_extract
6666
storage[0] = val;
@@ -88,6 +88,7 @@ where
8888
storage
8989
}
9090

91+
#[inline]
9192
pub(crate) fn key_to_cmp<T, K, F>(key: F) -> impl Fn(&T, &T) -> Ordering
9293
where
9394
F: Fn(&T) -> K,

0 commit comments

Comments
 (0)