<!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>[182604] 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/182604">182604</a></dd>
<dt>Author</dt> <dd>zalan@apple.com</dd>
<dt>Date</dt> <dd>2015-04-09 11:57:33 -0700 (Thu, 09 Apr 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Simple line layout(regression): Calling innerText on RenderFlow with multiple children is slow.
https://bugs.webkit.org/show_bug.cgi?id=143554

Reviewed by Antti Koivisto.

Initialize render flow's segments only when the render flow changes in TextIterator.
The included performance test shows 6x speedup. (from ~10 runs/sec to ~60 runs/sec)

PerformanceTests:

* Layout/simple-line-layout-innertext.html: Added.

Source/WebCore:

Test: PerformanceTests/Layout/simple-line-layout-innertext.html.

* editing/TextIterator.cpp:
(WebCore::TextIterator::handleTextNode):
* editing/TextIterator.h:
* rendering/SimpleLineLayoutFlowContents.cpp: Instruments log shows that vector's expandCapacity could be expensive when flow has large amount of children.
(WebCore::SimpleLineLayout::initializeSegments):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkPerformanceTestsChangeLog">trunk/PerformanceTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreeditingTextIteratorcpp">trunk/Source/WebCore/editing/TextIterator.cpp</a></li>
<li><a href="#trunkSourceWebCoreeditingTextIteratorh">trunk/Source/WebCore/editing/TextIterator.h</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutFlowContentscpp">trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.cpp</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutResolvercpp">trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.cpp</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutResolverh">trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkPerformanceTestsLayoutsimplelinelayoutinnertexthtml">trunk/PerformanceTests/Layout/simple-line-layout-innertext.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkPerformanceTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/PerformanceTests/ChangeLog (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/PerformanceTests/ChangeLog        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/PerformanceTests/ChangeLog        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2015-04-09  Zalan Bujtas  &lt;zalan@apple.com&gt;
+
+        Simple line layout(regression): Calling innerText on RenderFlow with multiple children is slow.
+        https://bugs.webkit.org/show_bug.cgi?id=143554
+
+        Reviewed by Antti Koivisto.
+
+        Initialize render flow's segments only when the render flow changes in TextIterator.
+        The included performance test shows 6x speedup. (from ~10 runs/sec to ~60 runs/sec)
+
+        * Layout/simple-line-layout-innertext.html: Added.
+
</ins><span class="cx"> 2015-03-09  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [CG] Have Canvas use the IOSurfacePool
</span></span></pre></div>
<a id="trunkPerformanceTestsLayoutsimplelinelayoutinnertexthtml"></a>
<div class="addfile"><h4>Added: trunk/PerformanceTests/Layout/simple-line-layout-innertext.html (0 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/PerformanceTests/Layout/simple-line-layout-innertext.html                                (rev 0)
+++ trunk/PerformanceTests/Layout/simple-line-layout-innertext.html        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -0,0 +1,25 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+    &lt;title&gt;innerText performance test with simple line layout.&lt;/title&gt;
+    &lt;script src=&quot;../resources/runner.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+    &lt;pre id=&quot;log&quot;&gt;&lt;/pre&gt;
+    &lt;div id=&quot;target&quot; style=&quot;width: 300px;&quot;&gt;&lt;/div&gt;
+    &lt;div id=&quot;result&quot;&gt;&lt;/div&gt;
+    &lt;script&gt;
+        var target = document.getElementById(&quot;target&quot;);
+        var result = document.getElementById(&quot;result&quot;);
+        for (i = 0; i &lt; 500; ++i)
+            target.appendChild(document.createTextNode(&quot;foobar&quot;));
+
+        function test() {
+            for (i = 0; i &lt; 10; ++i)
+                result.innerText += target.innerText;
+            result.innerText = &quot;&quot;;
+        }
+        PerfTestRunner.measureRunsPerSecond({ run: test });
+    &lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/ChangeLog        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2015-04-09  Zalan Bujtas  &lt;zalan@apple.com&gt;
+
+        Simple line layout(regression): Calling innerText on RenderFlow with multiple children is slow.
+        https://bugs.webkit.org/show_bug.cgi?id=143554
+
+        Reviewed by Antti Koivisto.
+
+        Initialize render flow's segments only when the render flow changes in TextIterator.
+        The included performance test shows 6x speedup. (from ~10 runs/sec to ~60 runs/sec)
+
+        Test: PerformanceTests/Layout/simple-line-layout-innertext.html.
+
+        * editing/TextIterator.cpp:
+        (WebCore::TextIterator::handleTextNode):
+        * editing/TextIterator.h:
+        * rendering/SimpleLineLayoutFlowContents.cpp: Instruments log shows that vector's expandCapacity could be expensive when flow has large amount of children.
+        (WebCore::SimpleLineLayout::initializeSegments):
+
</ins><span class="cx"> 2015-04-09  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [WK2][iOS] editorState() should not cause a synchronous layout
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingTextIteratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextIterator.cpp (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextIterator.cpp        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/editing/TextIterator.cpp        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -548,20 +548,18 @@
</span><span class="cx">             return true;
</span><span class="cx">         // Use the simple layout runs to iterate over the text content.
</span><span class="cx">         ASSERT(renderer.parent() &amp;&amp; is&lt;RenderBlockFlow&gt;(renderer.parent()));
</span><del>-        const auto&amp; blockFlow = downcast&lt;RenderBlockFlow&gt;(*renderer.parent());
-        SimpleLineLayout::RunResolver runResolver(blockFlow, *layout);
-        auto range = runResolver.rangeForRenderer(renderer);
</del><span class="cx">         unsigned endPosition = (m_node == m_endContainer) ? static_cast&lt;unsigned&gt;(m_endOffset) : rendererText.length();
</span><del>-        // Simple line layout run positions are all absolute to the parent flow.
-        // Offsetting is required when multiple renderers are present.
-        if (previousTextNode &amp;&amp; previousTextNode != &amp;textNode) {
-            const RenderObject&amp; previousRenderer = *previousTextNode-&gt;renderer();
-            if (previousRenderer.parent() != &amp;blockFlow)
-                m_previousTextLengthInFlow = 0;
-            else
-                m_previousTextLengthInFlow += previousTextNode-&gt;renderer()-&gt;text()-&gt;length();
</del><ins>+        const auto&amp; blockFlow = downcast&lt;RenderBlockFlow&gt;(*renderer.parent());
+        if (!m_flowRunResolverCache || &amp;m_flowRunResolverCache-&gt;flow() != &amp;blockFlow) {
+            m_flowRunResolverCache = std::make_unique&lt;SimpleLineLayout::RunResolver&gt;(blockFlow, *layout);
+            m_previousTextLengthInFlow = 0;
+        } else if (previousTextNode &amp;&amp; previousTextNode != &amp;textNode) {
+            // Simple line layout run positions are all absolute to the parent flow.
+            // Offsetting is required when multiple renderers are present.
+            m_previousTextLengthInFlow += previousTextNode-&gt;renderer()-&gt;text()-&gt;length();
</ins><span class="cx">         }
</span><span class="cx">         // Skip to m_offset position.
</span><ins>+        auto range = m_flowRunResolverCache-&gt;rangeForRenderer(renderer);
</ins><span class="cx">         auto it = range.begin();
</span><span class="cx">         auto end = range.end();
</span><span class="cx">         while (it != end &amp;&amp; (*it).end() &lt;= (static_cast&lt;unsigned&gt;(m_offset) + m_previousTextLengthInFlow))
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingTextIteratorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextIterator.h (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextIterator.h        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/editing/TextIterator.h        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -39,6 +39,9 @@
</span><span class="cx"> class InlineTextBox;
</span><span class="cx"> class RenderText;
</span><span class="cx"> class RenderTextFragment;
</span><ins>+namespace SimpleLineLayout {
+class RunResolver;
+}
</ins><span class="cx"> 
</span><span class="cx"> WEBCORE_EXPORT String plainText(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
</span><span class="cx"> WEBCORE_EXPORT String plainTextReplacingNoBreakSpace(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
</span><span class="lines">@@ -166,6 +169,8 @@
</span><span class="cx">     // Used to do simple line layout run logic.
</span><span class="cx">     bool m_nextRunNeedsWhitespace { false };
</span><span class="cx">     unsigned m_previousTextLengthInFlow { 0 };
</span><ins>+    std::unique_ptr&lt;SimpleLineLayout::RunResolver&gt; m_flowRunResolverCache;
+
</ins><span class="cx">     // Used when text boxes are out of order (Hebrew/Arabic with embedded LTR text)
</span><span class="cx">     Vector&lt;InlineTextBox*&gt; m_sortedTextBoxes;
</span><span class="cx">     size_t m_sortedTextBoxesPosition;
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutFlowContentscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.cpp (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.cpp        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.cpp        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -35,7 +35,13 @@
</span><span class="cx"> 
</span><span class="cx"> static Vector&lt;FlowContents::Segment&gt; initializeSegments(const RenderBlockFlow&amp; flow)
</span><span class="cx"> {
</span><del>-    Vector&lt;FlowContents::Segment, 8&gt; segments;
</del><ins>+
+    unsigned numberOfChildren = 0;
+    auto children = childrenOfType&lt;RenderText&gt;(flow);
+    for (auto it = children.begin(), end = children.end(); it != end; ++it)
+        ++numberOfChildren;
+    Vector&lt;FlowContents::Segment&gt; segments;
+    segments.reserveCapacity(numberOfChildren);
</ins><span class="cx">     unsigned startPosition = 0;
</span><span class="cx">     for (auto&amp; textChild : childrenOfType&lt;RenderText&gt;(flow)) {
</span><span class="cx">         unsigned textLength = textChild.text()-&gt;length();
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutResolvercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.cpp (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.cpp        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.cpp        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -116,7 +116,8 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RunResolver::RunResolver(const RenderBlockFlow&amp; flow, const Layout&amp; layout)
</span><del>-    : m_layout(layout)
</del><ins>+    : m_flowRenderer(flow)
+    , m_layout(layout)
</ins><span class="cx">     , m_flowContents(flow)
</span><span class="cx">     , m_lineHeight(lineHeightFromFlow(flow))
</span><span class="cx">     , m_baseline(baselineFromFlow(flow))
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutResolverh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.h (182603 => 182604)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.h        2015-04-09 18:31:51 UTC (rev 182603)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutResolver.h        2015-04-09 18:57:33 UTC (rev 182604)
</span><span class="lines">@@ -99,6 +99,7 @@
</span><span class="cx"> 
</span><span class="cx">     RunResolver(const RenderBlockFlow&amp;, const Layout&amp;);
</span><span class="cx"> 
</span><ins>+    const RenderBlockFlow&amp; flow() const { return m_flowRenderer; }
</ins><span class="cx">     Iterator begin() const;
</span><span class="cx">     Iterator end() const;
</span><span class="cx"> 
</span><span class="lines">@@ -109,6 +110,7 @@
</span><span class="cx">     enum class IndexType { First, Last };
</span><span class="cx">     unsigned lineIndexForHeight(LayoutUnit, IndexType) const;
</span><span class="cx"> 
</span><ins>+    const RenderBlockFlow&amp; m_flowRenderer;
</ins><span class="cx">     const Layout&amp; m_layout;
</span><span class="cx">     const FlowContents m_flowContents;
</span><span class="cx">     const LayoutUnit m_lineHeight;
</span></span></pre>
</div>
</div>

</body>
</html>