Skip to content
Merged
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
23 changes: 22 additions & 1 deletion Source/JavaScriptCore/runtime/WaiterListManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,29 @@ WaiterListManager::WaitSyncResult WaiterListManager::waitSyncImpl(VM& vm, ValueT
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> added a new SyncWaiter=", syncWaiter.get(), " to a waiterList for ptr ", RawPointer(ptr));
syncWaiter->setParkedList(list.copyRef());

while (syncWaiter->isOnList() && time.now() < time && !shouldStopWaitingForTermination(vm))
#if USE(MIMALLOC)
// Only the owning thread can return the memory its thread-local heap holds, so do that once in a wait that
// takes a while. A wait that is notified soon pays nothing.
MonotonicTime releaseFreeMemoryTime = MonotonicTime::now() + 100_ms;
bool didReleaseFreeMemory = false;
#endif
while (syncWaiter->isOnList() && time.now() < time && !shouldStopWaitingForTermination(vm)) {
#if USE(MIMALLOC)
if (!didReleaseFreeMemory && releaseFreeMemoryTime < time) {
if (MonotonicTime::now() < releaseFreeMemoryTime) {
syncWaiter->condition().waitUntil(list->lock, releaseFreeMemoryTime.approximate<WallTime>());
continue;
}
didReleaseFreeMemory = true;
// A notification in the meantime takes us off the list, and the loop tests for that (and for a
// termination request) under the lock again before it waits.
DropLockForScope dropLock { listLocker };
WTF::releaseFastMallocFreeMemoryForIdleThread();
continue;
}
#endif
syncWaiter->condition().waitUntil(list->lock, time.approximate<WallTime>());
}

syncWaiter->setParkedList(nullptr);

Expand Down
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 @@ -94,6 +97,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
{
mi_theap_collect(mi_theap_get_default(), /* force */ false);
}
57 changes: 56 additions & 1 deletion 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 All @@ -48,6 +54,16 @@ AutomaticThreadCondition::~AutomaticThreadCondition() = default;

void AutomaticThreadCondition::notifyOne(const AbstractLocker& locker)
{
#if USE(MIMALLOC)
// A thread that is releasing its free memory only sees the notification when it is done with that.
for (auto& thread : m_threads) {
if (thread->isWaiting(locker) && !thread->m_isReleasingFreeMemory) {
thread->notify(locker);
return;
}
}
#endif

for (auto& thread : m_threads) {
if (thread->isWaiting(locker)) {
thread->notify(locker);
Expand Down Expand Up @@ -215,6 +231,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,8 +249,39 @@ 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).
Seconds timeout = m_timeout;
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;
m_isReleasingFreeMemory = true;
{
DropLockForScope dropLock { locker };
releaseFastMallocFreeMemoryForIdleThread();
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}
m_isReleasingFreeMemory = false;
if (!m_isWaiting)
continue;
timeout -= idleReleaseDelay;
}
#else
Seconds timeout = m_timeout;
#endif
bool awokenByNotify =
m_waitCondition.waitFor(*m_lock, m_timeout);
m_waitCondition.waitFor(*m_lock, timeout);
if (verbose && !awokenByNotify && !m_isWaiting)
dataLog(RawPointer(this), ": waitFor timed out, but notified via m_isWaiting flag!\n");
if (m_isWaiting && shouldSleep(locker)) {
Expand All @@ -244,6 +296,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
1 change: 1 addition & 0 deletions Source/WTF/wtf/AutomaticThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class WTF_EXPORT_PRIVATE AutomaticThread : public ThreadSafeRefCounted<Automatic
ThreadQOS m_qos { defaultThreadQOS };
bool m_isRunning { true };
bool m_isWaiting { false };
bool m_isReleasingFreeMemory { false };
bool m_hasUnderlyingThread { false };
Condition m_waitCondition;
Condition m_isRunningCondition;
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 @@ -699,6 +699,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
17 changes: 17 additions & 0 deletions Source/bmalloc/bmalloc/bmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ 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
// Not forced: that would also purge what every other thread just freed.
scavengeThisThread(/* force */ false);
#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