-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Support allocating iterators with arenas #59533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -23,7 +23,9 @@ | |||
|
||||
extern crate alloc; | ||||
|
||||
use rustc_data_structures::cold_path; | ||||
use rustc_data_structures::sync::MTLock; | ||||
use smallvec::SmallVec; | ||||
|
||||
use std::cell::{Cell, RefCell}; | ||||
use std::cmp; | ||||
|
@@ -55,13 +57,16 @@ pub struct TypedArena<T> { | |||
struct TypedArenaChunk<T> { | ||||
/// The raw storage for the arena chunk. | ||||
storage: RawVec<T>, | ||||
/// The number of valid entries in the chunk. | ||||
entries: usize, | ||||
} | ||||
|
||||
impl<T> TypedArenaChunk<T> { | ||||
#[inline] | ||||
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> { | ||||
TypedArenaChunk { | ||||
storage: RawVec::with_capacity(capacity), | ||||
entries: 0, | ||||
} | ||||
} | ||||
|
||||
|
@@ -149,6 +154,34 @@ impl<T> TypedArena<T> { | |||
} | ||||
} | ||||
|
||||
#[inline] | ||||
fn can_allocate(&self, len: usize) -> bool { | ||||
let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize; | ||||
let at_least_bytes = len.checked_mul(mem::size_of::<T>()).unwrap(); | ||||
available_capacity_bytes >= at_least_bytes | ||||
} | ||||
|
||||
/// Ensures there's enough space in the current chunk to fit `len` objects. | ||||
#[inline] | ||||
fn ensure_capacity(&self, len: usize) { | ||||
if !self.can_allocate(len) { | ||||
self.grow(len); | ||||
debug_assert!(self.can_allocate(len)); | ||||
} | ||||
} | ||||
|
||||
#[inline] | ||||
unsafe fn alloc_raw_slice(&self, len: usize) -> *mut T { | ||||
assert!(mem::size_of::<T>() != 0); | ||||
assert!(len != 0); | ||||
|
||||
self.ensure_capacity(len); | ||||
|
||||
let start_ptr = self.ptr.get(); | ||||
self.ptr.set(start_ptr.add(len)); | ||||
start_ptr | ||||
} | ||||
|
||||
/// Allocates a slice of objects that are copied into the `TypedArena`, returning a mutable | ||||
/// reference to it. Will panic if passed a zero-sized types. | ||||
/// | ||||
|
@@ -161,21 +194,64 @@ impl<T> TypedArena<T> { | |||
where | ||||
T: Copy, | ||||
{ | ||||
unsafe { | ||||
let len = slice.len(); | ||||
let start_ptr = self.alloc_raw_slice(len); | ||||
slice.as_ptr().copy_to_nonoverlapping(start_ptr, len); | ||||
slice::from_raw_parts_mut(start_ptr, len) | ||||
} | ||||
} | ||||
|
||||
#[inline] | ||||
pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] { | ||||
assert!(mem::size_of::<T>() != 0); | ||||
assert!(slice.len() != 0); | ||||
let mut iter = iter.into_iter(); | ||||
let size_hint = iter.size_hint(); | ||||
|
||||
let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize; | ||||
let at_least_bytes = slice.len() * mem::size_of::<T>(); | ||||
if available_capacity_bytes < at_least_bytes { | ||||
self.grow(slice.len()); | ||||
} | ||||
match size_hint { | ||||
(min, Some(max)) if min == max => { | ||||
// We know the exact number of elements the iterator will produce here | ||||
let len = min; | ||||
|
||||
unsafe { | ||||
let start_ptr = self.ptr.get(); | ||||
let arena_slice = slice::from_raw_parts_mut(start_ptr, slice.len()); | ||||
self.ptr.set(start_ptr.add(arena_slice.len())); | ||||
arena_slice.copy_from_slice(slice); | ||||
arena_slice | ||||
if len == 0 { | ||||
return &mut []; | ||||
} | ||||
|
||||
self.ensure_capacity(len); | ||||
|
||||
let slice = self.ptr.get(); | ||||
|
||||
unsafe { | ||||
let mut ptr = self.ptr.get(); | ||||
for _ in 0..len { | ||||
// Write into uninitialized memory. | ||||
ptr::write(ptr, iter.next().unwrap()); | ||||
// Advance the pointer. | ||||
ptr = ptr.offset(1); | ||||
// Update the pointer per iteration so if `iter.next()` panics | ||||
// we destroy the correct amount | ||||
self.ptr.set(ptr); | ||||
} | ||||
slice::from_raw_parts_mut(slice, len) | ||||
} | ||||
} | ||||
_ => { | ||||
cold_path(move || -> &mut [T] { | ||||
let mut vec: SmallVec<[_; 8]> = iter.collect(); | ||||
if vec.is_empty() { | ||||
return &mut []; | ||||
} | ||||
// Move the content to the arena by copying it and then forgetting | ||||
// the content of the SmallVec | ||||
unsafe { | ||||
let len = vec.len(); | ||||
let start_ptr = self.alloc_raw_slice(len); | ||||
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len); | ||||
vec.set_len(0); | ||||
slice::from_raw_parts_mut(start_ptr, len) | ||||
} | ||||
}) | ||||
} | ||||
} | ||||
} | ||||
|
||||
|
@@ -189,6 +265,7 @@ impl<T> TypedArena<T> { | |||
if let Some(last_chunk) = chunks.last_mut() { | ||||
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize; | ||||
let currently_used_cap = used_bytes / mem::size_of::<T>(); | ||||
last_chunk.entries = currently_used_cap; | ||||
|
self.grow(slice.len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That requires Copy
types, so it can't trigger it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment would be good here.