Skip to content

Commit f4c59b3

Browse files
committed
Use drop_in_place in array::IntoIter::drop
This skips the loop when the element type is known not to have drop glue, even in debug mode.
1 parent 23f890f commit f4c59b3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libcore/array/iter.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ where
9292
mem::transmute::<&[MaybeUninit<T>], &[T]>(slice)
9393
}
9494
}
95+
96+
/// Returns a mutable slice of all elements that have not been yielded yet.
97+
fn as_mut_slice(&mut self) -> &mut [T] {
98+
// This transmute is safe, same as in `as_slice` above.
99+
let slice = &mut self.data[self.alive.clone()];
100+
unsafe {
101+
mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(slice)
102+
}
103+
}
95104
}
96105

97106

@@ -182,10 +191,9 @@ where
182191
[T; N]: LengthAtMost32,
183192
{
184193
fn drop(&mut self) {
185-
// We simply drop each element via `for_each`. This should not incur
186-
// any significant runtime overhead and avoids adding another `unsafe`
187-
// block.
188-
self.by_ref().for_each(drop);
194+
unsafe {
195+
ptr::drop_in_place(self.as_mut_slice())
196+
}
189197
}
190198
}
191199

0 commit comments

Comments
 (0)