Skip to content

Commit

Permalink
Auto merge of #56378 - ljedrz:arena_tweaks, r=nagisa
Browse files Browse the repository at this point in the history
arena: speed up TypedArena::clear and improve common patterns

- speed up `TypedArena::clear`: improves its performance by up to **33%** (in case of a single entry)
- simplify `DroplessArena::in_arena`
  • Loading branch information
bors committed Dec 2, 2018
2 parents 0765eb9 + 95f32f1 commit 9abc231
Showing 1 changed file with 14 additions and 10 deletions.
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 @@ -311,13 +311,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())
}

#[inline]
Expand Down Expand Up @@ -410,7 +405,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 @@ -606,6 +601,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

0 comments on commit 9abc231

Please sign in to comment.