Apple friendly way to madvice on commit/decommit

This commit is contained in:
iurii zakipnyi 2025-06-03 09:23:42 -07:00
parent 3e32b4c385
commit 76b631254d

View file

@ -430,13 +430,28 @@ int _mi_prim_commit(void* start, size_t size, bool* is_zero) {
err = errno; err = errno;
unix_mprotect_hint(err); unix_mprotect_hint(err);
} }
#if defined(__APPLE__)
// MADV_FREE_REUSABLE is paired with MADV_FREE_REUSE for accounting
madvise(start, size, MADV_FREE_REUSE);
#endif
return err; return err;
} }
int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) { int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) {
int err = 0; int err = 0;
// decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) #if defined(__APPLE__)
// On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure.
err = unix_madvise(start, size, MADV_FREE_REUSABLE);
// if this fails - fallback to MADV_DONTNEED
#endif
if (err)
{
// use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
err = unix_madvise(start, size, MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED);
}
#if !MI_DEBUG && MI_SECURE<=2 #if !MI_DEBUG && MI_SECURE<=2
*needs_recommit = false; *needs_recommit = false;
#else #else
@ -454,6 +469,15 @@ int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) {
} }
int _mi_prim_reset(void* start, size_t size) { int _mi_prim_reset(void* start, size_t size) {
int err = 0;
#if defined(__APPLE__)
// On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure.
err = unix_madvise(start, size, MADV_FREE_REUSABLE);
// if this fails - fallback to MADV_FREE/MADV_DONTNEED
#endif
if (err)
{
// We try to use `MADV_FREE` as that is the fastest. A drawback though is that it // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it
// will not reduce the `rss` stats in tools like `top` even though the memory is available // will not reduce the `rss` stats in tools like `top` even though the memory is available
// to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by
@ -461,7 +485,6 @@ int _mi_prim_reset(void* start, size_t size) {
#if defined(MADV_FREE) #if defined(MADV_FREE)
static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE);
int oadvice = (int)mi_atomic_load_relaxed(&advice); int oadvice = (int)mi_atomic_load_relaxed(&advice);
int err;
while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; };
if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) {
// if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on
@ -469,8 +492,9 @@ int _mi_prim_reset(void* start, size_t size) {
err = unix_madvise(start, size, MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED);
} }
#else #else
int err = unix_madvise(start, size, MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED);
#endif #endif
}
return err; return err;
} }