Skip to content

Commit

Permalink
use separate methods for forward and backward cleanup
Browse files Browse the repository at this point in the history
This reduces the amount of IR generated when post-loop cleanup is needed
  • Loading branch information
the8472 committed Apr 20, 2022
1 parent 14f829a commit ffff194
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 148 deletions.
12 changes: 6 additions & 6 deletions library/alloc/src/collections/vec_deque/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ unsafe impl<T> TrustedLen for Iter<'_, T> {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<T> TrustedRandomAccess for Iter<'_, T> {
fn cleanup(&mut self, num: usize, forward: bool) {
if forward {
let _ = self.advance_by(num);
} else {
let _ = self.advance_back_by(num);
}
fn cleanup_front(&mut self, num: usize) {
let _ = self.advance_by(num);
}

fn cleanup_back(&mut self, num: usize) {
let _ = self.advance_back_by(num);
}
}
12 changes: 6 additions & 6 deletions library/alloc/src/collections/vec_deque/iter_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ unsafe impl<T> TrustedLen for IterMut<'_, T> {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {
fn cleanup(&mut self, num: usize, forward: bool) {
if forward {
let _ = self.advance_by(num);
} else {
let _ = self.advance_back_by(num);
}
fn cleanup_front(&mut self, num: usize) {
let _ = self.advance_by(num);
}

fn cleanup_back(&mut self, num: usize) {
let _ = self.advance_back_by(num);
}
}
38 changes: 19 additions & 19 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,27 @@ unsafe impl<T, A: Allocator> TrustedRandomAccess for IntoIter<T, A>
where
T: NonDrop,
{
fn cleanup(&mut self, num: usize, forward: bool) {
if forward {
if mem::size_of::<T>() == 0 {
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
// effectively results in unsigned pointers representing positions 0..usize::MAX,
// which is valid for ZSTs.
self.ptr = unsafe { arith_offset(self.ptr as *const i8, num as isize) as *mut T }
} else {
// SAFETY: the caller must guarantee that `num` is in bounds
self.ptr = unsafe { self.ptr.add(num) };
}
fn cleanup_front(&mut self, num: usize) {
if mem::size_of::<T>() == 0 {
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
// effectively results in unsigned pointers representing positions 0..usize::MAX,
// which is valid for ZSTs.
self.ptr = unsafe { arith_offset(self.ptr as *const i8, num as isize) as *mut T }
} else {
if mem::size_of::<T>() == 0 {
// SAFETY: same as above
self.end = unsafe {
arith_offset(self.end as *const i8, num.wrapping_neg() as isize) as *mut T
}
} else {
// SAFETY: same as above
self.end = unsafe { self.end.offset(num.wrapping_neg() as isize) };
// SAFETY: the caller must guarantee that `num` is in bounds
self.ptr = unsafe { self.ptr.add(num) };
}
}

fn cleanup_back(&mut self, num: usize) {
if mem::size_of::<T>() == 0 {
// SAFETY: same as above
self.end = unsafe {
arith_offset(self.end as *const i8, num.wrapping_neg() as isize) as *mut T
}
} else {
// SAFETY: same as above
self.end = unsafe { self.end.offset(num.wrapping_neg() as isize) };
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions library/core/src/iter/adapters/cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,13 @@ where
I: TrustedRandomAccess,
{
#[inline]
fn cleanup(&mut self, num: usize, forward: bool) {
self.it.cleanup(num, forward);
fn cleanup_front(&mut self, num: usize) {
self.it.cleanup_front(num);
}

#[inline]
fn cleanup_back(&mut self, num: usize) {
self.it.cleanup_back(num);
}
}

Expand Down
9 changes: 7 additions & 2 deletions library/core/src/iter/adapters/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,13 @@ where
I: TrustedRandomAccess,
{
#[inline]
fn cleanup(&mut self, num: usize, forward: bool) {
self.it.cleanup(num, forward);
fn cleanup_front(&mut self, num: usize) {
self.it.cleanup_front(num);
}

#[inline]
fn cleanup_back(&mut self, num: usize) {
self.it.cleanup_back(num);
}
}

Expand Down
9 changes: 7 additions & 2 deletions library/core/src/iter/adapters/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ where
I: TrustedRandomAccess,
{
#[inline]
fn cleanup(&mut self, num: usize, forward: bool) {
self.iter.cleanup(num, forward);
fn cleanup_front(&mut self, num: usize) {
self.iter.cleanup_front(num);
}

#[inline]
fn cleanup_back(&mut self, num: usize) {
self.iter.cleanup_back(num);
}
}

Expand Down
11 changes: 9 additions & 2 deletions library/core/src/iter/adapters/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,16 @@ where
I: TrustedRandomAccess,
{
#[inline]
fn cleanup(&mut self, num: usize, forward: bool) {
fn cleanup_front(&mut self, num: usize) {
if let Some(iter) = self.iter.as_mut() {
iter.cleanup(num, forward);
iter.cleanup_front(num);
}
}

#[inline]
fn cleanup_back(&mut self, num: usize) {
if let Some(iter) = self.iter.as_mut() {
iter.cleanup_back(num);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions library/core/src/iter/adapters/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ where
I: TrustedRandomAccess,
{
#[inline]
fn cleanup(&mut self, num: usize, forward: bool) {
self.iter.cleanup(num, forward);
fn cleanup_front(&mut self, num: usize) {
self.iter.cleanup_front(num);
}

#[inline]
fn cleanup_back(&mut self, num: usize) {
self.iter.cleanup_back(num);
}
}

Expand Down
16 changes: 10 additions & 6 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ where
accum = f(accum, x);
}
// FIXME drop-guard or use ForLoopDesugar
self.cleanup(len, true);
self.cleanup_front(len);
accum
}

Expand All @@ -236,7 +236,7 @@ where
accum = f(accum, x)?;
}
// FIXME drop-guard or use ForLoopDesugar
self.cleanup(len, true);
self.cleanup_front(len);
try { accum }
}

Expand All @@ -262,9 +262,14 @@ where
A: TrustedRandomAccess,
B: TrustedRandomAccess,
{
fn cleanup(&mut self, num: usize, forward: bool) {
self.a.cleanup(num, forward);
self.b.cleanup(num, forward);
fn cleanup_front(&mut self, num: usize) {
self.a.cleanup_front(num);
self.b.cleanup_front(num);
}

fn cleanup_back(&mut self, num: usize) {
self.a.cleanup_back(num);
self.b.cleanup_back(num);
}
}

Expand Down Expand Up @@ -314,7 +319,6 @@ where
{
}


#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<A, B> TrustedRandomAccessNeedsReverseSetup for Zip<A, B> where Self: TrustedRandomAccess {}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/loop_desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ where
{
#[inline]
fn cleanup(&mut self) {
self.iter.cleanup(self.idx, true);
self.iter.cleanup_front(self.idx);
}
}
13 changes: 7 additions & 6 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,14 @@ macro_rules! unsafe_range_trusted_random_access_impl {
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl TrustedRandomAccess for ops::Range<$t> {

fn cleanup(&mut self, num: usize, forward: bool) {
if forward {
let _ = self.advance_by(num);
} else {
let _ = self.advance_back_by(num);
}
fn cleanup_front(&mut self, num: usize) {
let _ = self.advance_by(num);
}

fn cleanup_back(&mut self, num: usize) {
let _ = self.advance_back_by(num);
}

}
)*)
}
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/iter/traits/trusted_random_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub unsafe trait TrustedRandomAccess: Sized {
self.size_hint().0
}

fn cleanup(&mut self, num: usize, forward: bool);
fn cleanup_front(&mut self, num: usize);

fn cleanup_back(&mut self, num: usize);
}

// The following marker traits exist because specializing on them currently is the only way to avoid
Expand Down
Loading

0 comments on commit ffff194

Please sign in to comment.