Skip to content

Commit 59f9ed5

Browse files
CoAlloc: Slice: Fixing COOP_PREFERRED
1 parent 6720d1e commit 59f9ed5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

library/alloc/src/slice.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,48 @@ where
911911
}
912912
};
913913

914+
//<<<<<<< HEAD
914915
let run_alloc_fn = |len: usize| -> *mut sort::TimSortRun {
915916
// SAFETY: Creating the layout is safe as long as merge_sort never calls this with an
916917
// obscene length or 0.
917918
unsafe {
918919
alloc::alloc(alloc::Layout::array::<sort::TimSortRun>(len).unwrap_unchecked())
919920
as *mut sort::TimSortRun
921+
/*=======
922+
// Allocate a buffer to use as scratch memory. We keep the length 0 so we can keep in it
923+
// shallow copies of the contents of `v` without risking the dtors running on copies if
924+
// `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
925+
// which will always have length at most `len / 2`.
926+
// `buf` is temporary = not passed around too much => using COOP_PREFERRED=true.
927+
// @FIXME move definitions of `buf` and `runs` down, after while end > 0 {...}, just before they are used. Then benchmark if it makes (cache-related) difference.
928+
let mut buf = Vec::<T, Global, true>::with_capacity(len / 2);
929+
930+
// In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
931+
// strange decision, but consider the fact that merges more often go in the opposite direction
932+
// (forwards). According to benchmarks, merging forwards is slightly faster than merging
933+
// backwards. To conclude, identifying runs by traversing backwards improves performance.
934+
// `runs` is temporary = not passed around too much => using COOP_PREFERRED=true.
935+
let mut runs: Vec<_, Global, true> = vec![];
936+
let mut end = len;
937+
while end > 0 {
938+
// Find the next natural run, and reverse it if it's strictly descending.
939+
let mut start = end - 1;
940+
if start > 0 {
941+
start -= 1;
942+
unsafe {
943+
if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
944+
while start > 0 && is_less(v.get_unchecked(start), v.get_unchecked(start - 1)) {
945+
start -= 1;
946+
}
947+
v[start..end].reverse();
948+
} else {
949+
while start > 0 && !is_less(v.get_unchecked(start), v.get_unchecked(start - 1))
950+
{
951+
start -= 1;
952+
}
953+
}
954+
}
955+
>>>>>>> 6bd68177557 (CoAlloc: Slice: Fixing COOP_PREFERRED)*/
920956
}
921957
};
922958

0 commit comments

Comments
 (0)