about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorljedrz <ljedrz@gmail.com>2018-11-30 16:36:20 +0100
committerljedrz <ljedrz@gmail.com>2018-11-30 16:36:20 +0100
commitca0806c70320093a37dbe52ba2442d1e047c9c8d (patch)
treea718157db8c67c7269ffe95e478bcec77a042f55 /src
parentd48ab693d1ce99f30c0cf9abdf45c209824fe825 (diff)
downloadrust-ca0806c70320093a37dbe52ba2442d1e047c9c8d.tar.gz
rust-ca0806c70320093a37dbe52ba2442d1e047c9c8d.zip
arena: speed up TypedArena::clear
Diffstat (limited to 'src')
-rw-r--r--src/libarena/lib.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs
index dae05a368fa..ed83eddc479 100644
--- a/src/libarena/lib.rs
+++ b/src/libarena/lib.rs
@@ -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);
             }
         }
     }
@@ -604,6 +604,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> {