<!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>[181396] trunk</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/181396">181396</a></dd>
<dt>Author</dt> <dd>hyatt@apple.com</dd>
<dt>Date</dt> <dd>2015-03-11 11:13:45 -0700 (Wed, 11 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Optimize offsetWidth and offsetHeight to avoid doing layouts.
https://bugs.webkit.org/show_bug.cgi?id=142544

Reviewed by Beth Dakin.

Source/WebCore:

* dom/Document.cpp:
(WebCore::Document::updateLayoutIfDimensionsOutOfDate):
* dom/Document.h:
Added a new method that only updates layout if it determines that the desired dimensions are out
of date.

* dom/Element.cpp:
(WebCore::Element::offsetWidth):
(WebCore::Element::offsetHeight):
Patch offsetWidth and offsetHeight to call the new method rather than updateLayoutIgnorePendingStylesheets.

LayoutTests:

* fast/images/repaint-subrect-grid.html:
Patch this test to use the window.internals update layout method rather than
relying on document.body.offsetWidth.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsfastimagesrepaintsubrectgridhtml">trunk/LayoutTests/fast/images/repaint-subrect-grid.html</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomDocumentcpp">trunk/Source/WebCore/dom/Document.cpp</a></li>
<li><a href="#trunkSourceWebCoredomDocumenth">trunk/Source/WebCore/dom/Document.h</a></li>
<li><a href="#trunkSourceWebCoredomElementcpp">trunk/Source/WebCore/dom/Element.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/LayoutTests/ChangeLog        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2015-03-11  David Hyatt  &lt;hyatt@apple.com&gt;
+
+        Optimize offsetWidth and offsetHeight to avoid doing layouts.
+        https://bugs.webkit.org/show_bug.cgi?id=142544
+
+        Reviewed by Beth Dakin.
+
+        * fast/images/repaint-subrect-grid.html:
+        Patch this test to use the window.internals update layout method rather than
+        relying on document.body.offsetWidth.
+
</ins><span class="cx"> 2015-03-11  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Win] Test gardening after r181260
</span></span></pre></div>
<a id="trunkLayoutTestsfastimagesrepaintsubrectgridhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/images/repaint-subrect-grid.html (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/images/repaint-subrect-grid.html        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/LayoutTests/fast/images/repaint-subrect-grid.html        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -40,8 +40,9 @@
</span><span class="cx">             ++currentPos;
</span><span class="cx"> 
</span><span class="cx">             // Force a style recalc.
</span><del>-            document.body.offsetWidth;
-
</del><ins>+            if (internals)
+                internals.updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks();
+                
</ins><span class="cx">             if (window.testRunner)
</span><span class="cx">                 repaintTest();
</span><span class="cx">             else
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/ChangeLog        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2015-03-11  David Hyatt  &lt;hyatt@apple.com&gt;
+
+        Optimize offsetWidth and offsetHeight to avoid doing layouts.
+        https://bugs.webkit.org/show_bug.cgi?id=142544
+
+        Reviewed by Beth Dakin.
+
+        * dom/Document.cpp:
+        (WebCore::Document::updateLayoutIfDimensionsOutOfDate):
+        * dom/Document.h:
+        Added a new method that only updates layout if it determines that the desired dimensions are out
+        of date.
+
+        * dom/Element.cpp:
+        (WebCore::Element::offsetWidth):
+        (WebCore::Element::offsetHeight):
+        Patch offsetWidth and offsetHeight to call the new method rather than updateLayoutIgnorePendingStylesheets.
+
</ins><span class="cx"> 2015-03-11  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r181367.
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.cpp (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.cpp        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Document.cpp        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -1892,6 +1892,103 @@
</span><span class="cx">     return ensureStyleResolver().styleForElement(element, element-&gt;parentNode() ? element-&gt;parentNode()-&gt;computedStyle() : nullptr);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool Document::updateLayoutIfDimensionsOutOfDate(Element* element, DimensionsCheck dimensionsCheck)
+{
+    ASSERT(isMainThread());
+    
+    // If the stylesheets haven't loaded, just give up and do a full layout ignoring pending stylesheets.
+    if (!haveStylesheetsLoaded()) {
+        updateLayoutIgnorePendingStylesheets();
+        return true;
+    }
+    
+    // Check for re-entrancy and assert (same code that is in updateLayout()).
+    FrameView* frameView = view();
+    if (frameView &amp;&amp; frameView-&gt;isInLayout()) {
+        // View layout should not be re-entrant.
+        ASSERT_NOT_REACHED();
+        return true;
+    }
+    
+    RenderView::RepaintRegionAccumulator repaintRegionAccumulator(renderView());
+    
+    // Mimic the structure of updateLayout(), but at each step, see if we have been forced into doing a full
+    // layout.
+    bool requireFullLayout = false;
+    if (HTMLFrameOwnerElement* owner = ownerElement()) {
+        if (owner-&gt;document().updateLayoutIfDimensionsOutOfDate(owner))
+            requireFullLayout = true;
+    }
+    
+    updateStyleIfNeeded();
+    
+    RenderObject* renderer = element-&gt;renderer();
+    if (!renderer || renderer-&gt;needsLayout())
+        requireFullLayout = true;
+
+    bool isVertical = renderer &amp;&amp; !renderer-&gt;isHorizontalWritingMode();
+    bool checkingLogicalWidth = ((dimensionsCheck &amp; WidthDimensionsCheck) &amp;&amp; !isVertical) || ((dimensionsCheck &amp; HeightDimensionsCheck) &amp;&amp; isVertical);
+    bool checkingLogicalHeight = ((dimensionsCheck &amp; HeightDimensionsCheck) &amp;&amp; !isVertical) || ((dimensionsCheck &amp; WidthDimensionsCheck) &amp;&amp; !isVertical);
+    bool hasSpecifiedLogicalHeight = renderer &amp;&amp; renderer-&gt;style().logicalMinHeight() == Length(0, Fixed) &amp;&amp; renderer-&gt;style().logicalHeight().isFixed() &amp;&amp; renderer-&gt;style().logicalMaxHeight().isAuto();
+    
+    if (!requireFullLayout) {
+        RenderBox* previousBox = nullptr;
+        RenderBox* currentBox = nullptr;
+        
+        // Check our containing block chain. If anything in the chain needs a layout, then require a full layout.
+        for (RenderObject* currRenderer = element-&gt;renderer(); currRenderer &amp;&amp; !currRenderer-&gt;isRenderView(); currRenderer = currRenderer-&gt;container()) {
+            
+            // Require the entire container chain to be boxes.
+            if (!is&lt;RenderBox&gt;(currRenderer)) {
+                requireFullLayout = true;
+                break;
+            }
+            
+            previousBox = currentBox;
+            currentBox = downcast&lt;RenderBox&gt;(currRenderer);
+            
+            // If a box needs layout for itself or if a box has changed children and sizes its width to
+            // its content, then require a full layout.
+            if (currentBox-&gt;selfNeedsLayout() ||
+                (checkingLogicalWidth &amp;&amp; currRenderer-&gt;needsLayout() &amp;&amp; currentBox-&gt;sizesLogicalWidthToFitContent(MainOrPreferredSize))) {
+                requireFullLayout = true;
+                break;
+            }
+            
+            // If a block contains floats and the child's height isn't specified, then
+            // give up also, since our height could end up being influenced by the floats.
+            if (checkingLogicalHeight &amp;&amp; !hasSpecifiedLogicalHeight &amp;&amp; currentBox-&gt;isRenderBlockFlow()) {
+                RenderBlockFlow* currentBlockFlow = downcast&lt;RenderBlockFlow&gt;(currentBox);
+                if (currentBlockFlow-&gt;containsFloats() &amp;&amp; previousBox &amp;&amp; !previousBox-&gt;isFloatingOrOutOfFlowPositioned()) {
+                    requireFullLayout = true;
+                    break;
+                }
+            }
+            
+            if (!currentBox-&gt;isRenderBlockFlow() || currentBox-&gt;isInline() || currentBox-&gt;isOutOfFlowPositioned() || currentBox-&gt;flowThreadContainingBlock() || currentBox-&gt;isWritingModeRoot()) {
+                // FIXME: For now require only block-level non-positioned
+                // block flows all the way back to the root. This limits the optimization
+                // for now, and we'll expand it in future patches to apply to more and more scenarios.
+                // Disallow regions/columns from having the optimization.
+                // Give up if the writing mode changes at all in the containing block chain.
+                requireFullLayout = true;
+                break;
+            }
+            
+            if (currRenderer == frameView-&gt;layoutRoot())
+                break;
+        }
+    }
+    
+    StackStats::LayoutCheckPoint layoutCheckPoint;
+
+    // Only do a layout if changes have occurred that make it necessary.      
+    if (requireFullLayout &amp;&amp; frameView &amp;&amp; renderView() &amp;&amp; (frameView-&gt;layoutPending() || renderView()-&gt;needsLayout()))
+        frameView-&gt;layout();
+    
+    return requireFullLayout;
+}
+
</ins><span class="cx"> bool Document::isPageBoxVisible(int pageIndex)
</span><span class="cx"> {
</span><span class="cx">     Ref&lt;RenderStyle&gt; pageStyle(ensureStyleResolver().styleForPage(pageIndex));
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.h (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.h        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Document.h        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -258,6 +258,8 @@
</span><span class="cx">     LimitedQuirksMode = 1 &lt;&lt; 2
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+enum DimensionsCheck { WidthDimensionsCheck = 0x1, HeightDimensionsCheck = 0x2, AllDimensionsCheck = 0x3 };
+
</ins><span class="cx"> class Document : public ContainerNode, public TreeScope, public ScriptExecutionContext, public FontSelectorClient {
</span><span class="cx"> public:
</span><span class="cx">     static Ref&lt;Document&gt; create(Frame* frame, const URL&amp; url)
</span><span class="lines">@@ -564,7 +566,9 @@
</span><span class="cx"> 
</span><span class="cx">     bool renderTreeBeingDestroyed() const { return m_renderTreeBeingDestroyed; }
</span><span class="cx">     bool hasLivingRenderTree() const { return renderView() &amp;&amp; !renderTreeBeingDestroyed(); }
</span><del>-
</del><ins>+    
+    bool updateLayoutIfDimensionsOutOfDate(Element*, DimensionsCheck = AllDimensionsCheck);
+    
</ins><span class="cx">     AXObjectCache* existingAXObjectCache() const;
</span><span class="cx">     WEBCORE_EXPORT AXObjectCache* axObjectCache() const;
</span><span class="cx">     void clearAXObjectCache();
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Element.cpp (181395 => 181396)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Element.cpp        2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Element.cpp        2015-03-11 18:13:45 UTC (rev 181396)
</span><span class="lines">@@ -723,7 +723,7 @@
</span><span class="cx"> 
</span><span class="cx"> double Element::offsetWidth()
</span><span class="cx"> {
</span><del>-    document().updateLayoutIgnorePendingStylesheets();
</del><ins>+    document().updateLayoutIfDimensionsOutOfDate(this, WidthDimensionsCheck);
</ins><span class="cx">     if (RenderBoxModelObject* renderer = renderBoxModelObject()) {
</span><span class="cx">         LayoutUnit offsetWidth = subpixelMetricsEnabled(renderer-&gt;document()) ? renderer-&gt;offsetWidth() : LayoutUnit(renderer-&gt;pixelSnappedOffsetWidth());
</span><span class="cx">         return convertToNonSubpixelValueIfNeeded(adjustLayoutUnitForAbsoluteZoom(offsetWidth, *renderer).toDouble(), renderer-&gt;document());
</span><span class="lines">@@ -733,7 +733,7 @@
</span><span class="cx"> 
</span><span class="cx"> double Element::offsetHeight()
</span><span class="cx"> {
</span><del>-    document().updateLayoutIgnorePendingStylesheets();
</del><ins>+    document().updateLayoutIfDimensionsOutOfDate(this, HeightDimensionsCheck);
</ins><span class="cx">     if (RenderBoxModelObject* renderer = renderBoxModelObject()) {
</span><span class="cx">         LayoutUnit offsetHeight = subpixelMetricsEnabled(renderer-&gt;document()) ? renderer-&gt;offsetHeight() : LayoutUnit(renderer-&gt;pixelSnappedOffsetHeight());
</span><span class="cx">         return convertToNonSubpixelValueIfNeeded(adjustLayoutUnitForAbsoluteZoom(offsetHeight, *renderer).toDouble(), renderer-&gt;document());
</span></span></pre>
</div>
</div>

</body>
</html>