From e62d118e0616a36a395283bbb17b1df11c022024 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 5 Sep 2026 16:03:16 +0000 Subject: [PATCH] [WTF] AutomaticThread: release the thread-local heap after 100 ms idle Only the owning thread can collect its mimalloc thread-local heap. An AutomaticThread that finishes its work and waits on its condition keeps every page it freed: retired pages, blocks other threads freed into its pages, and the free runs inside pages it still uses. Nothing touches them until the thread times out after 10 s and exits, or until it works again. For the wasm compiler threads this is about 10 MB each after one compile of a 4 MB module. With numberOfWasmCompilerThreads at cores - 1, a process that compiles wasm modules holds cores x 10 MB of RSS that no live object accounts for (oven-sh/bun#41438). The JIT worklist threads hold their B3 and Air temporaries the same way. When a thread has waited 100 ms without a notify, it now calls releaseFastMallocFreeMemoryForIdleThread() with the worklist lock dropped, then waits out the rest of its timeout. A thread that is notified within 100 ms, the usual case between tasks, pays nothing. The flag resets on each unit of work. On the consumer's mimalloc (USE_EXTERNAL_MIMALLOC) the release is mi_on_thread_idle(), which also discards the free runs inside still-used pages and hands the arena purge to mimalloc's scavenger thread. The vendored mimalloc does not have that entry point, so it falls back to a forced mi_theap_collect. The jsc shell and testFFI link the archives against the vendored mimalloc-obj, so they define mi_on_thread_idle in terms of the vendored API (shell/ExternalMimallocShims.cpp). libpas and system malloc are unchanged: the hook is compiled only under USE(MIMALLOC). --- Source/JavaScriptCore/shell/CMakeLists.txt | 6 ++- .../shell/ExternalMimallocShims.cpp | 39 +++++++++++++++++++ Source/WTF/wtf/AutomaticThread.cpp | 39 +++++++++++++++++++ Source/WTF/wtf/FastMalloc.cpp | 5 +++ Source/WTF/wtf/FastMalloc.h | 2 + Source/bmalloc/CMakeLists.txt | 4 ++ Source/bmalloc/bmalloc/BPlatform.h | 7 ++++ Source/bmalloc/bmalloc/bmalloc.cpp | 16 ++++++++ Source/bmalloc/bmalloc/bmalloc.h | 5 +++ 9 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 Source/JavaScriptCore/shell/ExternalMimallocShims.cpp diff --git a/Source/JavaScriptCore/shell/CMakeLists.txt b/Source/JavaScriptCore/shell/CMakeLists.txt index 83d560adb6e0d..beb999387a7a9 100644 --- a/Source/JavaScriptCore/shell/CMakeLists.txt +++ b/Source/JavaScriptCore/shell/CMakeLists.txt @@ -21,7 +21,10 @@ endif () if (USE_EXTERNAL_MIMALLOC) # The shipped static archives intentionally leave mi_* unresolved so the # consumer links its own mimalloc. The jsc shell still needs a working - # allocator, so link the vendored mimalloc object library here only. + # allocator, so link the vendored mimalloc object library here only, plus + # the entry points the archives expect from the consumer's mimalloc that + # the vendored one does not have (ExternalMimallocShims.cpp). + list(APPEND jsc_SOURCES ExternalMimallocShims.cpp) list(APPEND jsc_LIBRARIES $) endif () @@ -75,6 +78,7 @@ if (USE_BUN_JSC_ADDITIONS) # Same as jsc above: the shipped static archives leave mi_* unresolved # under USE_EXTERNAL_MIMALLOC, so any executable that links the # framework must supply the vendored mimalloc objects itself. + list(APPEND testFFI_SOURCES ExternalMimallocShims.cpp) list(APPEND testFFI_LIBRARIES $) endif () diff --git a/Source/JavaScriptCore/shell/ExternalMimallocShims.cpp b/Source/JavaScriptCore/shell/ExternalMimallocShims.cpp new file mode 100644 index 0000000000000..5fdc9253dde09 --- /dev/null +++ b/Source/JavaScriptCore/shell/ExternalMimallocShims.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2026 Oven, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// Under USE_EXTERNAL_MIMALLOC the static archives call into the consumer's mimalloc +// (oven-sh/mimalloc), which has entry points the vendored copy does not. An executable in this +// repository that links the archives against the vendored mimalloc-obj (the jsc shell, testFFI) +// supplies those entry points here, in terms of the vendored API. + +#include + +// The consumer's hook for a thread that goes idle: collect the heap, discard the free runs inside +// still-used pages, hand the arena purge to a scavenger thread. The vendored mimalloc can only do +// the first part. +extern "C" void mi_on_thread_idle(void) noexcept +{ + mi_theap_collect(mi_theap_get_default(), /* force */ true); +} diff --git a/Source/WTF/wtf/AutomaticThread.cpp b/Source/WTF/wtf/AutomaticThread.cpp index 048180f219bf3..f85c533c852fb 100644 --- a/Source/WTF/wtf/AutomaticThread.cpp +++ b/Source/WTF/wtf/AutomaticThread.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,11 @@ WTF_MAKE_TZONE_ALLOCATED_IMPL(AutomaticThread); static constexpr bool verbose = false; +#if USE(MIMALLOC) +// How long a thread waits for more work before it releases its allocator's free memory. +static constexpr Seconds idleReleaseDelay = 100_ms; +#endif + Ref AutomaticThreadCondition::create() { return adoptRef(*new AutomaticThreadCondition); @@ -215,6 +221,11 @@ void AutomaticThread::start(const AbstractLocker&) stopImpl(locker); }; +#if USE(MIMALLOC) + // Whether this thread released its allocator's free memory since it last worked. + bool didReleaseFreeMemory = false; +#endif + for (;;) { { Locker locker { *m_lock }; @@ -228,6 +239,31 @@ void AutomaticThread::start(const AbstractLocker&) // Shut the thread down after a timeout. m_isWaiting = true; +#if USE(MIMALLOC) + // Only the owning thread can return the memory its thread-local heap holds. + // A compiler thread frees tens of MB of temporaries after a large compile, + // and without this they stay resident until the thread times out and exits. + // Wait a little first, so that a thread which is notified again right away + // (the usual case between tasks) does not pay for the release. + // + // poll() runs once per notify: JITWorklistThread counts a thread as active + // from the notify until its poll() returns Wait. So the thread stays in the + // waiting state through the release, and polls again only if a notify + // arrived meanwhile (notify() clears m_isWaiting, which is checked under the + // lock, so a notify during the release is not lost). + if (!didReleaseFreeMemory && m_timeout > idleReleaseDelay) { + m_waitCondition.waitFor(*m_lock, idleReleaseDelay); + if (!m_isWaiting) + continue; + didReleaseFreeMemory = true; + { + DropLockForScope dropLock { locker }; + releaseFastMallocFreeMemoryForIdleThread(); + } + if (!m_isWaiting) + continue; + } +#endif bool awokenByNotify = m_waitCondition.waitFor(*m_lock, m_timeout); if (verbose && !awokenByNotify && !m_isWaiting) @@ -244,6 +280,9 @@ void AutomaticThread::start(const AbstractLocker&) } } +#if USE(MIMALLOC) + didReleaseFreeMemory = false; +#endif WorkResult result = work(); if (result == WorkResult::Stop) { Locker locker { *m_lock }; diff --git a/Source/WTF/wtf/FastMalloc.cpp b/Source/WTF/wtf/FastMalloc.cpp index bb0e77712461b..3c0fe4bef0888 100644 --- a/Source/WTF/wtf/FastMalloc.cpp +++ b/Source/WTF/wtf/FastMalloc.cpp @@ -605,6 +605,11 @@ void releaseFastMallocFreeMemoryForThisThread() bmalloc::api::scavengeThisThread(); } +void releaseFastMallocFreeMemoryForIdleThread() +{ + bmalloc::api::scavengeThisThreadOnIdle(); +} + void releaseFastMallocFreeMemory() { bmalloc::api::scavenge(); diff --git a/Source/WTF/wtf/FastMalloc.h b/Source/WTF/wtf/FastMalloc.h index 2e673c9f8c43a..8852635dc8bbc 100644 --- a/Source/WTF/wtf/FastMalloc.h +++ b/Source/WTF/wtf/FastMalloc.h @@ -177,6 +177,8 @@ WTF_EXPORT_PRIVATE size_t fastMallocGoodSize(size_t); WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemory(); WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemoryForThisThread(); +// For a thread that is idle and about to block for a while. See bmalloc::api::scavengeThisThreadOnIdle. +WTF_EXPORT_PRIVATE void releaseFastMallocFreeMemoryForIdleThread(); WTF_EXPORT_PRIVATE void fastCommitAlignedMemory(void*, size_t); WTF_EXPORT_PRIVATE void fastDecommitAlignedMemory(void*, size_t); diff --git a/Source/bmalloc/CMakeLists.txt b/Source/bmalloc/CMakeLists.txt index 8cc95b61e71d3..c9aa293d5f646 100644 --- a/Source/bmalloc/CMakeLists.txt +++ b/Source/bmalloc/CMakeLists.txt @@ -695,6 +695,10 @@ if (USE_MIMALLOC) add_definitions(-DUSE_MIMALLOC=1) endif () +if (USE_EXTERNAL_MIMALLOC) + add_definitions(-DUSE_EXTERNAL_MIMALLOC=1) +endif () + if (USE_SYSTEM_MALLOC) add_definitions(-DUSE_SYSTEM_MALLOC=1) endif () diff --git a/Source/bmalloc/bmalloc/BPlatform.h b/Source/bmalloc/bmalloc/BPlatform.h index b59de412789ab..99a96d5e59e62 100644 --- a/Source/bmalloc/bmalloc/BPlatform.h +++ b/Source/bmalloc/bmalloc/BPlatform.h @@ -387,6 +387,13 @@ #define BUSE_MIMALLOC 0 #endif +/* The consumer links its own mimalloc (oven-sh/mimalloc), which has APIs the vendored copy does not. */ +#if BUSE(MIMALLOC) && defined(USE_EXTERNAL_MIMALLOC) && USE_EXTERNAL_MIMALLOC +#define BUSE_EXTERNAL_MIMALLOC 1 +#else +#define BUSE_EXTERNAL_MIMALLOC 0 +#endif + #if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC #define BUSE_SYSTEM_MALLOC 1 #elif BTSAN_ENABLED diff --git a/Source/bmalloc/bmalloc/bmalloc.cpp b/Source/bmalloc/bmalloc/bmalloc.cpp index 02d1694849d29..5ac2fb569f206 100644 --- a/Source/bmalloc/bmalloc/bmalloc.cpp +++ b/Source/bmalloc/bmalloc/bmalloc.cpp @@ -125,6 +125,22 @@ void scavengeThisThread(bool force) #endif } +#if BUSE(EXTERNAL_MIMALLOC) +// oven-sh/mimalloc's idle hook: collects the thread's heap, discards the free runs inside the pages +// it still uses, and hands the arena purge to mimalloc's scavenger thread. The vendored mimalloc +// does not have it, so it is declared here and not in mimalloc.h. +extern "C" void mi_on_thread_idle(void) noexcept; +#endif + +void scavengeThisThreadOnIdle() +{ +#if BUSE(EXTERNAL_MIMALLOC) + mi_on_thread_idle(); +#else + scavengeThisThread(/* force */ true); +#endif +} + void scavenge() { #if BENABLE(LIBPAS) diff --git a/Source/bmalloc/bmalloc/bmalloc.h b/Source/bmalloc/bmalloc/bmalloc.h index 6e4a7ea21f17b..0198e8d8c5bbc 100644 --- a/Source/bmalloc/bmalloc/bmalloc.h +++ b/Source/bmalloc/bmalloc/bmalloc.h @@ -298,6 +298,11 @@ BEXPORT void freeLargeVirtual(void* object, size_t, HeapKind kind = HeapKind::Pr BEXPORT void scavengeThisThread(bool force = true); +// The calling thread is idle and about to block for a while. Return what its thread-local +// allocator state holds for it: retired pages, blocks that other threads freed into its pages, +// and the free runs inside the pages it still uses. Only the owning thread can do this. +BEXPORT void scavengeThisThreadOnIdle(); + BEXPORT void scavenge(); BEXPORT bool isEnabled(HeapKind kind = HeapKind::Primary);