Skip to content

Commit 056b8a0

Browse files
committed
tune typedarena
1 parent bc38a77 commit 056b8a0

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/libarena/lib.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,32 +151,32 @@ impl<T> TypedArena<T> {
151151

152152
let ptr = self.ptr.get();
153153

154-
let ptr = if ptr == self.end.get() {
155-
self.grow_and_alloc()
156-
} else {
157-
// Advance the pointer.
158-
unsafe {
159-
self.ptr.set(ptr.offset(1));
160-
}
161-
ptr
162-
};
163-
164154
unsafe {
165-
// Write into uninitialized memory.
166-
ptr::write(ptr, object);
167-
&mut *ptr
155+
if std::intrinsics::unlikely(ptr == self.end.get()) {
156+
self.grow_and_alloc(object)
157+
} else {
158+
self.alloc_unchecked(ptr, object)
159+
}
168160
}
169161
}
170162

163+
#[inline(always)]
164+
unsafe fn alloc_unchecked(&self, ptr: *mut T, object: T) -> &mut T {
165+
// Advance the pointer.
166+
self.ptr.set(ptr.offset(1));
167+
// Write into uninitialized memory.
168+
ptr::write(ptr, object);
169+
&mut *ptr
170+
}
171+
171172
#[inline(never)]
172173
#[cold]
173-
fn grow_and_alloc(&self) -> *mut T {
174+
fn grow_and_alloc(&self, object: T) -> &mut T {
175+
// We move the object in this function so if it has a destructor
176+
// the fast path need not have an unwind handler to destroy it
174177
self.grow(1);
175178
unsafe {
176-
let ptr = self.ptr.get();
177-
// Advance the pointer.
178-
self.ptr.set(ptr.offset(1));
179-
ptr
179+
self.alloc_unchecked(self.ptr.get(), object)
180180
}
181181
}
182182

@@ -355,6 +355,11 @@ pub fn atest2(a: &SyncDroplessArena, b: Box<usize>) -> &Box<usize> {
355355
a.promote(b)
356356
}
357357

358+
#[no_mangle]
359+
pub fn atest6(a: &SyncDroplessArena, b: usize) -> &usize {
360+
a.promote(b)
361+
}
362+
358363
#[no_mangle]
359364
pub fn atest3(a: &DroplessArena) {
360365
a.align(8);
@@ -479,7 +484,7 @@ impl DroplessArena {
479484
// Find some way to guarantee this doesn't happen for small fixed size types
480485
let ptr = self.ptr.get();
481486
let future_end = intrinsics::arith_offset(ptr, bytes as isize);
482-
if /*std::intrinsics::unlikely(*/(future_end as *mut u8) >= self.end.get()/*)*/ {
487+
if std::intrinsics::unlikely((future_end as *mut u8) >= self.end.get()) {
483488
self.grow_and_alloc_raw(bytes)
484489
} else {
485490
self.alloc_raw_unchecked(ptr, bytes)

0 commit comments

Comments
 (0)