Skip to content

Commit 2c816f7

Browse files
author
Stjepan Glavina
committed
Optimize insertion sort
This change slightly changes the main iteration loop so that LLVM can optimize it more efficiently. Benchmark: name before ns/iter after ns/iter diff ns/iter diff % slice::sort_unstable_small_ascending 39 (2051 MB/s) 38 (2105 MB/s) -1 -2.56% slice::sort_unstable_small_big_random 579 (2210 MB/s) 575 (2226 MB/s) -4 -0.69% slice::sort_unstable_small_descending 80 (1000 MB/s) 70 (1142 MB/s) -10 -12.50% slice::sort_unstable_small_random 396 (202 MB/s) 386 -10 -2.53%
1 parent c6df67a commit 2c816f7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/libcore/slice/sort.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ fn partial_insertion_sort<T, F>(v: &mut [T], is_less: &mut F) -> bool
152152
fn insertion_sort<T, F>(v: &mut [T], is_less: &mut F)
153153
where F: FnMut(&T, &T) -> bool
154154
{
155-
for i in 2..v.len()+1 {
156-
shift_tail(&mut v[..i], is_less);
155+
for i in 1..v.len() {
156+
shift_tail(&mut v[..i+1], is_less);
157157
}
158158
}
159159

0 commit comments

Comments
 (0)