Skip to content

Commit 1a84ad6

Browse files
CoAlloc: Vec, Box + related slice.rs: tidy
1 parent bc9bdc8 commit 1a84ad6

File tree

11 files changed

+99
-130
lines changed

11 files changed

+99
-130
lines changed

library/alloc/src/boxed.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,10 @@ where
780780
#[allow(unused_braces)]
781781
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
782782
where
783-
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
784-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
783+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
784+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
785785
{
786-
unsafe {
787-
RawVec::<T, A, false>::with_capacity_in(len, alloc)
788-
.into_box(len)
789-
}
786+
unsafe { RawVec::<T, A, false>::with_capacity_in(len, alloc).into_box(len) }
790787
}
791788

792789
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -816,15 +813,10 @@ where
816813
#[allow(unused_braces)]
817814
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
818815
where
819-
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
820-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
816+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
817+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
821818
{
822-
unsafe {
823-
RawVec::<T, A, false>::with_capacity_zeroed_in(
824-
len, alloc,
825-
)
826-
.into_box(len)
827-
}
819+
unsafe { RawVec::<T, A, false>::with_capacity_zeroed_in(len, alloc).into_box(len) }
828820
}
829821
}
830822

library/alloc/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,3 @@ pub(crate) mod test_helpers {
287287
rand::SeedableRng::from_seed(seed)
288288
}
289289
}
290-

library/alloc/src/raw_vec.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ enum AllocInit {
5454
pub(crate) struct RawVec<
5555
T,
5656
A: Allocator = Global,
57-
const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()},
57+
const COOP_PREFERRED: bool = { DEFAULT_COOP_PREFERRED!() },
5858
> where
5959
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
6060
{
@@ -223,9 +223,7 @@ where
223223
cap: capacity,
224224
alloc,
225225
metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/};
226-
alloc::co_alloc_metadata_num_slots_with_preference::<A>(
227-
COOP_PREFERRED,
228-
)],
226+
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)],
229227
}
230228
}
231229
}

library/alloc/src/slice.rs

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ pub(crate) mod hack {
101101
b: Box<[T], A>,
102102
) -> Vec<T, A, COOP_PREFERRED>
103103
where
104-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
105-
COOP_PREFERRED,
106-
)]:,
104+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
107105
{
108106
unsafe {
109107
let len = b.len();
@@ -119,9 +117,7 @@ pub(crate) mod hack {
119117
alloc: A,
120118
) -> Vec<T, A, COOP_PREFERRED>
121119
where
122-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
123-
COOP_PREFERRED,
124-
)]:,
120+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
125121
{
126122
T::to_vec(s, alloc)
127123
}
@@ -134,9 +130,7 @@ pub(crate) mod hack {
134130
) -> Vec<Self, A, COOP_PREFERRED>
135131
where
136132
Self: Sized,
137-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
138-
COOP_PREFERRED,
139-
)]:;
133+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:;
140134
}
141135

142136
#[cfg(not(no_global_oom_handling))]
@@ -147,24 +141,18 @@ pub(crate) mod hack {
147141
alloc: A,
148142
) -> Vec<Self, A, COOP_PREFERRED>
149143
where
150-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
151-
COOP_PREFERRED,
152-
)]:,
144+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
153145
{
154146
struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>
155147
where
156-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
157-
COOP_PREFERRED,
158-
)]:,
148+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
159149
{
160150
vec: &'a mut Vec<T, A, COOP_PREFERRED>,
161151
num_init: usize,
162152
}
163153
impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED>
164154
where
165-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
166-
COOP_PREFERRED,
167-
)]:,
155+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
168156
{
169157
#[inline]
170158
fn drop(&mut self) {
@@ -202,9 +190,7 @@ pub(crate) mod hack {
202190
alloc: A,
203191
) -> Vec<Self, A, COOP_PREFERRED>
204192
where
205-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
206-
COOP_PREFERRED,
207-
)]:,
193+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
208194
{
209195
let mut v = Vec::with_capacity_in(s.len(), alloc);
210196
// SAFETY:
@@ -459,9 +445,7 @@ impl<T> [T] {
459445
pub fn to_vec<const COOP_PREFERRED: bool>(&self) -> Vec<T, Global, COOP_PREFERRED>
460446
where
461447
T: Clone,
462-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(
463-
COOP_PREFERRED,
464-
)]:,
448+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
465449
{
466450
self.to_vec_in::<Global, COOP_PREFERRED>(Global)
467451
}
@@ -489,9 +473,7 @@ impl<T> [T] {
489473
) -> Vec<T, A, COOP_PREFERRED>
490474
where
491475
T: Clone,
492-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
493-
COOP_PREFERRED,
494-
)]:,
476+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
495477
{
496478
// N.B., see the `hack` module in this file for more details.
497479
hack::to_vec(self, alloc)
@@ -518,9 +500,7 @@ impl<T> [T] {
518500
self: Box<Self, A>,
519501
) -> Vec<T, A, COOP_PREFERRED>
520502
where
521-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(
522-
COOP_PREFERRED,
523-
)]:,
503+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
524504
{
525505
// N.B., see the `hack` module in this file for more details.
526506
hack::into_vec(self)
@@ -914,48 +894,48 @@ where
914894
}
915895
};
916896

917-
//<<<<<<< HEAD
897+
//<<<<<<< HEAD
918898
let run_alloc_fn = |len: usize| -> *mut sort::TimSortRun {
919899
// SAFETY: Creating the layout is safe as long as merge_sort never calls this with an
920900
// obscene length or 0.
921901
unsafe {
922902
alloc::alloc(alloc::Layout::array::<sort::TimSortRun>(len).unwrap_unchecked())
923903
as *mut sort::TimSortRun
924-
/*=======
925-
// Allocate a buffer to use as scratch memory. We keep the length 0 so we can keep in it
926-
// shallow copies of the contents of `v` without risking the dtors running on copies if
927-
// `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
928-
// which will always have length at most `len / 2`.
929-
// `buf` is temporary = not passed around too much => using COOP_PREFERRED=true.
930-
// @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.
931-
let mut buf = Vec::<T, Global, true>::with_capacity(len / 2);
932-
933-
// In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
934-
// strange decision, but consider the fact that merges more often go in the opposite direction
935-
// (forwards). According to benchmarks, merging forwards is slightly faster than merging
936-
// backwards. To conclude, identifying runs by traversing backwards improves performance.
937-
// `runs` is temporary = not passed around too much => using COOP_PREFERRED=true.
938-
let mut runs: Vec<_, Global, true> = vec![];
939-
let mut end = len;
940-
while end > 0 {
941-
// Find the next natural run, and reverse it if it's strictly descending.
942-
let mut start = end - 1;
943-
if start > 0 {
944-
start -= 1;
945-
unsafe {
946-
if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
947-
while start > 0 && is_less(v.get_unchecked(start), v.get_unchecked(start - 1)) {
948-
start -= 1;
949-
}
950-
v[start..end].reverse();
951-
} else {
952-
while start > 0 && !is_less(v.get_unchecked(start), v.get_unchecked(start - 1))
953-
{
904+
/*=======
905+
// Allocate a buffer to use as scratch memory. We keep the length 0 so we can keep in it
906+
// shallow copies of the contents of `v` without risking the dtors running on copies if
907+
// `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
908+
// which will always have length at most `len / 2`.
909+
// `buf` is temporary = not passed around too much => using COOP_PREFERRED=true.
910+
// @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.
911+
let mut buf = Vec::<T, Global, true>::with_capacity(len / 2);
912+
913+
// In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
914+
// strange decision, but consider the fact that merges more often go in the opposite direction
915+
// (forwards). According to benchmarks, merging forwards is slightly faster than merging
916+
// backwards. To conclude, identifying runs by traversing backwards improves performance.
917+
// `runs` is temporary = not passed around too much => using COOP_PREFERRED=true.
918+
let mut runs: Vec<_, Global, true> = vec![];
919+
let mut end = len;
920+
while end > 0 {
921+
// Find the next natural run, and reverse it if it's strictly descending.
922+
let mut start = end - 1;
923+
if start > 0 {
954924
start -= 1;
955-
}
956-
}
957-
}
958-
>>>>>>> 6bd68177557 (CoAlloc: Slice: Fixing COOP_PREFERRED)*/
925+
unsafe {
926+
if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
927+
while start > 0 && is_less(v.get_unchecked(start), v.get_unchecked(start - 1)) {
928+
start -= 1;
929+
}
930+
v[start..end].reverse();
931+
} else {
932+
while start > 0 && !is_less(v.get_unchecked(start), v.get_unchecked(start - 1))
933+
{
934+
start -= 1;
935+
}
936+
}
937+
}
938+
>>>>>>> 6bd68177557 (CoAlloc: Slice: Fixing COOP_PREFERRED)*/
959939
}
960940
};
961941

library/alloc/src/vec/in_place_collect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@
137137
//! }
138138
//! vec.truncate(write_idx);
139139
//! ```
140-
use core::alloc;
141140
use crate::alloc::Global;
141+
use core::alloc;
142142
use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce};
143143
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
144144
use core::ptr::{self};
@@ -157,7 +157,6 @@ impl<T, I, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, Global, COO
157157
where
158158
I: Iterator<Item = T> + SourceIter<Source: AsVecIntoIter> + InPlaceIterableMarker,
159159
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
160-
161160
{
162161
default fn from_iter(mut iterator: I) -> Self {
163162
// See "Layout constraints" section in the module documentation. We rely on const

library/alloc/src/vec/in_place_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::alloc::Global;
12
use core::ptr::{self};
23
use core::slice::{self};
3-
use crate::alloc::Global;
44

55
// A helper struct for in-place iteration that drops the destination slice of iteration,
66
// i.e. the head. The source slice (the tail) is dropped by IntoIter.

library/alloc/src/vec/into_iter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ where
433433
fn clone(&self) -> Self {
434434
// @FIXME Remove the following extras - used for type checks only
435435
let slice = self.as_slice();
436-
let vec: crate::vec::Vec<T, A, COOP_PREFERRED> = slice.to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone());
436+
let vec: crate::vec::Vec<T, A, COOP_PREFERRED> =
437+
slice.to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone());
437438
let _iter: IntoIter<T, A, COOP_PREFERRED> = vec.into_iter();
438439

439440
//self.as_slice().to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone()).into_iter()

0 commit comments

Comments
 (0)