Add the inner for loop back in the logic

This could be easier to predict in cases where one of `left` and `right`
is significantly greater than the other, and as such the same branch is
taken multiple times in a row
This commit is contained in:
Andrea Piseri
2022-12-21 22:10:02 +01:00
parent 191223bb3c
commit 3fa971a510
+16 -6
View File
@@ -75,15 +75,25 @@ ptr_rotate :: proc(left: int, mid: ^$T, right: int) {
// TODO(bill): Optimization with a buffer for smaller ranges
for left > 0 && right > 0 {
if left >= right {
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right * size_of(T))
mid = ptr_sub(mid, right)
for {
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right * size_of(T))
mid = ptr_sub(mid, right)
left -= right
left -= right
if left < right {
break
}
}
} else {
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left * size_of(T))
mid = ptr_add(mid, left)
for {
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left * size_of(T))
mid = ptr_add(mid, left)
right -= left
right -= left
if right < left {
break
}
}
}
}
}