Skip to content

Commit ffff194

Browse files
committed
use separate methods for forward and backward cleanup
This reduces the amount of IR generated when post-loop cleanup is needed
1 parent 14f829a commit ffff194

File tree

14 files changed

+201
-148
lines changed

14 files changed

+201
-148
lines changed

library/alloc/src/collections/vec_deque/iter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ unsafe impl<T> TrustedLen for Iter<'_, T> {}
206206
#[doc(hidden)]
207207
#[unstable(feature = "trusted_random_access", issue = "none")]
208208
unsafe impl<T> TrustedRandomAccess for Iter<'_, T> {
209-
fn cleanup(&mut self, num: usize, forward: bool) {
210-
if forward {
211-
let _ = self.advance_by(num);
212-
} else {
213-
let _ = self.advance_back_by(num);
214-
}
209+
fn cleanup_front(&mut self, num: usize) {
210+
let _ = self.advance_by(num);
211+
}
212+
213+
fn cleanup_back(&mut self, num: usize) {
214+
let _ = self.advance_back_by(num);
215215
}
216216
}

library/alloc/src/collections/vec_deque/iter_mut.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ unsafe impl<T> TrustedLen for IterMut<'_, T> {}
155155
#[doc(hidden)]
156156
#[unstable(feature = "trusted_random_access", issue = "none")]
157157
unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {
158-
fn cleanup(&mut self, num: usize, forward: bool) {
159-
if forward {
160-
let _ = self.advance_by(num);
161-
} else {
162-
let _ = self.advance_back_by(num);
163-
}
158+
fn cleanup_front(&mut self, num: usize) {
159+
let _ = self.advance_by(num);
160+
}
161+
162+
fn cleanup_back(&mut self, num: usize) {
163+
let _ = self.advance_back_by(num);
164164
}
165165
}

library/alloc/src/vec/into_iter.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,27 @@ unsafe impl<T, A: Allocator> TrustedRandomAccess for IntoIter<T, A>
289289
where
290290
T: NonDrop,
291291
{
292-
fn cleanup(&mut self, num: usize, forward: bool) {
293-
if forward {
294-
if mem::size_of::<T>() == 0 {
295-
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
296-
// effectively results in unsigned pointers representing positions 0..usize::MAX,
297-
// which is valid for ZSTs.
298-
self.ptr = unsafe { arith_offset(self.ptr as *const i8, num as isize) as *mut T }
299-
} else {
300-
// SAFETY: the caller must guarantee that `num` is in bounds
301-
self.ptr = unsafe { self.ptr.add(num) };
302-
}
292+
fn cleanup_front(&mut self, num: usize) {
293+
if mem::size_of::<T>() == 0 {
294+
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
295+
// effectively results in unsigned pointers representing positions 0..usize::MAX,
296+
// which is valid for ZSTs.
297+
self.ptr = unsafe { arith_offset(self.ptr as *const i8, num as isize) as *mut T }
303298
} else {
304-
if mem::size_of::<T>() == 0 {
305-
// SAFETY: same as above
306-
self.end = unsafe {
307-
arith_offset(self.end as *const i8, num.wrapping_neg() as isize) as *mut T
308-
}
309-
} else {
310-
// SAFETY: same as above
311-
self.end = unsafe { self.end.offset(num.wrapping_neg() as isize) };
299+
// SAFETY: the caller must guarantee that `num` is in bounds
300+
self.ptr = unsafe { self.ptr.add(num) };
301+
}
302+
}
303+
304+
fn cleanup_back(&mut self, num: usize) {
305+
if mem::size_of::<T>() == 0 {
306+
// SAFETY: same as above
307+
self.end = unsafe {
308+
arith_offset(self.end as *const i8, num.wrapping_neg() as isize) as *mut T
312309
}
310+
} else {
311+
// SAFETY: same as above
312+
self.end = unsafe { self.end.offset(num.wrapping_neg() as isize) };
313313
}
314314
}
315315
}

library/core/src/iter/adapters/cloned.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,13 @@ where
130130
I: TrustedRandomAccess,
131131
{
132132
#[inline]
133-
fn cleanup(&mut self, num: usize, forward: bool) {
134-
self.it.cleanup(num, forward);
133+
fn cleanup_front(&mut self, num: usize) {
134+
self.it.cleanup_front(num);
135+
}
136+
137+
#[inline]
138+
fn cleanup_back(&mut self, num: usize) {
139+
self.it.cleanup_back(num);
135140
}
136141
}
137142

library/core/src/iter/adapters/copied.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,13 @@ where
156156
I: TrustedRandomAccess,
157157
{
158158
#[inline]
159-
fn cleanup(&mut self, num: usize, forward: bool) {
160-
self.it.cleanup(num, forward);
159+
fn cleanup_front(&mut self, num: usize) {
160+
self.it.cleanup_front(num);
161+
}
162+
163+
#[inline]
164+
fn cleanup_back(&mut self, num: usize) {
165+
self.it.cleanup_back(num);
161166
}
162167
}
163168

library/core/src/iter/adapters/enumerate.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,13 @@ where
240240
I: TrustedRandomAccess,
241241
{
242242
#[inline]
243-
fn cleanup(&mut self, num: usize, forward: bool) {
244-
self.iter.cleanup(num, forward);
243+
fn cleanup_front(&mut self, num: usize) {
244+
self.iter.cleanup_front(num);
245+
}
246+
247+
#[inline]
248+
fn cleanup_back(&mut self, num: usize) {
249+
self.iter.cleanup_back(num);
245250
}
246251
}
247252

library/core/src/iter/adapters/fuse.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,16 @@ where
228228
I: TrustedRandomAccess,
229229
{
230230
#[inline]
231-
fn cleanup(&mut self, num: usize, forward: bool) {
231+
fn cleanup_front(&mut self, num: usize) {
232232
if let Some(iter) = self.iter.as_mut() {
233-
iter.cleanup(num, forward);
233+
iter.cleanup_front(num);
234+
}
235+
}
236+
237+
#[inline]
238+
fn cleanup_back(&mut self, num: usize) {
239+
if let Some(iter) = self.iter.as_mut() {
240+
iter.cleanup_back(num);
234241
}
235242
}
236243
}

library/core/src/iter/adapters/map.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,13 @@ where
198198
I: TrustedRandomAccess,
199199
{
200200
#[inline]
201-
fn cleanup(&mut self, num: usize, forward: bool) {
202-
self.iter.cleanup(num, forward);
201+
fn cleanup_front(&mut self, num: usize) {
202+
self.iter.cleanup_front(num);
203+
}
204+
205+
#[inline]
206+
fn cleanup_back(&mut self, num: usize) {
207+
self.iter.cleanup_back(num);
203208
}
204209
}
205210

library/core/src/iter/adapters/zip.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ where
217217
accum = f(accum, x);
218218
}
219219
// FIXME drop-guard or use ForLoopDesugar
220-
self.cleanup(len, true);
220+
self.cleanup_front(len);
221221
accum
222222
}
223223

@@ -236,7 +236,7 @@ where
236236
accum = f(accum, x)?;
237237
}
238238
// FIXME drop-guard or use ForLoopDesugar
239-
self.cleanup(len, true);
239+
self.cleanup_front(len);
240240
try { accum }
241241
}
242242

@@ -262,9 +262,14 @@ where
262262
A: TrustedRandomAccess,
263263
B: TrustedRandomAccess,
264264
{
265-
fn cleanup(&mut self, num: usize, forward: bool) {
266-
self.a.cleanup(num, forward);
267-
self.b.cleanup(num, forward);
265+
fn cleanup_front(&mut self, num: usize) {
266+
self.a.cleanup_front(num);
267+
self.b.cleanup_front(num);
268+
}
269+
270+
fn cleanup_back(&mut self, num: usize) {
271+
self.a.cleanup_back(num);
272+
self.b.cleanup_back(num);
268273
}
269274
}
270275

@@ -314,7 +319,6 @@ where
314319
{
315320
}
316321

317-
318322
#[doc(hidden)]
319323
#[unstable(feature = "trusted_random_access", issue = "none")]
320324
unsafe impl<A, B> TrustedRandomAccessNeedsReverseSetup for Zip<A, B> where Self: TrustedRandomAccess {}

library/core/src/iter/loop_desugar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ where
110110
{
111111
#[inline]
112112
fn cleanup(&mut self) {
113-
self.iter.cleanup(self.idx, true);
113+
self.iter.cleanup_front(self.idx);
114114
}
115115
}

library/core/src/iter/range.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,14 @@ macro_rules! unsafe_range_trusted_random_access_impl {
497497
#[unstable(feature = "trusted_random_access", issue = "none")]
498498
unsafe impl TrustedRandomAccess for ops::Range<$t> {
499499

500-
fn cleanup(&mut self, num: usize, forward: bool) {
501-
if forward {
502-
let _ = self.advance_by(num);
503-
} else {
504-
let _ = self.advance_back_by(num);
505-
}
500+
fn cleanup_front(&mut self, num: usize) {
501+
let _ = self.advance_by(num);
502+
}
503+
504+
fn cleanup_back(&mut self, num: usize) {
505+
let _ = self.advance_back_by(num);
506506
}
507+
507508
}
508509
)*)
509510
}

library/core/src/iter/traits/trusted_random_access.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ pub unsafe trait TrustedRandomAccess: Sized {
5555
self.size_hint().0
5656
}
5757

58-
fn cleanup(&mut self, num: usize, forward: bool);
58+
fn cleanup_front(&mut self, num: usize);
59+
60+
fn cleanup_back(&mut self, num: usize);
5961
}
6062

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

0 commit comments

Comments
 (0)