<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[165458] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/165458">165458</a></dd>
<dt>Author</dt> <dd>mhahnenberg@apple.com</dd>
<dt>Date</dt> <dd>2014-03-11 18:45:54 -0700 (Tue, 11 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>MarkedBlocks that are &quot;full enough&quot; shouldn't be swept after EdenCollections
https://bugs.webkit.org/show_bug.cgi?id=129920

Reviewed by Geoffrey Garen.

This patch introduces the notion of &quot;retiring&quot; MarkedBlocks. We retire a MarkedBlock
when the amount of free space in a MarkedBlock drops below a certain threshold.
Retired blocks are not considered for sweeping.

This is profitable because it reduces churn during sweeping. To build a free list, 
we have to scan through each cell in a block. After a collection, all objects that 
are live in the block will remain live until the next FullCollection, at which time
we un-retire all previously retired blocks. Thus, a small number of objects in a block
that die during each EdenCollection could cause us to do a disproportiante amount of 
sweeping for how much free memory we get back.

This patch looks like a consistent ~2% progression on boyer and is neutral everywhere else.

* heap/Heap.h:
(JSC::Heap::didRetireBlockWithFreeListSize):
* heap/MarkedAllocator.cpp:
(JSC::MarkedAllocator::tryAllocateHelper):
(JSC::MarkedAllocator::removeBlock):
(JSC::MarkedAllocator::reset):
* heap/MarkedAllocator.h:
(JSC::MarkedAllocator::MarkedAllocator):
(JSC::MarkedAllocator::forEachBlock):
* heap/MarkedBlock.cpp:
(JSC::MarkedBlock::sweepHelper):
(JSC::MarkedBlock::clearMarksWithCollectionType):
(JSC::MarkedBlock::didRetireBlock):
* heap/MarkedBlock.h:
(JSC::MarkedBlock::willRemoveBlock):
(JSC::MarkedBlock::isLive):
* heap/MarkedSpace.cpp:
(JSC::MarkedSpace::clearNewlyAllocated):
(JSC::MarkedSpace::clearMarks):
* runtime/Options.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedAllocatorcpp">trunk/Source/JavaScriptCore/heap/MarkedAllocator.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedAllocatorh">trunk/Source/JavaScriptCore/heap/MarkedAllocator.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedBlockcpp">trunk/Source/JavaScriptCore/heap/MarkedBlock.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedBlockh">trunk/Source/JavaScriptCore/heap/MarkedBlock.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedSpacecpp">trunk/Source/JavaScriptCore/heap/MarkedSpace.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeOptionsh">trunk/Source/JavaScriptCore/runtime/Options.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/ChangeLog        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -1,3 +1,44 @@
</span><ins>+2014-03-11  Mark Hahnenberg  &lt;mhahnenberg@apple.com&gt;
+
+        MarkedBlocks that are &quot;full enough&quot; shouldn't be swept after EdenCollections
+        https://bugs.webkit.org/show_bug.cgi?id=129920
+
+        Reviewed by Geoffrey Garen.
+
+        This patch introduces the notion of &quot;retiring&quot; MarkedBlocks. We retire a MarkedBlock
+        when the amount of free space in a MarkedBlock drops below a certain threshold.
+        Retired blocks are not considered for sweeping.
+
+        This is profitable because it reduces churn during sweeping. To build a free list, 
+        we have to scan through each cell in a block. After a collection, all objects that 
+        are live in the block will remain live until the next FullCollection, at which time
+        we un-retire all previously retired blocks. Thus, a small number of objects in a block
+        that die during each EdenCollection could cause us to do a disproportiante amount of 
+        sweeping for how much free memory we get back.
+
+        This patch looks like a consistent ~2% progression on boyer and is neutral everywhere else.
+
+        * heap/Heap.h:
+        (JSC::Heap::didRetireBlockWithFreeListSize):
+        * heap/MarkedAllocator.cpp:
+        (JSC::MarkedAllocator::tryAllocateHelper):
+        (JSC::MarkedAllocator::removeBlock):
+        (JSC::MarkedAllocator::reset):
+        * heap/MarkedAllocator.h:
+        (JSC::MarkedAllocator::MarkedAllocator):
+        (JSC::MarkedAllocator::forEachBlock):
+        * heap/MarkedBlock.cpp:
+        (JSC::MarkedBlock::sweepHelper):
+        (JSC::MarkedBlock::clearMarksWithCollectionType):
+        (JSC::MarkedBlock::didRetireBlock):
+        * heap/MarkedBlock.h:
+        (JSC::MarkedBlock::willRemoveBlock):
+        (JSC::MarkedBlock::isLive):
+        * heap/MarkedSpace.cpp:
+        (JSC::MarkedSpace::clearNewlyAllocated):
+        (JSC::MarkedSpace::clearMarks):
+        * runtime/Options.h:
+
</ins><span class="cx"> 2014-03-11  Andreas Kling  &lt;akling@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Streamline PropertyTable for lookup-only access.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedAllocatorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedAllocator.cpp (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedAllocator.cpp        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/heap/MarkedAllocator.cpp        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -79,12 +79,12 @@
</span><span class="cx"> 
</span><span class="cx">             MarkedBlock::FreeList freeList = block-&gt;sweep(MarkedBlock::SweepToFreeList);
</span><span class="cx">             
</span><del>-            if (!freeList.head) {
-                block-&gt;didConsumeEmptyFreeList();
</del><ins>+            double utilization = ((double)MarkedBlock::blockSize - (double)freeList.bytes) / (double)MarkedBlock::blockSize;
+            if (utilization &gt;= Options::minMarkedBlockUtilization()) {
+                ASSERT(freeList.bytes || !freeList.head);
</ins><span class="cx">                 m_blockList.remove(block);
</span><del>-                m_blockList.push(block);
-                if (!m_lastFullBlock)
-                    m_lastFullBlock = block;
</del><ins>+                m_retiredBlocks.push(block);
+                block-&gt;didRetireBlock(freeList);
</ins><span class="cx">                 continue;
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="lines">@@ -211,9 +211,7 @@
</span><span class="cx">     if (m_nextBlockToSweep == block)
</span><span class="cx">         m_nextBlockToSweep = m_nextBlockToSweep-&gt;next();
</span><span class="cx"> 
</span><del>-    if (block == m_lastFullBlock)
-        m_lastFullBlock = m_lastFullBlock-&gt;prev();
-    
</del><ins>+    block-&gt;willRemoveBlock();
</ins><span class="cx">     m_blockList.remove(block);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -223,12 +221,9 @@
</span><span class="cx">     m_currentBlock = 0;
</span><span class="cx">     m_freeList = MarkedBlock::FreeList();
</span><span class="cx">     if (m_heap-&gt;operationInProgress() == FullCollection)
</span><del>-        m_lastFullBlock = 0;
</del><ins>+        m_blockList.append(m_retiredBlocks);
</ins><span class="cx"> 
</span><del>-    if (m_lastFullBlock)
-        m_nextBlockToSweep = m_lastFullBlock-&gt;next() ? m_lastFullBlock-&gt;next() : m_lastFullBlock;
-    else
-        m_nextBlockToSweep = m_blockList.head();
</del><ins>+    m_nextBlockToSweep = m_blockList.head();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace JSC
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedAllocatorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedAllocator.h (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedAllocator.h        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/heap/MarkedAllocator.h        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -54,8 +54,8 @@
</span><span class="cx">     MarkedBlock* m_currentBlock;
</span><span class="cx">     MarkedBlock* m_lastActiveBlock;
</span><span class="cx">     MarkedBlock* m_nextBlockToSweep;
</span><del>-    MarkedBlock* m_lastFullBlock;
</del><span class="cx">     DoublyLinkedList&lt;MarkedBlock&gt; m_blockList;
</span><ins>+    DoublyLinkedList&lt;MarkedBlock&gt; m_retiredBlocks;
</ins><span class="cx">     size_t m_cellSize;
</span><span class="cx">     MarkedBlock::DestructorType m_destructorType;
</span><span class="cx">     Heap* m_heap;
</span><span class="lines">@@ -71,7 +71,6 @@
</span><span class="cx">     : m_currentBlock(0)
</span><span class="cx">     , m_lastActiveBlock(0)
</span><span class="cx">     , m_nextBlockToSweep(0)
</span><del>-    , m_lastFullBlock(0)
</del><span class="cx">     , m_cellSize(0)
</span><span class="cx">     , m_destructorType(MarkedBlock::None)
</span><span class="cx">     , m_heap(0)
</span><span class="lines">@@ -136,6 +135,11 @@
</span><span class="cx">         next = block-&gt;next();
</span><span class="cx">         functor(block);
</span><span class="cx">     }
</span><ins>+
+    for (MarkedBlock* block = m_retiredBlocks.head(); block; block = next) {
+        next = block-&gt;next();
+        functor(block);
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace JSC
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedBlockcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedBlock.cpp (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedBlock.cpp        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/heap/MarkedBlock.cpp        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -129,6 +129,7 @@
</span><span class="cx">         // Happens when a block transitions to fully allocated.
</span><span class="cx">         ASSERT(sweepMode == SweepToFreeList);
</span><span class="cx">         return FreeList();
</span><ins>+    case Retired:
</ins><span class="cx">     case Allocated:
</span><span class="cx">         RELEASE_ASSERT_NOT_REACHED();
</span><span class="cx">         return FreeList();
</span><span class="lines">@@ -226,11 +227,16 @@
</span><span class="cx"> #if ENABLE(GGC)
</span><span class="cx">         m_rememberedSet.clearAll();
</span><span class="cx"> #endif
</span><ins>+        // This will become true at the end of the mark phase. We set it now to
+        // avoid an extra pass to do so later.
+        m_state = Marked;
+        return;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // This will become true at the end of the mark phase. We set it now to
-    // avoid an extra pass to do so later.
-    m_state = Marked;
</del><ins>+    ASSERT(collectionType == EdenCollection);
+    // If a block was retired then there's no way an EdenCollection can un-retire it.
+    if (m_state != Retired)
+        m_state = Marked;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MarkedBlock::lastChanceToFinalize()
</span><span class="lines">@@ -258,4 +264,27 @@
</span><span class="cx">     return sweep(SweepToFreeList);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MarkedBlock::didRetireBlock(const FreeList&amp; freeList)
+{
+    HEAP_LOG_BLOCK_STATE_TRANSITION(this);
+    FreeCell* head = freeList.head;
+
+    // Currently we don't notify the Heap that we're giving up on this block. 
+    // The Heap might be able to make a better decision about how many bytes should 
+    // be allocated before the next collection if it knew about this retired block.
+    // On the other hand we'll waste at most 10% of our Heap space between FullCollections 
+    // and only under heavy fragmentation.
+
+    // We need to zap the free list when retiring a block so that we don't try to destroy 
+    // previously destroyed objects when we re-sweep the block in the future.
+    FreeCell* next;
+    for (FreeCell* current = head; current; current = next) {
+        next = current-&gt;next;
+        reinterpret_cast&lt;JSCell*&gt;(current)-&gt;zap();
+    }
+
+    ASSERT(m_state == FreeListed);
+    m_state = Retired;
+}
+
</ins><span class="cx"> } // namespace JSC
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedBlockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedBlock.h (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedBlock.h        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/heap/MarkedBlock.h        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -177,6 +177,8 @@
</span><span class="cx">         void clearNewlyAllocated(const void*);
</span><span class="cx"> 
</span><span class="cx">         bool needsSweeping();
</span><ins>+        void didRetireBlock(const FreeList&amp;);
+        void willRemoveBlock();
</ins><span class="cx"> 
</span><span class="cx">         template &lt;typename Functor&gt; void forEachCell(Functor&amp;);
</span><span class="cx">         template &lt;typename Functor&gt; void forEachLiveCell(Functor&amp;);
</span><span class="lines">@@ -187,7 +189,7 @@
</span><span class="cx">     private:
</span><span class="cx">         static const size_t atomAlignmentMask = atomSize - 1; // atomSize must be a power of two.
</span><span class="cx"> 
</span><del>-        enum BlockState { New, FreeListed, Allocated, Marked };
</del><ins>+        enum BlockState { New, FreeListed, Allocated, Marked, Retired };
</ins><span class="cx">         template&lt;DestructorType&gt; FreeList sweepHelper(SweepMode = SweepOnly);
</span><span class="cx"> 
</span><span class="cx">         typedef char Atom[atomSize];
</span><span class="lines">@@ -282,6 +284,11 @@
</span><span class="cx">         m_weakSet.reap();
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    inline void MarkedBlock::willRemoveBlock()
+    {
+        ASSERT(m_state != Retired);
+    }
+
</ins><span class="cx">     inline void MarkedBlock::didConsumeFreeList()
</span><span class="cx">     {
</span><span class="cx">         HEAP_LOG_BLOCK_STATE_TRANSITION(this);
</span><span class="lines">@@ -405,6 +412,7 @@
</span><span class="cx">         case Allocated:
</span><span class="cx">             return true;
</span><span class="cx"> 
</span><ins>+        case Retired:
</ins><span class="cx">         case Marked:
</span><span class="cx">             return m_marks.get(atomNumber(cell)) || (m_newlyAllocated &amp;&amp; isNewlyAllocated(cell));
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedSpacecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedSpace.cpp (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedSpace.cpp        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/heap/MarkedSpace.cpp        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -317,12 +317,6 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-#ifndef NDEBUG
-struct VerifyMarked : MarkedBlock::VoidFunctor {
-    void operator()(MarkedBlock* block) { ASSERT(block-&gt;needsSweeping()); }
-};
-#endif
-
</del><span class="cx"> void MarkedSpace::clearMarks()
</span><span class="cx"> {
</span><span class="cx">     if (m_heap-&gt;operationInProgress() == EdenCollection) {
</span><span class="lines">@@ -330,9 +324,6 @@
</span><span class="cx">             m_blocksWithNewObjects[i]-&gt;clearMarks();
</span><span class="cx">     } else
</span><span class="cx">         forEachBlock&lt;ClearMarks&gt;();
</span><del>-#ifndef NDEBUG
-    forEachBlock&lt;VerifyMarked&gt;();
-#endif
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MarkedSpace::willStartIterating()
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeOptionsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/Options.h (165457 => 165458)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/Options.h        2014-03-12 00:19:40 UTC (rev 165457)
+++ trunk/Source/JavaScriptCore/runtime/Options.h        2014-03-12 01:45:54 UTC (rev 165458)
</span><span class="lines">@@ -238,6 +238,7 @@
</span><span class="cx">     v(unsigned, opaqueRootMergeThreshold, 1000) \
</span><span class="cx">     v(double, minHeapUtilization, 0.8) \
</span><span class="cx">     v(double, minCopiedBlockUtilization, 0.9) \
</span><ins>+    v(double, minMarkedBlockUtilization, 0.9) \
</ins><span class="cx">     \
</span><span class="cx">     v(bool, forceWeakRandomSeed, false) \
</span><span class="cx">     v(unsigned, forcedWeakRandomSeed, 0) \
</span></span></pre>
</div>
</div>

</body>
</html>