Skip to content

Commit 2511176

Browse files
Add opt-in MI_SINGLE_THREADED specialization for the free fast path
In a program that only ever uses mimalloc from a single thread, every freed block is guaranteed to be thread-local, so the per-free thread ownership check in mi_free_ex (a thread-id TLS read, a relaxed atomic load of segment->thread_id, and a compare) is redundant. This adds an opt-in, off-by-default compile-time switch (-DMI_SINGLE_THREADED=1) that forces is_local=true and skips that check. The default build is unchanged (the #else path is identical to before and the full multi-threaded test suite still passes). Measured on a single-threaded, allocation-heavy workload (mimalloc-bench alloc-test, 1 thread, pinned, interleaved median-of-11): ~0.4% faster wall time, with perf showing ~2.5% fewer branches and ~1.1% fewer instructions (the eliminated per-free ownership branch). cfrac/espresso output is byte-identical to baseline. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9285427 commit 2511176

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/free.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ static inline void mi_free_ex(void* p, size_t* usable) mi_attr_noexcept
153153
mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free");
154154
if mi_unlikely(segment==NULL) return;
155155

156+
#if defined(MI_SINGLE_THREADED) && (MI_SINGLE_THREADED)
157+
// Single-threaded specialization (ExGen-Malloc, CAL'25): in a program that only ever
158+
// uses mimalloc from one thread, every block is thread-local, so we can skip the
159+
// thread-id TLS read + atomic load + compare on every free.
160+
const bool is_local = true;
161+
#else
156162
const bool is_local = (_mi_prim_thread_id() == mi_atomic_load_relaxed(&segment->thread_id));
163+
#endif
157164
mi_page_t* const page = _mi_segment_page_of(segment, p);
158165
if (usable!=NULL) { *usable = mi_page_usable_block_size(page); }
159166

0 commit comments

Comments
 (0)