diff --git a/src/k_smallest.rs b/src/k_smallest.rs index f5817700b..e1bb1114b 100644 --- a/src/k_smallest.rs +++ b/src/k_smallest.rs @@ -14,6 +14,7 @@ where where F: FnMut(&T, &T) -> bool, { + #[inline] fn children_of(n: usize) -> (usize, usize) { (2 * n + 1, 2 * n + 2) } @@ -47,7 +48,7 @@ where let mut is_less_than = move |a: &_, b: &_| comparator(a, b) == Ordering::Less; - // Rearrange the into a valid heap by reordering from the second-bottom-most layer up to the root + // Rearrange the storage into a valid heap by reordering from the second-bottom-most layer up to the root // Slightly faster than ordering on each insert, but only by a factor of lg(k) // The resulting heap has the **largest** item on top for i in (0..=(storage.len() / 2)).rev() { @@ -59,8 +60,7 @@ where // So feed them into the heap // Also avoids unexpected behaviour with restartable iterators iter.for_each(|val| { - // `for_each` is potentially more performant for deeply nested iterators, see its docs. - if is_less_than(&val, &mut storage[0]) { + if is_less_than(&val, &storage[0]) { // Treating this as an push-and-pop saves having to write a sift-up implementation // https://en.wikipedia.org/wiki/Binary_heap#Insert_then_extract storage[0] = val; @@ -88,6 +88,7 @@ where storage } +#[inline] pub(crate) fn key_to_cmp(key: F) -> impl Fn(&T, &T) -> Ordering where F: Fn(&T) -> K,