Skip to content

Commit 3ee8933

Browse files
committed
Remove RawVec::capacity_from_bytes and RawVec::set_ptr.
It reduces generated code size a bit more.
1 parent 7b820b0 commit 3ee8933

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<T, A: Allocator> RawVec<T, A> {
178178

179179
Self {
180180
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
181-
cap: Self::capacity_from_bytes(ptr.len()),
181+
cap: ptr.len() / mem::size_of::<T>(),
182182
alloc,
183183
}
184184
}
@@ -346,16 +346,6 @@ impl<T, A: Allocator> RawVec<T, A> {
346346
additional > self.capacity().wrapping_sub(len)
347347
}
348348

349-
fn capacity_from_bytes(excess: usize) -> usize {
350-
debug_assert_ne!(mem::size_of::<T>(), 0);
351-
excess / mem::size_of::<T>()
352-
}
353-
354-
fn set_ptr(&mut self, ptr: NonNull<[u8]>) {
355-
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
356-
self.cap = Self::capacity_from_bytes(ptr.len());
357-
}
358-
359349
// This method must only be called after `needs_to_grow(len, additional)`
360350
// succeeds. Otherwise, if `T` is zero-sized it will cause a divide by
361351
// zero.
@@ -370,15 +360,16 @@ impl<T, A: Allocator> RawVec<T, A> {
370360
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
371361
// `finish_grow_amortized` is non-generic over `T`.
372362
let elem_layout = Layout::new::<T>();
373-
let ptr = finish_grow_amortized(
363+
let (ptr, cap) = finish_grow_amortized(
374364
len,
375365
additional,
376366
elem_layout,
377367
self.cap,
378368
self.current_memory(),
379369
&mut self.alloc,
380370
)?;
381-
self.set_ptr(ptr);
371+
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
372+
self.cap = cap;
382373
Ok(())
383374
}
384375

@@ -392,14 +383,15 @@ impl<T, A: Allocator> RawVec<T, A> {
392383
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
393384
// `finish_grow_exact` is non-generic over `T`.
394385
let elem_layout = Layout::new::<T>();
395-
let ptr = finish_grow_exact(
386+
let (ptr, cap) = finish_grow_exact(
396387
len,
397388
additional,
398389
elem_layout,
399390
self.current_memory(),
400391
&mut self.alloc,
401392
)?;
402-
self.set_ptr(ptr);
393+
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
394+
self.cap = cap;
403395
Ok(())
404396
}
405397

@@ -415,7 +407,8 @@ impl<T, A: Allocator> RawVec<T, A> {
415407
.shrink(ptr, layout, new_layout)
416408
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
417409
};
418-
self.set_ptr(ptr);
410+
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
411+
self.cap = ptr.len() / mem::size_of::<T>();
419412
Ok(())
420413
}
421414
}
@@ -432,7 +425,7 @@ fn finish_grow_amortized<A>(
432425
current_cap: usize,
433426
current_memory: Option<(NonNull<u8>, Layout)>,
434427
alloc: &mut A,
435-
) -> Result<NonNull<[u8]>, TryReserveError>
428+
) -> Result<(NonNull<[u8]>, usize), TryReserveError>
436429
where
437430
A: Allocator,
438431
{
@@ -467,18 +460,20 @@ where
467460

468461
let new_layout = unsafe { Layout::from_size_align_unchecked(array_size, elem_layout.align()) };
469462

470-
let memory = if let Some((ptr, old_layout)) = current_memory {
463+
let new_ptr = if let Some((old_ptr, old_layout)) = current_memory {
471464
debug_assert_eq!(old_layout.align(), new_layout.align());
472465
unsafe {
473466
// The allocator checks for alignment equality
474467
intrinsics::assume(old_layout.align() == new_layout.align());
475-
alloc.grow(ptr, old_layout, new_layout)
468+
alloc.grow(old_ptr, old_layout, new_layout)
476469
}
477470
} else {
478471
alloc.allocate(new_layout)
479-
};
472+
}
473+
.map_err(|_| TryReserveError::from(AllocError { layout: new_layout, non_exhaustive: () }))?;
480474

481-
memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
475+
let new_cap = new_ptr.len() / elem_layout.size();
476+
Ok((new_ptr, new_cap))
482477
}
483478

484479
// This function is outside `RawVec` to minimize compile times. See the comment
@@ -492,7 +487,7 @@ fn finish_grow_exact<A>(
492487
elem_layout: Layout,
493488
current_memory: Option<(NonNull<u8>, Layout)>,
494489
alloc: &mut A,
495-
) -> Result<NonNull<[u8]>, TryReserveError>
490+
) -> Result<(NonNull<[u8]>, usize), TryReserveError>
496491
where
497492
A: Allocator,
498493
{
@@ -509,18 +504,20 @@ where
509504

510505
let new_layout = unsafe { Layout::from_size_align_unchecked(array_size, elem_layout.align()) };
511506

512-
let memory = if let Some((ptr, old_layout)) = current_memory {
507+
let new_ptr = if let Some((old_ptr, old_layout)) = current_memory {
513508
debug_assert_eq!(old_layout.align(), new_layout.align());
514509
unsafe {
515510
// The allocator checks for alignment equality
516511
intrinsics::assume(old_layout.align() == new_layout.align());
517-
alloc.grow(ptr, old_layout, new_layout)
512+
alloc.grow(old_ptr, old_layout, new_layout)
518513
}
519514
} else {
520515
alloc.allocate(new_layout)
521-
};
516+
}
517+
.map_err(|_| TryReserveError::from(AllocError { layout: new_layout, non_exhaustive: () }))?;
522518

523-
memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
519+
let new_cap = new_ptr.len() / elem_layout.size();
520+
Ok((new_ptr, new_cap))
524521
}
525522

526523
unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {

0 commit comments

Comments
 (0)