Skip to content

Commit 2e5e49f

Browse files
committed
Remove the TypedArena::alloc_from_iter specialization.
It was added in #78569. It's complicated and doesn't actually help performance.
1 parent df5f0c6 commit 2e5e49f

File tree

1 file changed

+16
-75
lines changed
  • compiler/rustc_arena/src

1 file changed

+16
-75
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#![feature(dropck_eyepatch)]
1616
#![feature(new_uninit)]
1717
#![feature(maybe_uninit_slice)]
18-
#![feature(min_specialization)]
1918
#![feature(decl_macro)]
2019
#![feature(pointer_byte_offsets)]
2120
#![feature(rustc_attrs)]
@@ -113,77 +112,6 @@ impl<T> ArenaChunk<T> {
113112
const PAGE: usize = 4096;
114113
const HUGE_PAGE: usize = 2 * 1024 * 1024;
115114

116-
trait IterExt<T> {
117-
fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T];
118-
}
119-
120-
impl<I, T> IterExt<T> for I
121-
where
122-
I: IntoIterator<Item = T>,
123-
{
124-
// This default collects into a `SmallVec` and then allocates by copying
125-
// from it. The specializations below for types like `Vec` are more
126-
// efficient, copying directly without the intermediate collecting step.
127-
#[inline]
128-
default fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T] {
129-
// Safety: if iteration panics, `self` is untouched, and thus left in a
130-
// good state.
131-
let vec: SmallVec<[_; 8]> = self.into_iter().collect();
132-
vec.alloc_from_iter(arena)
133-
}
134-
}
135-
136-
impl<T, const N: usize> IterExt<T> for [T; N] {
137-
#[inline]
138-
fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T] {
139-
let len = self.len();
140-
if len == 0 {
141-
return &mut [];
142-
}
143-
// Move the content to the arena by copying and then forgetting it.
144-
let start_ptr = arena.alloc_raw_slice(len);
145-
unsafe {
146-
self.as_slice().as_ptr().copy_to_nonoverlapping(start_ptr, len);
147-
mem::forget(self);
148-
slice::from_raw_parts_mut(start_ptr, len)
149-
}
150-
}
151-
}
152-
153-
impl<T> IterExt<T> for Vec<T> {
154-
#[inline]
155-
fn alloc_from_iter(mut self, arena: &TypedArena<T>) -> &mut [T] {
156-
let len = self.len();
157-
if len == 0 {
158-
return &mut [];
159-
}
160-
// Move the content to the arena by copying and then forgetting it.
161-
let start_ptr = arena.alloc_raw_slice(len);
162-
unsafe {
163-
self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
164-
self.set_len(0);
165-
slice::from_raw_parts_mut(start_ptr, len)
166-
}
167-
}
168-
}
169-
170-
impl<A: smallvec::Array> IterExt<A::Item> for SmallVec<A> {
171-
#[inline]
172-
fn alloc_from_iter(mut self, arena: &TypedArena<A::Item>) -> &mut [A::Item] {
173-
let len = self.len();
174-
if len == 0 {
175-
return &mut [];
176-
}
177-
// Move the content to the arena by copying and then forgetting it.
178-
let start_ptr = arena.alloc_raw_slice(len);
179-
unsafe {
180-
self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
181-
self.set_len(0);
182-
slice::from_raw_parts_mut(start_ptr, len)
183-
}
184-
}
185-
}
186-
187115
/// An arena that can hold objects of only one type.
188116
pub struct TypedArena<T> {
189117
/// A pointer to the next object to be allocated.
@@ -282,10 +210,23 @@ impl<T> TypedArena<T> {
282210
// will then cause UB in `TypedArena::drop`.
283211
//
284212
// Instead we use an approach where any iterator panic will occur
285-
// before the memory is allocated, with some specialization for common
286-
// cases like `Vec` and `SmallVec`.
213+
// before the memory is allocated. This function is much less hot than
214+
// `DroplessArena::alloc_from_iter`, so it doesn't need to be
215+
// hyper-optimized.
287216
assert!(mem::size_of::<T>() != 0);
288-
iter.alloc_from_iter(self)
217+
218+
let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
219+
if vec.is_empty() {
220+
return &mut [];
221+
}
222+
// Move the content to the arena by copying and then forgetting it.
223+
let len = vec.len();
224+
let start_ptr = self.alloc_raw_slice(len);
225+
unsafe {
226+
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
227+
vec.set_len(0);
228+
slice::from_raw_parts_mut(start_ptr, len)
229+
}
289230
}
290231

291232
/// Grows the arena.

0 commit comments

Comments
 (0)