From dbf748e3f44e17227cd1cc52a7ed87e6bc4b62b5 Mon Sep 17 00:00:00 2001 From: Paul Bone Date: Tue, 1 Sep 2026 00:08:56 +0000 Subject: [PATCH] Bug 2055507 - pt 8. Disable the chunk cache on Windows r=glandium Differential Revision: https://phabricator.services.mozilla.com/D313993 --- memory/build/Chunk.cpp | 30 +++++++++++++----------------- memory/build/Chunk.h | 21 +++++++++------------ memory/build/mozjemalloc.cpp | 3 +++ 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/memory/build/Chunk.cpp b/memory/build/Chunk.cpp index b78446472783..119fce7f840a 100644 --- a/memory/build/Chunk.cpp +++ b/memory/build/Chunk.cpp @@ -517,9 +517,11 @@ void base_chunk_dealloc(void* aChunk, size_t aSize, ChunkType aType) { MOZ_ASSERT((aSize & kChunkSizeMask) == 0); MOZ_ASSERT(!gChunkRTree.Get(aChunk)); +#ifndef XP_WIN if (gCache.TryRecord(aChunk, aSize, aType)) { return; } +#endif pages_unmap(aChunk, aSize); } @@ -575,10 +577,16 @@ void* arena_chunk_alloc(chunk_allocator_t* aChunkAllocator, size_t aSize, } static void* system_pages_map(size_t aSize, size_t aAlignment) { - void* ret = gCache.Recycle(aSize, aAlignment); + void* ret = nullptr; + +#ifndef XP_WIN + ret = gCache.Recycle(aSize, aAlignment); if (!ret) { +#endif ret = pages_mmap_aligned(aSize, aAlignment, ReserveAndCommit); +#ifndef XP_WIN } +#endif return ret; } @@ -602,11 +610,9 @@ bool arena_chunk_t::IsEmpty() { (~gPageSizeMask | CHUNK_MAP_ALLOCATED)) == gMaxLargeClass; } -bool ChunkCache::TryRecord(void* aChunk, size_t aSize, ChunkType aType) { - if (!CanRecycle(aSize)) { - return false; - } +#ifndef XP_WIN +bool ChunkCache::TryRecord(void* aChunk, size_t aSize, ChunkType aType) { size_t recycled_so_far = mRecycledSize; // In case some race condition put us above the limit. @@ -617,17 +623,9 @@ bool ChunkCache::TryRecord(void* aChunk, size_t aSize, ChunkType aType) { size_t recycle_remaining = gRecycleLimit - recycled_so_far; size_t to_recycle; if (aSize > recycle_remaining) { -#ifndef XP_WIN to_recycle = recycle_remaining; // Drop pages that would overflow the recycle limit pages_trim(aChunk, aSize, 0, to_recycle, ReserveAndCommit); -#else - // On windows pages_trim unallocates and reallocates the whole - // chunk, there's no point doing that during recycling so instead we - // fail. - pages_unmap(aChunk, aSize); - return; -#endif } else { to_recycle = aSize; } @@ -705,10 +703,6 @@ void ChunkCache::Record(void* aChunk, size_t aSize, ChunkType aType) { } void* ChunkCache::Recycle(size_t aSize, size_t aAlignment) { - if (!CanRecycle(aSize)) { - return nullptr; - } - size_t alloc_size = aSize + aAlignment - kChunkSize; // Beware size_t wrap-around. if (alloc_size < aSize) { @@ -780,3 +774,5 @@ void* ChunkCache::Recycle(size_t aSize, size_t aAlignment) { // The global chunk cache. ChunkCache gCache; + +#endif /* ! XP_WIN */ diff --git a/memory/build/Chunk.h b/memory/build/Chunk.h index 894293095bed..c09dc01cb5f2 100644 --- a/memory/build/Chunk.h +++ b/memory/build/Chunk.h @@ -230,6 +230,13 @@ void* pages_mmap_aligned(size_t size, size_t alignment, void pages_unmap(void* aAddr, size_t aSize); +// On Windows, calls to VirtualAlloc and VirtualFree must be matched, making +// it awkward to recycle allocations of varying sizes. +// Therefore the chunk cache is disabled on windows since arenas have a +// chunk list for arena chunks, and non-arena chunks arbitrary sizes +// don't cache well without splitting. +#ifndef XP_WIN + class ChunkCache { private: Mutex mMutex; @@ -251,18 +258,6 @@ class ChunkCache { void Init() { mMutex.Init(); } - static constexpr bool CanRecycle(size_t aSize) { -#ifdef XP_WIN - // On Windows, calls to VirtualAlloc and VirtualFree must be matched, making - // it awkward to recycle allocations of varying sizes. Therefore we only - // allow recycling when the size equals the chunksize, unless deallocation - // is entirely disabled. - return aSize == kChunkSize; -#else - return true; -#endif - } - // Try to put this chunk in the cache, false if the cache is full. bool TryRecord(void* aChunk, size_t aSize, ChunkType aType); @@ -277,4 +272,6 @@ class ChunkCache { extern ChunkCache gCache; +#endif /* ! XP_WIN */ + #endif /* ! CHUNK_H */ diff --git a/memory/build/mozjemalloc.cpp b/memory/build/mozjemalloc.cpp index ee1d5ab8d696..d07ff71c3e0e 100644 --- a/memory/build/mozjemalloc.cpp +++ b/memory/build/mozjemalloc.cpp @@ -3448,7 +3448,10 @@ static bool malloc_init_hard() { DefineGlobals(); #endif +#ifndef XP_WIN gCache.Init(); +#endif + huge_init(); sBaseAlloc.Init();