Skip to content

Commit 7e0ff8b

Browse files
committed
Adding short-circuit if there's no children, to save a compare
1 parent 58c0362 commit 7e0ff8b

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/k_smallest.rs

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ where
152152
let (left_idx, right_idx) = children_of(origin);
153153
let (left_item, right_item) = (self.get(left_idx), self.get(right_idx));
154154

155+
if left_item.is_none() { // the left is the earlier child, so if it doesn't exist there's nothing to swap with
156+
return;
157+
}
158+
155159
let cmp = self
156160
.compare(left_item, right_item)
157161
.unwrap_or(Ordering::Greater); // The right item may not exist, so default to picking the left, i.e. lower

0 commit comments

Comments
 (0)