Skip to content

Commit 7d882e6

Browse files
committed
Auto merge of rust-lang#123835 - saethlin:vec-from-nonnull, r=<try>
Avoid more NonNull-raw-NonNull roundtrips in Vec r? the8472 The standard library in general has a lot of these round-trips from niched types to their raw innards and back. Such round-trips have overhead in debug builds since rust-lang#120594. I removed some such round-trips in that initial PR and I've been meaning to come back and hunt down more such examples (this is the last item on rust-lang#120848).
2 parents a07f3eb + e4c567e commit 7d882e6

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

library/alloc/src/raw_vec.rs

+5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ impl<T, A: Allocator> RawVec<T, A> {
259259
Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc }
260260
}
261261

262+
pub(crate) unsafe fn from_nonnull_in(ptr: NonNull<T>, capacity: usize, alloc: A) -> Self {
263+
let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } };
264+
Self { ptr: Unique::from(ptr), cap, alloc }
265+
}
266+
262267
/// Gets a raw pointer to the start of the allocation. Note that this is
263268
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
264269
/// be careful.

library/alloc/src/vec/in_place_collect.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce};
161161
use core::marker::PhantomData;
162162
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
163163
use core::num::NonZero;
164-
use core::ptr::{self, NonNull};
164+
use core::ptr;
165165

166166
use super::{InPlaceDrop, InPlaceDstDataSrcBufDrop, SpecFromIter, SpecFromIterNested, Vec};
167167

@@ -254,28 +254,30 @@ where
254254
let (src_buf, src_ptr, src_cap, mut dst_buf, dst_end, dst_cap) = unsafe {
255255
let inner = iterator.as_inner().as_into_iter();
256256
(
257-
inner.buf.as_ptr(),
257+
inner.buf,
258258
inner.ptr,
259259
inner.cap,
260-
inner.buf.as_ptr() as *mut T,
260+
inner.buf.cast::<T>(),
261261
inner.end as *const T,
262262
inner.cap * mem::size_of::<I::Src>() / mem::size_of::<T>(),
263263
)
264264
};
265265

266266
// SAFETY: `dst_buf` and `dst_end` are the start and end of the buffer.
267-
let len = unsafe { SpecInPlaceCollect::collect_in_place(&mut iterator, dst_buf, dst_end) };
267+
let len = unsafe {
268+
SpecInPlaceCollect::collect_in_place(&mut iterator, dst_buf.as_ptr() as *mut T, dst_end)
269+
};
268270

269271
let src = unsafe { iterator.as_inner().as_into_iter() };
270272
// check if SourceIter contract was upheld
271273
// caveat: if they weren't we might not even make it to this point
272-
debug_assert_eq!(src_buf, src.buf.as_ptr());
274+
debug_assert_eq!(src_buf, src.buf);
273275
// check InPlaceIterable contract. This is only possible if the iterator advanced the
274276
// source pointer at all. If it uses unchecked access via TrustedRandomAccess
275277
// then the source pointer will stay in its initial position and we can't use it as reference
276278
if src.ptr != src_ptr {
277279
debug_assert!(
278-
unsafe { dst_buf.add(len) as *const _ } <= src.ptr.as_ptr(),
280+
unsafe { dst_buf.add(len).cast() } <= src.ptr,
279281
"InPlaceIterable contract violation, write pointer advanced beyond read pointer"
280282
);
281283
}
@@ -315,18 +317,17 @@ where
315317
let dst_size = mem::size_of::<T>().unchecked_mul(dst_cap);
316318
let new_layout = Layout::from_size_align_unchecked(dst_size, dst_align);
317319

318-
let result =
319-
alloc.shrink(NonNull::new_unchecked(dst_buf as *mut u8), old_layout, new_layout);
320+
let result = alloc.shrink(dst_buf.cast(), old_layout, new_layout);
320321
let Ok(reallocated) = result else { handle_alloc_error(new_layout) };
321-
dst_buf = reallocated.as_ptr() as *mut T;
322+
dst_buf = reallocated.cast();
322323
}
323324
} else {
324325
debug_assert_eq!(src_cap * mem::size_of::<I::Src>(), dst_cap * mem::size_of::<T>());
325326
}
326327

327328
mem::forget(dst_guard);
328329

329-
let vec = unsafe { Vec::from_raw_parts(dst_buf, len, dst_cap) };
330+
let vec = unsafe { Vec::from_nonnull(dst_buf, len, dst_cap) };
330331

331332
vec
332333
}

library/alloc/src/vec/in_place_drop.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::marker::PhantomData;
2+
use core::ptr::NonNull;
23
use core::ptr::{self, drop_in_place};
34
use core::slice::{self};
45

@@ -31,7 +32,7 @@ impl<T> Drop for InPlaceDrop<T> {
3132
// the source allocation - i.e. before the reallocation happened - to avoid leaking them
3233
// if some other destructor panics.
3334
pub(super) struct InPlaceDstDataSrcBufDrop<Src, Dest> {
34-
pub(super) ptr: *mut Dest,
35+
pub(super) ptr: NonNull<Dest>,
3536
pub(super) len: usize,
3637
pub(super) src_cap: usize,
3738
pub(super) src: PhantomData<Src>,
@@ -42,8 +43,8 @@ impl<Src, Dest> Drop for InPlaceDstDataSrcBufDrop<Src, Dest> {
4243
fn drop(&mut self) {
4344
unsafe {
4445
let _drop_allocation =
45-
RawVec::<Src>::from_raw_parts_in(self.ptr.cast::<Src>(), self.src_cap, Global);
46-
drop_in_place(core::ptr::slice_from_raw_parts_mut::<Dest>(self.ptr, self.len));
46+
RawVec::<Src>::from_nonnull_in(self.ptr.cast::<Src>(), self.src_cap, Global);
47+
drop_in_place(core::ptr::slice_from_raw_parts_mut::<Dest>(self.ptr.as_ptr(), self.len));
4748
};
4849
}
4950
}

library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
433433
// `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
434434
let alloc = ManuallyDrop::take(&mut self.0.alloc);
435435
// RawVec handles deallocation
436-
let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc);
436+
let _ = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc);
437437
}
438438
}
439439
}

library/alloc/src/vec/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ impl<T> Vec<T> {
603603
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
604604
unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) }
605605
}
606+
607+
#[inline]
608+
pub(crate) unsafe fn from_nonnull(ptr: NonNull<T>, length: usize, capacity: usize) -> Self {
609+
unsafe { Self::from_nonnull_in(ptr, length, capacity, Global) }
610+
}
606611
}
607612

608613
impl<T, A: Allocator> Vec<T, A> {
@@ -820,6 +825,16 @@ impl<T, A: Allocator> Vec<T, A> {
820825
unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } }
821826
}
822827

828+
#[inline]
829+
pub(crate) unsafe fn from_nonnull_in(
830+
ptr: NonNull<T>,
831+
length: usize,
832+
capacity: usize,
833+
alloc: A,
834+
) -> Self {
835+
unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } }
836+
}
837+
823838
/// Decomposes a `Vec<T>` into its raw components: `(pointer, length, capacity)`.
824839
///
825840
/// Returns the raw pointer to the underlying data, the length of

library/alloc/src/vec/spec_from_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T> {
5151
if has_advanced {
5252
ptr::copy(it.ptr.as_ptr(), it.buf.as_ptr(), it.len());
5353
}
54-
return Vec::from_raw_parts(it.buf.as_ptr(), it.len(), it.cap);
54+
return Vec::from_nonnull(it.buf, it.len(), it.cap);
5555
}
5656
}
5757

0 commit comments

Comments
 (0)