Changeset 241789 in webkit


Ignore:
Timestamp:
Feb 19, 2019 5:51:50 PM (5 years ago)
Author:
ysuzuki@apple.com
Message:

[bmalloc] bmalloc::Cache should not be instantiated if we are using system malloc
https://bugs.webkit.org/show_bug.cgi?id=194811

Reviewed by Mark Lam.

bmalloc::Cache is very large. It is 13KB. Since it exists per HeapKind, it takes 40KB.
But this is meaningless if we are under the system malloc mode by using "Malloc=1". We
found that it continues using so much dirty memory region even under the system malloc mode.
This patch avoids instantiation of bmalloc::Cache under the system malloc mode.

  • bmalloc/Allocator.cpp:

(bmalloc::Allocator::Allocator):
(bmalloc::Allocator::tryAllocate):
(bmalloc::Allocator::allocateImpl):
(bmalloc::Allocator::reallocateImpl):
(bmalloc::Allocator::allocateSlowCase):
Allocator is a per Cache object. So we no longer need to keep m_debugHeap. If debug heap is enabled,
Allocator is never created.

  • bmalloc/Allocator.h:
  • bmalloc/Cache.cpp:

(bmalloc::debugHeap):
(bmalloc::Cache::Cache):
(bmalloc::Cache::tryAllocateSlowCaseNullCache):
(bmalloc::Cache::allocateSlowCaseNullCache):
(bmalloc::Cache::deallocateSlowCaseNullCache):
(bmalloc::Cache::tryReallocateSlowCaseNullCache):
(bmalloc::Cache::reallocateSlowCaseNullCache):

  • bmalloc/Cache.h:

(bmalloc::Cache::tryAllocate):
(bmalloc::Cache::tryReallocate):
If the debug heap mode is enabled, we keep Cache::getFast() returning nullptr. And in the slow path case, we use debugHeap.
This makes bmalloc fast path fast, while we avoid Cache instantiation.

  • bmalloc/Deallocator.cpp:

(bmalloc::Deallocator::Deallocator):
(bmalloc::Deallocator::scavenge):
(bmalloc::Deallocator::deallocateSlowCase):

  • bmalloc/Deallocator.h:

Ditto for Deallocator.

Location:
trunk/Source/bmalloc
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/bmalloc/ChangeLog

    r241581 r241789  
     12019-02-19  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [bmalloc] bmalloc::Cache should not be instantiated if we are using system malloc
     4        https://bugs.webkit.org/show_bug.cgi?id=194811
     5
     6        Reviewed by Mark Lam.
     7
     8        bmalloc::Cache is very large. It is 13KB. Since it exists per HeapKind, it takes 40KB.
     9        But this is meaningless if we are under the system malloc mode by using "Malloc=1". We
     10        found that it continues using so much dirty memory region even under the system malloc mode.
     11        This patch avoids instantiation of bmalloc::Cache under the system malloc mode.
     12
     13        * bmalloc/Allocator.cpp:
     14        (bmalloc::Allocator::Allocator):
     15        (bmalloc::Allocator::tryAllocate):
     16        (bmalloc::Allocator::allocateImpl):
     17        (bmalloc::Allocator::reallocateImpl):
     18        (bmalloc::Allocator::allocateSlowCase):
     19        Allocator is a per Cache object. So we no longer need to keep m_debugHeap. If debug heap is enabled,
     20        Allocator is never created.
     21
     22        * bmalloc/Allocator.h:
     23        * bmalloc/Cache.cpp:
     24        (bmalloc::debugHeap):
     25        (bmalloc::Cache::Cache):
     26        (bmalloc::Cache::tryAllocateSlowCaseNullCache):
     27        (bmalloc::Cache::allocateSlowCaseNullCache):
     28        (bmalloc::Cache::deallocateSlowCaseNullCache):
     29        (bmalloc::Cache::tryReallocateSlowCaseNullCache):
     30        (bmalloc::Cache::reallocateSlowCaseNullCache):
     31        * bmalloc/Cache.h:
     32        (bmalloc::Cache::tryAllocate):
     33        (bmalloc::Cache::tryReallocate):
     34        If the debug heap mode is enabled, we keep Cache::getFast() returning nullptr. And in the slow path case, we use debugHeap.
     35        This makes bmalloc fast path fast, while we avoid Cache instantiation.
     36
     37        * bmalloc/Deallocator.cpp:
     38        (bmalloc::Deallocator::Deallocator):
     39        (bmalloc::Deallocator::scavenge):
     40        (bmalloc::Deallocator::deallocateSlowCase):
     41        * bmalloc/Deallocator.h:
     42        Ditto for Deallocator.
     43
    1442019-02-15  Yusuke Suzuki  <ysuzuki@apple.com>
    245
  • trunk/Source/bmalloc/bmalloc/Allocator.cpp

    r237577 r241789  
    2828#include "Chunk.h"
    2929#include "Deallocator.h"
    30 #include "DebugHeap.h"
     30#include "Environment.h"
    3131#include "Heap.h"
    3232#include "PerProcess.h"
     
    3939Allocator::Allocator(Heap& heap, Deallocator& deallocator)
    4040    : m_heap(heap)
    41     , m_debugHeap(heap.debugHeap())
    4241    , m_deallocator(deallocator)
    4342{
     43    BASSERT(!PerProcess<Environment>::get()->isDebugHeapEnabled());
    4444    for (size_t sizeClass = 0; sizeClass < sizeClassCount; ++sizeClass)
    4545        m_bumpAllocators[sizeClass].init(objectSize(sizeClass));
     
    5353void* Allocator::tryAllocate(size_t size)
    5454{
    55     if (m_debugHeap)
    56         return m_debugHeap->malloc(size);
    57 
    5855    if (size <= smallMax)
    5956        return allocate(size);
     
    7875{
    7976    BASSERT(isPowerOfTwo(alignment));
    80 
    81     if (m_debugHeap)
    82         return m_debugHeap->memalign(alignment, size, crashOnFailure);
    8377
    8478    if (!size)
     
    108102void* Allocator::reallocateImpl(void* object, size_t newSize, bool crashOnFailure)
    109103{
    110     if (m_debugHeap)
    111         return m_debugHeap->realloc(object, newSize, crashOnFailure);
    112 
    113104    size_t oldSize = 0;
    114105    switch (objectType(m_heap.kind(), object)) {
     
    201192void* Allocator::allocateSlowCase(size_t size)
    202193{
    203     if (m_debugHeap)
    204         return m_debugHeap->malloc(size);
    205 
    206194    if (size <= maskSizeClassMax) {
    207195        size_t sizeClass = bmalloc::maskSizeClass(size);
  • trunk/Source/bmalloc/bmalloc/Allocator.h

    r237577 r241789  
    3434
    3535class Deallocator;
    36 class DebugHeap;
    3736class Heap;
    3837
     
    7069
    7170    Heap& m_heap;
    72     DebugHeap* m_debugHeap;
    7371    Deallocator& m_deallocator;
    7472};
  • trunk/Source/bmalloc/bmalloc/Cache.cpp

    r223239 r241789  
    2626#include "BInline.h"
    2727#include "Cache.h"
     28#include "DebugHeap.h"
     29#include "Environment.h"
    2830#include "Heap.h"
    2931#include "PerProcess.h"
    3032
    3133namespace bmalloc {
     34
     35static DebugHeap* debugHeapCache { nullptr };
    3236
    3337void Cache::scavenge(HeapKind heapKind)
     
    4347}
    4448
     49static BINLINE DebugHeap* debugHeap()
     50{
     51    if (debugHeapCache)
     52        return debugHeapCache;
     53    if (PerProcess<Environment>::get()->isDebugHeapEnabled()) {
     54        debugHeapCache = PerProcess<DebugHeap>::get();
     55        return debugHeapCache;
     56    }
     57    return nullptr;
     58}
     59
    4560Cache::Cache(HeapKind heapKind)
    4661    : m_deallocator(PerProcess<PerHeapKind<Heap>>::get()->at(heapKind))
    4762    , m_allocator(PerProcess<PerHeapKind<Heap>>::get()->at(heapKind), m_deallocator)
    4863{
     64    BASSERT(!PerProcess<Environment>::get()->isDebugHeapEnabled());
    4965}
    5066
    5167BNO_INLINE void* Cache::tryAllocateSlowCaseNullCache(HeapKind heapKind, size_t size)
    5268{
     69    // FIXME: DebugHeap does not have tryAllocate feature.
     70    // https://bugs.webkit.org/show_bug.cgi?id=194837
     71    if (auto* heap = debugHeap())
     72        return heap->malloc(size);
    5373    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryAllocate(size);
    5474}
     
    5676BNO_INLINE void* Cache::allocateSlowCaseNullCache(HeapKind heapKind, size_t size)
    5777{
     78    if (auto* heap = debugHeap())
     79        return heap->malloc(size);
    5880    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().allocate(size);
     81}
     82
     83BNO_INLINE void* Cache::tryAllocateSlowCaseNullCache(HeapKind heapKind, size_t alignment, size_t size)
     84{
     85    if (auto* heap = debugHeap()) {
     86        constexpr bool crashOnFailure = false;
     87        return heap->memalign(alignment, size, crashOnFailure);
     88    }
     89    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryAllocate(alignment, size);
    5990}
    6091
    6192BNO_INLINE void* Cache::allocateSlowCaseNullCache(HeapKind heapKind, size_t alignment, size_t size)
    6293{
     94    if (auto* heap = debugHeap()) {
     95        constexpr bool crashOnFailure = true;
     96        return heap->memalign(alignment, size, crashOnFailure);
     97    }
    6398    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().allocate(alignment, size);
    6499}
     
    66101BNO_INLINE void Cache::deallocateSlowCaseNullCache(HeapKind heapKind, void* object)
    67102{
     103    if (auto* heap = debugHeap()) {
     104        heap->free(object);
     105        return;
     106    }
    68107    PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).deallocator().deallocate(object);
     108}
     109
     110BNO_INLINE void* Cache::tryReallocateSlowCaseNullCache(HeapKind heapKind, void* object, size_t newSize)
     111{
     112    if (auto* heap = debugHeap()) {
     113        constexpr bool crashOnFailure = false;
     114        return heap->realloc(object, newSize, crashOnFailure);
     115    }
     116    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryReallocate(object, newSize);
    69117}
    70118
    71119BNO_INLINE void* Cache::reallocateSlowCaseNullCache(HeapKind heapKind, void* object, size_t newSize)
    72120{
     121    if (auto* heap = debugHeap()) {
     122        constexpr bool crashOnFailure = true;
     123        return heap->realloc(object, newSize, crashOnFailure);
     124    }
    73125    return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().reallocate(object, newSize);
    74126}
  • trunk/Source/bmalloc/bmalloc/Cache.h

    r237577 r241789  
    5757    BEXPORT static void* tryAllocateSlowCaseNullCache(HeapKind, size_t);
    5858    BEXPORT static void* allocateSlowCaseNullCache(HeapKind, size_t);
     59    BEXPORT static void* tryAllocateSlowCaseNullCache(HeapKind, size_t alignment, size_t);
    5960    BEXPORT static void* allocateSlowCaseNullCache(HeapKind, size_t alignment, size_t);
    6061    BEXPORT static void deallocateSlowCaseNullCache(HeapKind, void*);
     62    BEXPORT static void* tryReallocateSlowCaseNullCache(HeapKind, void*, size_t);
    6163    BEXPORT static void* reallocateSlowCaseNullCache(HeapKind, void*, size_t);
    6264
     
    8587    PerHeapKind<Cache>* caches = PerThread<PerHeapKind<Cache>>::getFastCase();
    8688    if (!caches)
    87         return allocateSlowCaseNullCache(heapKind, alignment, size);
     89        return tryAllocateSlowCaseNullCache(heapKind, alignment, size);
    8890    return caches->at(mapToActiveHeapKindAfterEnsuringGigacage(heapKind)).allocator().tryAllocate(alignment, size);
    8991}
     
    109111    PerHeapKind<Cache>* caches = PerThread<PerHeapKind<Cache>>::getFastCase();
    110112    if (!caches)
    111         return reallocateSlowCaseNullCache(heapKind, object, newSize);
     113        return tryReallocateSlowCaseNullCache(heapKind, object, newSize);
    112114    return caches->at(mapToActiveHeapKindAfterEnsuringGigacage(heapKind)).allocator().tryReallocate(object, newSize);
    113115}
  • trunk/Source/bmalloc/bmalloc/Deallocator.cpp

    r231403 r241789  
    2828#include "Chunk.h"
    2929#include "Deallocator.h"
    30 #include "DebugHeap.h"
     30#include "Environment.h"
    3131#include "Heap.h"
    3232#include "Object.h"
     
    4040Deallocator::Deallocator(Heap& heap)
    4141    : m_heap(heap)
    42     , m_debugHeap(heap.debugHeap())
    4342{
    44     if (m_debugHeap) {
    45         // Fill the object log in order to disable the fast path.
    46         while (m_objectLog.size() != m_objectLog.capacity())
    47             m_objectLog.push(nullptr);
    48     }
     43    BASSERT(!PerProcess<Environment>::get()->isDebugHeapEnabled());
    4944}
    5045
     
    5651void Deallocator::scavenge()
    5752{
    58     if (m_debugHeap)
    59         return;
    60 
    6153    std::unique_lock<Mutex> lock(Heap::mutex());
    6254
     
    7466void Deallocator::deallocateSlowCase(void* object)
    7567{
    76     if (m_debugHeap)
    77         return m_debugHeap->free(object);
    78 
    7968    if (!object)
    8069        return;
  • trunk/Source/bmalloc/bmalloc/Deallocator.h

    r230501 r241789  
    3434namespace bmalloc {
    3535
    36 class DebugHeap;
    3736class Heap;
    3837class Mutex;
     
    5958    FixedVector<void*, deallocatorLogCapacity> m_objectLog;
    6059    LineCache m_lineCache; // The Heap removes items from this cache.
    61     DebugHeap* m_debugHeap;
    6260};
    6361
Note: See TracChangeset for help on using the changeset viewer.