Skip to content

Commit bea61da

Browse files
committed
Drop bubble_sort
While using it results in slightly slammer binaries, it's not deemed worth it to add yet another sort algorithm to the standard library. select_nth_unstable has bigger binary-size problems.
1 parent 717e3aa commit bea61da

File tree

3 files changed

+3
-41
lines changed

3 files changed

+3
-41
lines changed

core/src/slice/sort/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ pub mod stable;
55
pub mod unstable;
66

77
pub(crate) mod select;
8-
#[cfg(not(feature = "optimize_for_size"))]
98
pub(crate) mod shared;

core/src/slice/sort/select.rs

+1-40
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use crate::mem::{self, SizedTypeProperties};
1010
#[cfg(not(feature = "optimize_for_size"))]
1111
use crate::slice::sort::shared::pivot::choose_pivot;
12-
#[cfg(not(feature = "optimize_for_size"))]
1312
use crate::slice::sort::shared::smallsort::insertion_sort_shift_left;
1413
use crate::slice::sort::unstable::quicksort::partition;
1514

@@ -176,13 +175,7 @@ fn median_of_medians<T, F: FnMut(&T, &T) -> bool>(mut v: &mut [T], is_less: &mut
176175
loop {
177176
if v.len() <= INSERTION_SORT_THRESHOLD {
178177
if v.len() >= 2 {
179-
cfg_if! {
180-
if #[cfg(feature = "optimize_for_size")] {
181-
bubble_sort(v, is_less);
182-
} else {
183-
insertion_sort_shift_left(v, 1, is_less);
184-
}
185-
}
178+
insertion_sort_shift_left(v, 1, is_less);
186179
}
187180

188181
return;
@@ -314,35 +307,3 @@ fn median_idx<T, F: FnMut(&T, &T) -> bool>(
314307
}
315308
b
316309
}
317-
318-
// It's possible to re-use the insertion sort in the smallsort module, but with optimize_for_size it
319-
// would clutter that module with cfg statements and make it generally harder to read and develop.
320-
// So to decouple things and simplify it, we use an even smaller bubble sort.
321-
#[cfg(feature = "optimize_for_size")]
322-
fn bubble_sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
323-
use crate::ptr;
324-
325-
let mut n = v.len();
326-
327-
let v_base = v.as_mut_ptr();
328-
329-
while n > 1 {
330-
let loop_n = n;
331-
n = 0;
332-
for i in 1..loop_n {
333-
// SAFETY: The loop construction implies that `i` and `i - 1` will always be in-bounds.
334-
unsafe {
335-
// Even if `is_less` erroneously always returns true, we are guaranteed that `n`
336-
// reduces by one each out loop iteration, because `1..n` is exclusive. This
337-
// guarantees a bounded run-time should `Ord` be implemented incorrectly.
338-
let v_i = v_base.add(i);
339-
let v_i_minus_one = v_base.add(i - 1);
340-
341-
if is_less(&*v_i, &*v_i_minus_one) {
342-
ptr::swap_nonoverlapping(v_i, v_i_minus_one, 1);
343-
n = i;
344-
}
345-
}
346-
}
347-
}
348-
}

core/src/slice/sort/shared/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(feature = "optimize_for_size", allow(dead_code))]
2+
13
use crate::marker::Freeze;
24

35
pub(crate) mod pivot;

0 commit comments

Comments
 (0)