1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#![deny(unsafe_op_in_unsafe_fn)]
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
#![cfg_attr(feature = "unstable", allow(incomplete_features))]
#![cfg_attr(feature = "unstable", feature(specialization, marker_trait_attr))]
#![allow(clippy::needless_lifetimes)] #![allow(clippy::type_complexity)] #![allow(clippy::unnecessary_mut_passed)] #![allow(clippy::forget_non_drop)] const MAX_STACK_SCRATCH_SIZE_BYTES: usize = 4096;
const FULL_ALLOC_MAX_BYTES: usize = 1024 * 1024;
const HALF_ALLOC_MAX_BYTES: usize = 1024 * 1024 * 1024;
const MERGE_SPLIT_THRESHOLD: usize = 32;
const PSEUDO_MEDIAN_REC_THRESHOLD: usize = 64;
const SMALL_SORT: usize = 48;
#[cfg(not(feature = "tracking"))]
mod tracking;
#[cfg(feature = "tracking")]
pub mod tracking;
mod branchless_merge;
mod gap_guard;
mod glidesort;
mod merge_reduction;
mod mut_slice;
mod physical_merges;
mod pivot_selection;
mod powersort;
mod small_sort;
mod stable_quicksort;
mod util;
use core::cmp::Ordering;
use core::mem::{ManuallyDrop, MaybeUninit};
use util::*;
use crate::mut_slice::states::AlwaysInit;
use crate::mut_slice::{Brand, MutSlice};
fn glidesort_alloc_size<T>(n: usize) -> usize {
let tlen = core::mem::size_of::<T>();
let full_allowed = n.min(FULL_ALLOC_MAX_BYTES / tlen);
let half_allowed = (n / 2).min(HALF_ALLOC_MAX_BYTES / tlen);
let eighth_allowed = n / 8;
full_allowed
.max(half_allowed)
.max(eighth_allowed)
.max(SMALL_SORT)
}
pub fn sort<T: Ord>(v: &mut [T]) {
sort_with_vec_by(v, &mut Vec::new(), |a, b| a.cmp(b))
}
pub fn sort_by_key<T, F: FnMut(&T) -> K, K: Ord>(v: &mut [T], mut f: F) {
sort_with_vec_by(v, &mut Vec::new(), |a, b| f(a).cmp(&f(b)))
}
pub fn sort_by<T, F>(v: &mut [T], compare: F)
where
F: FnMut(&T, &T) -> Ordering,
{
sort_with_vec_by(v, &mut Vec::new(), compare)
}
pub fn sort_with_vec<T: Ord>(v: &mut [T], scratch_buf: &mut Vec<T>) {
sort_with_vec_by(v, scratch_buf, |a, b| a.cmp(b))
}
pub fn sort_with_vec_by_key<T, F: FnMut(&T) -> K, K: Ord>(
v: &mut [T],
scratch_buf: &mut Vec<T>,
mut f: F,
) {
sort_with_vec_by(v, scratch_buf, |a, b| f(a).cmp(&f(b)))
}
pub fn sort_with_vec_by<T, F>(v: &mut [T], scratch_buf: &mut Vec<T>, mut compare: F)
where
F: FnMut(&T, &T) -> Ordering,
{
if core::mem::size_of::<T>() == 0 {
return;
}
let mut is_less = cmp_from_closure(|a, b| {
tracking::register_cmp(a, b);
compare(a, b) == Ordering::Less
});
let n = v.len();
MutSlice::from_mut_slice(v, |el| {
if n < SMALL_SORT {
return small_sort::small_sort(el, &mut is_less);
}
let stack_buffer_cap = MAX_STACK_SCRATCH_SIZE_BYTES / core::mem::size_of::<T>();
if stack_buffer_cap >= n / 2 {
return glidesort_with_max_stack_scratch(el, &mut is_less);
}
let (_, buffer) = make_scratch_after_vec(scratch_buf, glidesort_alloc_size::<T>(n));
MutSlice::from_maybeuninit_mut_slice(buffer, |scratch| {
glidesort::glidesort(el, scratch.assume_uninit(), &mut is_less, false)
})
})
}
pub fn sort_with_buffer<T: Ord>(v: &mut [T], buffer: &mut [MaybeUninit<T>]) {
sort_with_buffer_by(v, buffer, |a, b| a.cmp(b))
}
pub fn sort_with_buffer_by_key<T, F: FnMut(&T) -> K, K: Ord>(
v: &mut [T],
buffer: &mut [MaybeUninit<T>],
mut f: F,
) {
sort_with_buffer_by(v, buffer, |a, b| f(a).cmp(&f(b)))
}
pub fn sort_with_buffer_by<T, F>(v: &mut [T], buffer: &mut [MaybeUninit<T>], mut compare: F)
where
F: FnMut(&T, &T) -> Ordering,
{
if core::mem::size_of::<T>() == 0 {
return;
}
let mut is_less = cmp_from_closure(|a, b| {
tracking::register_cmp(a, b);
compare(a, b) == Ordering::Less
});
let n = v.len();
MutSlice::from_mut_slice(v, |el| {
if n < SMALL_SORT {
return small_sort::small_sort(el, &mut is_less);
}
MutSlice::from_maybeuninit_mut_slice(buffer, |scratch| {
glidesort::glidesort(el, scratch.assume_uninit(), &mut is_less, false)
})
})
}
pub fn sort_in_vec<T: Ord>(v: &mut Vec<T>) {
sort_in_vec_by(v, |a, b| a.cmp(b))
}
pub fn sort_in_vec_by_key<T, F: FnMut(&T) -> K, K: Ord>(v: &mut Vec<T>, mut f: F) {
sort_in_vec_by(v, |a, b| f(a).cmp(&f(b)))
}
pub fn sort_in_vec_by<T, F>(v: &mut Vec<T>, mut compare: F)
where
F: FnMut(&T, &T) -> Ordering,
{
if core::mem::size_of::<T>() == 0 {
return;
}
let mut is_less = cmp_from_closure(|a, b| {
tracking::register_cmp(a, b);
compare(a, b) == Ordering::Less
});
let n = v.len();
if n < SMALL_SORT {
return MutSlice::from_mut_slice(v, |el| small_sort::small_sort(el, &mut is_less));
}
let stack_buffer_cap = MAX_STACK_SCRATCH_SIZE_BYTES / core::mem::size_of::<T>();
if stack_buffer_cap >= n / 2 {
return MutSlice::from_mut_slice(v, |el| {
glidesort_with_max_stack_scratch(el, &mut is_less)
});
}
let (el, buffer) = make_scratch_after_vec(v, glidesort_alloc_size::<T>(n));
MutSlice::from_mut_slice(el, |el| {
MutSlice::from_maybeuninit_mut_slice(buffer, |scratch| {
glidesort::glidesort(el, scratch.assume_uninit(), &mut is_less, false)
})
})
}
fn make_scratch_after_vec<T>(
buffer: &mut Vec<T>,
mut target_size: usize,
) -> (&mut [T], &mut [MaybeUninit<T>]) {
let free_capacity = buffer.capacity() - buffer.len();
if free_capacity / 2 < target_size || free_capacity < SMALL_SORT {
while buffer.try_reserve(target_size).is_err() {
target_size /= 8;
if target_size == 0 {
return (&mut buffer[..], &mut []);
}
}
}
split_at_spare_mut(buffer)
}
#[repr(C)]
union AlignedBuffer<T, const N: usize> {
buffer: [MaybeUninit<u8>; N],
_dummy_for_alignment: ManuallyDrop<MaybeUninit<T>>,
}
#[inline(never)]
#[cold]
fn glidesort_with_max_stack_scratch<'l, B: Brand, T, F: Cmp<T>>(
el: MutSlice<'l, B, T, AlwaysInit>,
is_less: &mut F,
) {
unsafe {
#[allow(clippy::uninit_assumed_init)]
let mut aligned_buffer: AlignedBuffer<T, MAX_STACK_SCRATCH_SIZE_BYTES> =
MaybeUninit::uninit().assume_init();
let aligned_buffer_bytes = aligned_buffer.buffer.as_mut_slice();
let max_elements = MAX_STACK_SCRATCH_SIZE_BYTES / core::mem::size_of::<T>();
let buffer = core::slice::from_raw_parts_mut(
aligned_buffer_bytes.as_mut_ptr().cast::<MaybeUninit<T>>(),
max_elements,
);
MutSlice::from_maybeuninit_mut_slice(buffer, |scratch| {
glidesort::glidesort(el, scratch.assume_uninit(), is_less, false)
})
}
}