Skip to content

arena: speed up TypedArena::clear and improve common patterns #56378

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

Merged
merged 2 commits into from
Dec 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ impl<T> TypedArena<T> {
unsafe {
// Clear the last chunk, which is partially filled.
let mut chunks_borrow = self.chunks.borrow_mut();
if let Some(mut last_chunk) = chunks_borrow.pop() {
if let Some(mut last_chunk) = chunks_borrow.last_mut() {
self.clear_last_chunk(&mut last_chunk);
let len = chunks_borrow.len();
// If `T` is ZST, code below has no effect.
for mut chunk in chunks_borrow.drain(..) {
for mut chunk in chunks_borrow.drain(..len-1) {
let cap = chunk.storage.cap();
chunk.destroy(cap);
}
chunks_borrow.push(last_chunk);
}
}
}
Expand Down Expand Up @@ -310,13 +310,8 @@ impl Default for DroplessArena {
impl DroplessArena {
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
let ptr = ptr as *const u8 as *mut u8;
for chunk in &*self.chunks.borrow() {
if chunk.start() <= ptr && ptr < chunk.end() {
return true;
}
}

false
self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
}

fn align(&self, align: usize) {
Expand Down Expand Up @@ -408,7 +403,7 @@ impl DroplessArena {
{
assert!(!mem::needs_drop::<T>());
assert!(mem::size_of::<T>() != 0);
assert!(slice.len() != 0);
assert!(!slice.is_empty());

let mem = self.alloc_raw(
slice.len() * mem::size_of::<T>(),
Expand Down Expand Up @@ -604,6 +599,15 @@ mod tests {
}
}

#[bench]
pub fn bench_typed_arena_clear(b: &mut Bencher) {
let mut arena = TypedArena::default();
b.iter(|| {
arena.alloc(Point { x: 1, y: 2, z: 3 });
arena.clear();
})
}

// Drop tests

struct DropCounter<'a> {
Expand Down