Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Source/JavaScriptCore/shell/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_OBJECTS:mimalloc-obj>)
endif ()

Expand Down Expand Up @@ -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 $<TARGET_OBJECTS:mimalloc-obj>)
endif ()

Expand Down
39 changes: 39 additions & 0 deletions Source/JavaScriptCore/shell/ExternalMimallocShims.cpp
Original file line number Diff line number Diff line change
@@ -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 <bmalloc/mimalloc.h>

// 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
Comment thread
robobun marked this conversation as resolved.
Resolved
{
mi_theap_collect(mi_theap_get_default(), /* force */ true);
}
39 changes: 39 additions & 0 deletions Source/WTF/wtf/AutomaticThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <wtf/AutomaticThread.h>

#include <wtf/DataLog.h>
#include <wtf/FastMalloc.h>
#include <wtf/PageBlock.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/Threading.h>
Expand All @@ -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> AutomaticThreadCondition::create()
{
return adoptRef(*new AutomaticThreadCondition);
Expand Down Expand Up @@ -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 };
Expand All @@ -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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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)
Expand All @@ -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 };
Expand Down
5 changes: 5 additions & 0 deletions Source/WTF/wtf/FastMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ void releaseFastMallocFreeMemoryForThisThread()
bmalloc::api::scavengeThisThread();
}

void releaseFastMallocFreeMemoryForIdleThread()
{
bmalloc::api::scavengeThisThreadOnIdle();
}

void releaseFastMallocFreeMemory()
{
bmalloc::api::scavenge();
Expand Down
2 changes: 2 additions & 0 deletions Source/WTF/wtf/FastMalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions Source/bmalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
7 changes: 7 additions & 0 deletions Source/bmalloc/bmalloc/BPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions Source/bmalloc/bmalloc/bmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
#endif

void scavengeThisThreadOnIdle()
{
#if BUSE(EXTERNAL_MIMALLOC)
mi_on_thread_idle();
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
#else
scavengeThisThread(/* force */ true);
#endif
}

void scavenge()
{
#if BENABLE(LIBPAS)
Expand Down
5 changes: 5 additions & 0 deletions Source/bmalloc/bmalloc/bmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading