• mhahnenberg@apple.com's avatar
    Implement a new allocator for backing stores · 5d0b30a2
    mhahnenberg@apple.com authored
    Reviewed by Filip Pizlo.
    We want to move away from using fastMalloc for the backing stores for 
    some of our objects (e.g. JSArray, JSObject, JSString, etc).  These backing 
    stores have a nice property in that they only have a single owner (i.e. a 
    single pointer to them at any one time).  One way that we can take advantage 
    of this property is to implement a simple bump allocator/copying collector, 
    which will run alongside our normal mark/sweep collector, that only needs to 
    update the single owner pointer rather than having to redirect an arbitrary 
    number of pointers in from-space to to-space.
    This plan can give us a number of benefits. We can beat fastMalloc in terms 
    of both performance and memory usage, we can track how much memory we're using 
    far more accurately than our rough estimation now through the use of 
    reportExtraMemoryCost, and we can allocate arbitrary size objects (as opposed 
    to being limited to size classes like we have been historically). This is also 
    another step toward moving away from lazy destruction, which will improve our memory footprint.
    We start by creating said allocator and moving the ArrayStorage for JSArray 
    to use it rather than fastMalloc.
    The design of the collector is as follows:
    -The collector allocates 64KB chunks from the OS to use for object allocation.
    -Each chunk contains an offset, a flag indicating if the block has been pinned, 
     and a payload, along with next and prev pointers so that they can be put in DoublyLinkedLists.
    -Any allocation greater than 64KB gets its own separate oversize block, which 
     is managed separately from the rest.
    -If the allocator receives a request for more than the remaining amount in the 
     current block, it grabs a fresh block.
    -Grabbing a fresh block means grabbing one off of the global free list (which is now 
     shared between the mark/sweep allocator and the bump allocator) if there is one. 
     If there isn't a new one we do one of two things: allocate a new block from the OS 
     if we're not ready for a GC yet, or run a GC and then try again. If we still don't 
     have enough space after the GC, we allocate a new block from the OS.
    Garbage collection:
    -At the start of garbage collection during conservative stack scanning, if we encounter 
     what appears to be a pointer to a bump-allocated block of memory, we pin that block so 
     that it will not be copied for this round of collection.
    -We also pin any oversize blocks that we encounter, which effectively doubles as a 
     "mark bit" for that block. Any oversize blocks that aren't pinned at the end of copying 
     are given back to the OS.
    -Marking threads are now also responsible for copying bump-allocated objects to newSpace
    -Each marking thread has a private 64KB block into which it copies bump-allocated objects that it encounters.
    -When that block fills up, the marking thread gives it back to the allocator and requests a new one.
    -When all marking has concluded, each thread gives back its copy block, even if it isn't full.
    -At the conclusion of copying (which is done by the end of the marking phase), we un-pin 
     any pinned blocks and give any blocks left in from-space to the global free list.
    * CMakeLists.txt:
    * GNUmakefile.list.am:
    * JavaScriptCore.gypi:
    * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
    * JavaScriptCore.vcproj/WTF/WTF.vcproj:
    * JavaScriptCore.xcodeproj/project.pbxproj:
    * Target.pri:
    * heap/AllocationSpace.cpp:
    * heap/AllocationSpace.h:
    * heap/BumpBlock.h: Added.
    * heap/BumpSpace.cpp: Added.
    * heap/BumpSpace.h: Added.
    * heap/BumpSpaceInlineMethods.h: Added.
    * heap/ConservativeRoots.cpp:
    * heap/ConservativeRoots.h:
    * heap/Heap.cpp:
    * heap/Heap.h:
    * heap/HeapBlock.h: Added.
    * heap/MarkStack.cpp:
    * heap/MarkStack.h:
    * heap/MarkedBlock.cpp:
    * heap/MarkedBlock.h:
    * heap/MarkedSpace.cpp:
    * heap/MarkedSpace.h:
    * heap/SlotVisitor.h:
    * heap/TinyBloomFilter.h:
    * runtime/JSArray.cpp:
    * runtime/JSArray.h:
    * wtf/CheckedBoolean.h: Added.
    (CheckedBoolean::operator bool):
    * wtf/DoublyLinkedList.h:
    * wtf/StdLibExtras.h:
    Added forwarding header for new CheckedBoolean used in the bump allocator.
    * ForwardingHeaders/wtf/CheckedBoolean.h: Added.
    No new tests.
    Added forwarding header for new CheckedBoolean used in the bump allocator.
    * ForwardingHeaders/wtf/CheckedBoolean.h: Added.
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@105442 268f45cc-cd09-0410-ab3c-d52691b4dbfc
JSArray.cpp 71.5 KB