From 318a0205b0cda55fba5c17596c50935523cf0815 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 01:36:59 +0000 Subject: [PATCH] perf(alloc): return &'static Key from mallctl! macro --- src/core/alloc/je.rs | 62 +++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/core/alloc/je.rs b/src/core/alloc/je.rs index 98241aad4..e9da2d721 100644 --- a/src/core/alloc/je.rs +++ b/src/core/alloc/je.rs @@ -209,7 +209,6 @@ macro_rules! mallctl { static KEY: OnceLock = OnceLock::new(); KEY.get_or_init(|| key($name).expect("failed to translate name into mib key")) - .clone() }}; } @@ -230,12 +229,12 @@ pub mod this_thread { pub fn idle() -> Result { // SAFETY: `thread.idle` is a non-indexed notification ctl. - unsafe { super::notify(&mallctl!("thread.idle")) } + unsafe { super::notify(mallctl!("thread.idle")) } } pub fn flush() -> Result { // SAFETY: `thread.tcache.flush` is a non-indexed notification ctl. - unsafe { super::notify(&mallctl!("thread.tcache.flush")) } + unsafe { super::notify(mallctl!("thread.tcache.flush")) } } pub fn set_muzzy_decay(decay_ms: isize) -> Result { @@ -260,59 +259,59 @@ pub mod this_thread { pub fn cache_enable(enable: bool) -> Result { // SAFETY: `thread.tcache.enabled` has C type `bool` (1 byte on non-MSVC). - unsafe { super::set::(&mallctl!("thread.tcache.enabled"), enable.into()) } + unsafe { super::set::(mallctl!("thread.tcache.enabled"), enable.into()) } .map(is_nonzero!()) } pub fn is_cache_enabled() -> Result { // SAFETY: `thread.tcache.enabled` has C type `bool` (1 byte on non-MSVC). - unsafe { super::get::(&mallctl!("thread.tcache.enabled")) }.map(is_nonzero!()) + unsafe { super::get::(mallctl!("thread.tcache.enabled")) }.map(is_nonzero!()) } pub fn set_arena(id: usize) -> Result { // SAFETY: `thread.arena` has C type `unsigned`. - unsafe { super::set::(&mallctl!("thread.arena"), id.try_into()?) } + unsafe { super::set::(mallctl!("thread.arena"), id.try_into()?) } .and_then(math::try_into) } pub fn arena_id() -> Result { // SAFETY: `thread.arena` has C type `unsigned`. - unsafe { super::get::(&mallctl!("thread.arena")) }.and_then(math::try_into) + unsafe { super::get::(mallctl!("thread.arena")) }.and_then(math::try_into) } pub fn prof_enable(enable: bool) -> Result { // SAFETY: `thread.prof.active` has C type `bool` (1 byte on non-MSVC). - unsafe { super::set::(&mallctl!("thread.prof.active"), enable.into()) } + unsafe { super::set::(mallctl!("thread.prof.active"), enable.into()) } .map(is_nonzero!()) } pub fn is_prof_enabled() -> Result { // SAFETY: `thread.prof.active` has C type `bool` (1 byte on non-MSVC). - unsafe { super::get::(&mallctl!("thread.prof.active")) }.map(is_nonzero!()) + unsafe { super::get::(mallctl!("thread.prof.active")) }.map(is_nonzero!()) } pub fn reset_peak() -> Result { // SAFETY: `thread.peak.reset` is a non-indexed notification ctl. - unsafe { super::notify(&mallctl!("thread.peak.reset")) } + unsafe { super::notify(mallctl!("thread.peak.reset")) } } pub fn peak() -> Result { // SAFETY: `thread.peak.read` has C type `uint64_t`. - unsafe { super::get(&mallctl!("thread.peak.read")) } + unsafe { super::get(mallctl!("thread.peak.read")) } } #[inline] #[must_use] pub fn allocated() -> u64 { // SAFETY: `thread.allocated` has C type `uint64_t`. - unsafe { super::get(&mallctl!("thread.allocated")) }.unwrap_or_default() + unsafe { super::get(mallctl!("thread.allocated")) }.unwrap_or_default() } #[inline] #[must_use] pub fn deallocated() -> u64 { // SAFETY: `thread.deallocated` has C type `uint64_t`. - unsafe { super::get(&mallctl!("thread.deallocated")) }.unwrap_or_default() + unsafe { super::get(mallctl!("thread.deallocated")) }.unwrap_or_default() } /// # Safety @@ -321,7 +320,8 @@ pub mod this_thread { // `arena_id()` returns this thread's own valid arena (< narenas < ARENA_LIMIT), // so we skip `validate_arena_id()` to avoid locking `ctl_mtx` on hot paths like // `decay()`. - unsafe fn notify(mut key: Key) -> Result { + unsafe fn notify(key: &Key) -> Result { + let mut key = key.clone(); key[1] = arena_id()?; // SAFETY: Caller guarantees `key` is an `arena..*` notification MIB // and `arena_id()` returns a valid arena index. @@ -331,10 +331,11 @@ pub mod this_thread { /// # Safety /// Caller must ensure `key` is an `arena..*` MIB and `T` matches its /// exact C type. - unsafe fn set(mut key: Key, val: T) -> Result + unsafe fn set(key: &Key, val: T) -> Result where T: Copy + Debug, { + let mut key = key.clone(); // `arena_id()` is already a valid arena index; skip `validate_arena_id()`. key[1] = arena_id()?; // SAFETY: Caller guarantees `T` matches `key`. @@ -344,10 +345,11 @@ pub mod this_thread { /// # Safety /// Caller must ensure `key` is an `arena..*` MIB and `T` matches its /// exact C type. - unsafe fn get(mut key: Key) -> Result + unsafe fn get(key: &Key) -> Result where T: Copy + Debug, { + let mut key = key.clone(); // `arena_id()` is already a valid arena index; skip `validate_arena_id()`. key[1] = arena_id()?; // SAFETY: Caller guarantees `T` matches `key`. @@ -357,22 +359,22 @@ pub mod this_thread { pub fn stats_reset() -> Result { // SAFETY: `stats.mutexes.reset` is a non-indexed notification ctl. - unsafe { notify(&mallctl!("stats.mutexes.reset")) } + unsafe { notify(mallctl!("stats.mutexes.reset")) } } pub fn prof_reset() -> Result { // SAFETY: `prof.reset` is a non-indexed notification ctl. - unsafe { notify(&mallctl!("prof.reset")) } + unsafe { notify(mallctl!("prof.reset")) } } pub fn prof_enable(enable: bool) -> Result { // SAFETY: `prof.active` has C type `bool` (1 byte on non-MSVC). - unsafe { set::(&mallctl!("prof.active"), enable.into()) }.map(is_nonzero!()) + unsafe { set::(mallctl!("prof.active"), enable.into()) }.map(is_nonzero!()) } pub fn is_prof_enabled() -> Result { // SAFETY: `prof.active` has C type `bool` (1 byte on non-MSVC). - unsafe { get::(&mallctl!("prof.active")) }.map(is_nonzero!()) + unsafe { get::(mallctl!("prof.active")) }.map(is_nonzero!()) } pub fn trim> + Copy>(arena: I) -> Result { @@ -396,7 +398,7 @@ pub fn set_muzzy_decay>>(arena: I, decay_ms: isize) -> Res set_by_arena(arena, mallctl!("arena.0.muzzy_decay_ms"), decay_ms) }, // SAFETY: `arenas.muzzy_decay_ms` has C type `ssize_t`. - | _ => unsafe { set(&mallctl!("arenas.muzzy_decay_ms"), decay_ms) }, + | _ => unsafe { set(mallctl!("arenas.muzzy_decay_ms"), decay_ms) }, } } @@ -407,13 +409,13 @@ pub fn set_dirty_decay>>(arena: I, decay_ms: isize) -> Res set_by_arena(arena, mallctl!("arena.0.dirty_decay_ms"), decay_ms) }, // SAFETY: `arenas.dirty_decay_ms` has C type `ssize_t`. - | _ => unsafe { set(&mallctl!("arenas.dirty_decay_ms"), decay_ms) }, + | _ => unsafe { set(mallctl!("arenas.dirty_decay_ms"), decay_ms) }, } } pub fn background_thread_enable(enable: bool) -> Result { // SAFETY: `background_thread` has C type `bool` (1 byte on non-MSVC). - unsafe { set::(&mallctl!("background_thread"), enable.into()) }.map(is_nonzero!()) + unsafe { set::(mallctl!("background_thread"), enable.into()) }.map(is_nonzero!()) } #[inline] @@ -430,7 +432,7 @@ pub fn is_phycpu_arena() -> bool { percpu_arenas().is_ok_and(is_equal_to!("phycp pub fn percpu_arenas() -> Result<&'static str> { // SAFETY: `opt.percpu_arena` has C type `const char *`. - let ptr = unsafe { get::<*const c_char>(&mallctl!("opt.percpu_arena")) }?; + let ptr = unsafe { get::<*const c_char>(mallctl!("opt.percpu_arena")) }?; if ptr.is_null() { return Err(err!("mallctl: opt.percpu_arena returned NULL")); } @@ -443,17 +445,17 @@ pub fn percpu_arenas() -> Result<&'static str> { pub fn arenas() -> Result { // SAFETY: `arenas.narenas` has C type `unsigned`. - unsafe { get::(&mallctl!("arenas.narenas")) }.and_then(math::try_into) + unsafe { get::(mallctl!("arenas.narenas")) }.and_then(math::try_into) } pub fn inc_epoch() -> Result { // SAFETY: `epoch` has C type `uint64_t`. - unsafe { xchg(&mallctl!("epoch"), 1_u64) } + unsafe { xchg(mallctl!("epoch"), 1_u64) } } pub fn acq_epoch() -> Result { // SAFETY: `epoch` has C type `uint64_t`. - unsafe { get(&mallctl!("epoch")) } + unsafe { get(mallctl!("epoch")) } } fn validate_arena_id(id: usize, allow_all: bool) -> Result { @@ -472,7 +474,8 @@ fn validate_arena_id(id: usize, allow_all: bool) -> Result { /// # Safety /// Caller must ensure `key` is an `arena..*` notification MIB /// (`key.len() >= 2`). -unsafe fn notify_by_arena(id: Option, mut key: Key) -> Result { +unsafe fn notify_by_arena(id: Option, key: &Key) -> Result { + let mut key = key.clone(); key[1] = match id { | Some(id) => validate_arena_id(id, true)?, | None => ARENAS_ALL, @@ -485,10 +488,11 @@ unsafe fn notify_by_arena(id: Option, mut key: Key) -> Result { /// # Safety /// Caller must ensure `key` is an `arena..*` MIB (`key.len() >= 2`) and `T` /// matches its exact C type. -unsafe fn set_by_arena(id: usize, mut key: Key, val: T) -> Result +unsafe fn set_by_arena(id: usize, key: &Key, val: T) -> Result where T: Copy + Debug, { + let mut key = key.clone(); key[1] = validate_arena_id(id, false)?; // SAFETY: Caller guarantees `T` matches `key`. unsafe { set(&key, val) }