<!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>[280193] 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/280193">280193</a></dd>
<dt>Author</dt> <dd>ysuzuki@apple.com</dd>
<dt>Date</dt> <dd>2021-07-22 12:18:45 -0700 (Thu, 22 Jul 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Micro-optimize innerHTML
https://bugs.webkit.org/show_bug.cgi?id=228142

Reviewed by Simon Fraser.

Source/WebCore:

No behavior change.

This patch does some micro optimizations revealed by the profiler when running some of Speedometer2 tests which intensively use innerHTML.
This offers improvement in jQuery-TodoMVC and Vanilla-ES2015-Babel-Webpack-TodoMVC since both are super innerHTML heavy benchmarks.

----------------------------------------------------------------------------------------------------------------------------------
|               subtest                |     ms      |     ms      |  b / a   | pValue (significance using False Discovery Rate) |
----------------------------------------------------------------------------------------------------------------------------------
| Elm-TodoMVC                          |126.862500   |126.687500   |0.998621  | 0.673462                                         |
| VueJS-TodoMVC                        |27.775000    |27.645833    |0.995350  | 0.741588                                         |
| EmberJS-TodoMVC                      |129.350000   |129.129167   |0.998293  | 0.624196                                         |
| BackboneJS-TodoMVC                   |51.129167    |51.204167    |1.001467  | 0.716622                                         |
| Preact-TodoMVC                       |21.870833    |21.337500    |0.975614  | 0.217771                                         |
| AngularJS-TodoMVC                    |139.854167   |140.266667   |1.002950  | 0.489838                                         |
| Vanilla-ES2015-TodoMVC               |69.229167    |68.895833    |0.995185  | 0.238772                                         |
| Inferno-TodoMVC                      |68.391667    |68.266667    |0.998172  | 0.762281                                         |
| Flight-TodoMVC                       |77.979167    |78.166667    |1.002404  | 0.710324                                         |
| Angular2-TypeScript-TodoMVC          |39.741667    |39.966667    |1.005662  | 0.524123                                         |
| VanillaJS-TodoMVC                    |55.416667    |55.512500    |1.001729  | 0.781447                                         |
| jQuery-TodoMVC                       |268.812500   |266.966667   |0.993133  | 0.003384 (significant)                           |
| EmberJS-Debug-TodoMVC                |345.383333   |345.662500   |1.000808  | 0.695259                                         |
| React-TodoMVC                        |90.679167    |90.179167    |0.994486  | 0.067477                                         |
| React-Redux-TodoMVC                  |152.691667   |152.687500   |0.999973  | 0.991207                                         |
| Vanilla-ES2015-Babel-Webpack-TodoMVC |66.487500    |65.729167    |0.988594  | 0.000118 (significant)                           |
----------------------------------------------------------------------------------------------------------------------------------

a mean = 242.12319
b mean = 242.80485
pValue = 0.1992654128
(Bigger means are better.)
1.003 times better
Results ARE NOT significant

* html/parser/HTMLConstructionSite.cpp:
(WebCore::HTMLConstructionSite::insertTextNode):
* html/parser/HTMLConstructionSite.h:
* html/parser/HTMLDocumentParser.cpp:
(WebCore::HTMLDocumentParser::pumpTokenizerLoop): We do not need to call `shrinkToBestFit` in fragment parsing case since
we will discard HTMLToken soon.
* html/parser/HTMLMetaCharsetParser.cpp:
(WebCore::HTMLMetaCharsetParser::checkForMetaCharset):
* html/parser/HTMLPreloadScanner.cpp:
(WebCore::HTMLPreloadScanner::scan):
* html/parser/HTMLToken.h:
(WebCore::HTMLToken::clear): We found that these `clear` calls cause performance problem according to the Instruments: we
repeatedly use this Vector, and we repeatedly allocate and deallocate this Vector unnecessarily. We use `resize(0)` instead
to avoid this allocation and deallocation.
(WebCore::HTMLToken::shrinkToBestFit): But HTMLToken is kept so long, so at some point, we would like to make backing storage
small. So, we add shrinkToBestFit and we call it only after finishing batching of HTMLToken processing.
(WebCore::HTMLToken::beginStartTag):
(WebCore::HTMLToken::beginEndTag):
* html/parser/HTMLTokenizer.h:
(WebCore::HTMLTokenizer::shrinkToBestFit):
* html/parser/HTMLTreeBuilder.cpp:
(WebCore::HTMLTreeBuilder::ExternalCharacterTokenBuffer::characterPredicate):
(WebCore::HTMLTreeBuilder::insertPhoneNumberLink):
(WebCore::HTMLTreeBuilder::linkifyPhoneNumbers):
(WebCore::HTMLTreeBuilder::processCharacterBuffer):
(WebCore::HTMLTreeBuilder::processCharacterBufferForInBody):
(WebCore::HTMLTreeBuilder::defaultForInTableText):
(WebCore::HTMLTreeBuilder::processTokenInForeignContent):
(WebCore::HTMLTreeBuilder::processFakeCharacters): Deleted. It is dead code before this patch.
* html/parser/HTMLTreeBuilder.h:

Source/WTF:

* wtf/Vector.h:
(WTF::Malloc>::shrinkToBestFit): This shrinks the backing storage to "appropriate for the living Vector".
* wtf/text/AtomStringImpl.cpp:
(WTF::UCharBufferTranslator::equal): WTF::equal for String does not check hash. This is because computing hash is expensive
and we would like to avoid that if it is not necessary. But when inserting string into AtomStringTable, we can use hash value
since they must be already computed because of HashMap's requirement. So let's use it before calling WTF::equal.
(WTF::LCharBufferTranslator::equal):
(WTF::BufferFromStaticDataTranslator::equal):
* wtf/text/StringView.h:
(WTF::StringView::stripLeadingMatchedCharacters): Add this and use it in HTMLTreeBuilder.

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp:
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::TEST):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfVectorh">trunk/Source/WTF/wtf/Vector.h</a></li>
<li><a href="#trunkSourceWTFwtftextAtomStringImplcpp">trunk/Source/WTF/wtf/text/AtomStringImpl.cpp</a></li>
<li><a href="#trunkSourceWTFwtftextStringViewh">trunk/Source/WTF/wtf/text/StringView.h</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLConstructionSitecpp">trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLConstructionSiteh">trunk/Source/WebCore/html/parser/HTMLConstructionSite.h</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLDocumentParsercpp">trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLMetaCharsetParsercpp">trunk/Source/WebCore/html/parser/HTMLMetaCharsetParser.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLPreloadScannercpp">trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLTokenh">trunk/Source/WebCore/html/parser/HTMLToken.h</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLTokenizerh">trunk/Source/WebCore/html/parser/HTMLTokenizer.h</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLTreeBuildercpp">trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlparserHTMLTreeBuilderh">trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWTFStringViewcpp">trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWTFVectorcpp">trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog       2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WTF/ChangeLog  2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2021-07-22  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        Micro-optimize innerHTML
+        https://bugs.webkit.org/show_bug.cgi?id=228142
+
+        Reviewed by Simon Fraser.
+
+        * wtf/Vector.h:
+        (WTF::Malloc>::shrinkToBestFit): This shrinks the backing storage to "appropriate for the living Vector".
+        * wtf/text/AtomStringImpl.cpp:
+        (WTF::UCharBufferTranslator::equal): WTF::equal for String does not check hash. This is because computing hash is expensive
+        and we would like to avoid that if it is not necessary. But when inserting string into AtomStringTable, we can use hash value
+        since they must be already computed because of HashMap's requirement. So let's use it before calling WTF::equal.
+        (WTF::LCharBufferTranslator::equal):
+        (WTF::BufferFromStaticDataTranslator::equal):
+        * wtf/text/StringView.h:
+        (WTF::StringView::stripLeadingMatchedCharacters): Add this and use it in HTMLTreeBuilder.
+
</ins><span class="cx"> 2021-07-22  Alex Christensen  <achristensen@webkit.org>
</span><span class="cx"> 
</span><span class="cx">         XHR.send(Document) should replace mismatched surrogates with replacement character before sending
</span></span></pre></div>
<a id="trunkSourceWTFwtfVectorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Vector.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Vector.h    2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WTF/wtf/Vector.h       2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -768,6 +768,10 @@
</span><span class="cx">     void shrinkCapacity(size_t newCapacity);
</span><span class="cx">     void shrinkToFit() { shrinkCapacity(size()); }
</span><span class="cx"> 
</span><ins>+    // "shrinkToBestFit()" shrinks the capacity to fix the current size, while leaving sufficient capacity for more items.
+    // The upperbound of the capacity is that which would result from calling expandCapacity() with the current size.
+    void shrinkToBestFit();
+
</ins><span class="cx">     void clear() { shrinkCapacity(0); }
</span><span class="cx"> 
</span><span class="cx">     template<typename U = T> Vector<U> isolatedCopy() const &;
</span><span class="lines">@@ -1258,6 +1262,15 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
</span><ins>+void Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::shrinkToBestFit()
+{
+    size_t currentSize = size();
+    size_t newCapacity = currentSize ? std::max<size_t>(minCapacity, currentSize + currentSize / 4 + 1) : 0;
+    ASSERT(currentSize <= newCapacity);
+    shrinkCapacity(newCapacity);
+}
+
+template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
</ins><span class="cx"> template<FailureAction action, typename U>
</span><span class="cx"> ALWAYS_INLINE bool Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::append(const U* data, size_t dataSize)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceWTFwtftextAtomStringImplcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/AtomStringImpl.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/AtomStringImpl.cpp     2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WTF/wtf/text/AtomStringImpl.cpp        2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -127,7 +127,10 @@
</span><span class="cx"> 
</span><span class="cx">     static bool equal(PackedPtr<StringImpl> const& str, const UCharBuffer& buf)
</span><span class="cx">     {
</span><del>-        return WTF::equal(str.get(), buf.characters, buf.length);
</del><ins>+        StringImpl* impl = str.get();
+        if (impl->existingHash() != buf.hash)
+            return false;
+        return WTF::equal(impl, buf.characters, buf.length);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     static void translate(PackedPtr<StringImpl>& location, const UCharBuffer& buf, unsigned hash)
</span><span class="lines">@@ -307,7 +310,10 @@
</span><span class="cx"> 
</span><span class="cx">     static bool equal(PackedPtr<StringImpl> const& str, const LCharBuffer& buf)
</span><span class="cx">     {
</span><del>-        return WTF::equal(str.get(), buf.characters, buf.length);
</del><ins>+        StringImpl* impl = str.get();
+        if (impl->existingHash() != buf.hash)
+            return false;
+        return WTF::equal(impl, buf.characters, buf.length);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     static void translate(PackedPtr<StringImpl>& location, const LCharBuffer& buf, unsigned hash)
</span><span class="lines">@@ -329,7 +335,10 @@
</span><span class="cx"> 
</span><span class="cx">     static bool equal(PackedPtr<StringImpl> const& str, const Buffer& buf)
</span><span class="cx">     {
</span><del>-        return WTF::equal(str.get(), buf.characters, buf.length);
</del><ins>+        StringImpl* impl = str.get();
+        if (impl->existingHash() != buf.hash)
+            return false;
+        return WTF::equal(impl, buf.characters, buf.length);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     static void translate(PackedPtr<StringImpl>& location, const Buffer& buf, unsigned hash)
</span></span></pre></div>
<a id="trunkSourceWTFwtftextStringViewh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/text/StringView.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/text/StringView.h   2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WTF/wtf/text/StringView.h      2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -131,6 +131,8 @@
</span><span class="cx">     StringView right(unsigned length) const { return substring(this->length() - length, length); }
</span><span class="cx"> 
</span><span class="cx">     template<typename MatchedCharacterPredicate>
</span><ins>+    StringView stripLeadingMatchedCharacters(const MatchedCharacterPredicate&);
+    template<typename MatchedCharacterPredicate>
</ins><span class="cx">     StringView stripLeadingAndTrailingMatchedCharacters(const MatchedCharacterPredicate&);
</span><span class="cx"> 
</span><span class="cx">     class SplitResult;
</span><span class="lines">@@ -182,6 +184,9 @@
</span><span class="cx">     void initialize(const UChar*, unsigned length);
</span><span class="cx"> 
</span><span class="cx">     template<typename CharacterType, typename MatchedCharacterPredicate>
</span><ins>+    StringView stripLeadingMatchedCharacters(const CharacterType*, const MatchedCharacterPredicate&);
+
+    template<typename CharacterType, typename MatchedCharacterPredicate>
</ins><span class="cx">     StringView stripLeadingAndTrailingMatchedCharacters(const CharacterType*, const MatchedCharacterPredicate&);
</span><span class="cx"> 
</span><span class="cx"> #if CHECK_STRINGVIEW_LIFETIME
</span><span class="lines">@@ -1030,6 +1035,32 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template<typename CharacterType, typename MatchedCharacterPredicate>
</span><ins>+inline StringView StringView::stripLeadingMatchedCharacters(const CharacterType* characters, const MatchedCharacterPredicate& predicate)
+{
+    unsigned start = 0;
+    while (start < m_length && predicate(characters[start]))
+        ++start;
+
+    if (start == m_length)
+        return StringView::empty();
+
+    if (!start)
+        return *this;
+
+    StringView result(characters + start, m_length - start);
+    result.setUnderlyingString(*this);
+    return result;
+}
+
+template<typename MatchedCharacterPredicate>
+StringView StringView::stripLeadingMatchedCharacters(const MatchedCharacterPredicate& predicate)
+{
+    if (is8Bit())
+        return stripLeadingMatchedCharacters<LChar>(characters8(), predicate);
+    return stripLeadingMatchedCharacters<UChar>(characters16(), predicate);
+}
+
+template<typename CharacterType, typename MatchedCharacterPredicate>
</ins><span class="cx"> inline StringView StringView::stripLeadingAndTrailingMatchedCharacters(const CharacterType* characters, const MatchedCharacterPredicate& predicate)
</span><span class="cx"> {
</span><span class="cx">     if (!m_length)
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/ChangeLog      2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -1,3 +1,75 @@
</span><ins>+2021-07-22  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        Micro-optimize innerHTML
+        https://bugs.webkit.org/show_bug.cgi?id=228142
+
+        Reviewed by Simon Fraser.
+
+        No behavior change.
+
+        This patch does some micro optimizations revealed by the profiler when running some of Speedometer2 tests which intensively use innerHTML.
+        This offers improvement in jQuery-TodoMVC and Vanilla-ES2015-Babel-Webpack-TodoMVC since both are super innerHTML heavy benchmarks.
+
+        ----------------------------------------------------------------------------------------------------------------------------------
+        |               subtest                |     ms      |     ms      |  b / a   | pValue (significance using False Discovery Rate) |
+        ----------------------------------------------------------------------------------------------------------------------------------
+        | Elm-TodoMVC                          |126.862500   |126.687500   |0.998621  | 0.673462                                         |
+        | VueJS-TodoMVC                        |27.775000    |27.645833    |0.995350  | 0.741588                                         |
+        | EmberJS-TodoMVC                      |129.350000   |129.129167   |0.998293  | 0.624196                                         |
+        | BackboneJS-TodoMVC                   |51.129167    |51.204167    |1.001467  | 0.716622                                         |
+        | Preact-TodoMVC                       |21.870833    |21.337500    |0.975614  | 0.217771                                         |
+        | AngularJS-TodoMVC                    |139.854167   |140.266667   |1.002950  | 0.489838                                         |
+        | Vanilla-ES2015-TodoMVC               |69.229167    |68.895833    |0.995185  | 0.238772                                         |
+        | Inferno-TodoMVC                      |68.391667    |68.266667    |0.998172  | 0.762281                                         |
+        | Flight-TodoMVC                       |77.979167    |78.166667    |1.002404  | 0.710324                                         |
+        | Angular2-TypeScript-TodoMVC          |39.741667    |39.966667    |1.005662  | 0.524123                                         |
+        | VanillaJS-TodoMVC                    |55.416667    |55.512500    |1.001729  | 0.781447                                         |
+        | jQuery-TodoMVC                       |268.812500   |266.966667   |0.993133  | 0.003384 (significant)                           |
+        | EmberJS-Debug-TodoMVC                |345.383333   |345.662500   |1.000808  | 0.695259                                         |
+        | React-TodoMVC                        |90.679167    |90.179167    |0.994486  | 0.067477                                         |
+        | React-Redux-TodoMVC                  |152.691667   |152.687500   |0.999973  | 0.991207                                         |
+        | Vanilla-ES2015-Babel-Webpack-TodoMVC |66.487500    |65.729167    |0.988594  | 0.000118 (significant)                           |
+        ----------------------------------------------------------------------------------------------------------------------------------
+
+
+        a mean = 242.12319
+        b mean = 242.80485
+        pValue = 0.1992654128
+        (Bigger means are better.)
+        1.003 times better
+        Results ARE NOT significant
+
+        * html/parser/HTMLConstructionSite.cpp:
+        (WebCore::HTMLConstructionSite::insertTextNode):
+        * html/parser/HTMLConstructionSite.h:
+        * html/parser/HTMLDocumentParser.cpp:
+        (WebCore::HTMLDocumentParser::pumpTokenizerLoop): We do not need to call `shrinkToBestFit` in fragment parsing case since
+        we will discard HTMLToken soon.
+        * html/parser/HTMLMetaCharsetParser.cpp:
+        (WebCore::HTMLMetaCharsetParser::checkForMetaCharset):
+        * html/parser/HTMLPreloadScanner.cpp:
+        (WebCore::HTMLPreloadScanner::scan):
+        * html/parser/HTMLToken.h:
+        (WebCore::HTMLToken::clear): We found that these `clear` calls cause performance problem according to the Instruments: we
+        repeatedly use this Vector, and we repeatedly allocate and deallocate this Vector unnecessarily. We use `resize(0)` instead
+        to avoid this allocation and deallocation.
+        (WebCore::HTMLToken::shrinkToBestFit): But HTMLToken is kept so long, so at some point, we would like to make backing storage
+        small. So, we add shrinkToBestFit and we call it only after finishing batching of HTMLToken processing.
+        (WebCore::HTMLToken::beginStartTag):
+        (WebCore::HTMLToken::beginEndTag):
+        * html/parser/HTMLTokenizer.h:
+        (WebCore::HTMLTokenizer::shrinkToBestFit):
+        * html/parser/HTMLTreeBuilder.cpp:
+        (WebCore::HTMLTreeBuilder::ExternalCharacterTokenBuffer::characterPredicate):
+        (WebCore::HTMLTreeBuilder::insertPhoneNumberLink):
+        (WebCore::HTMLTreeBuilder::linkifyPhoneNumbers):
+        (WebCore::HTMLTreeBuilder::processCharacterBuffer):
+        (WebCore::HTMLTreeBuilder::processCharacterBufferForInBody):
+        (WebCore::HTMLTreeBuilder::defaultForInTableText):
+        (WebCore::HTMLTreeBuilder::processTokenInForeignContent):
+        (WebCore::HTMLTreeBuilder::processFakeCharacters): Deleted. It is dead code before this patch.
+        * html/parser/HTMLTreeBuilder.h:
+
</ins><span class="cx"> 2021-07-22  Wenson Hsieh  <wenson_hsieh@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Rename EventHandler::m_textRecognitionHoverTimerFired()
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLConstructionSitecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp        2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp   2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -566,7 +566,7 @@
</span><span class="cx">         m_openElements.push(HTMLStackItem::create(WTFMove(element), WTFMove(token), namespaceURI));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void HTMLConstructionSite::insertTextNode(const String& characters, WhitespaceMode whitespaceMode)
</del><ins>+void HTMLConstructionSite::insertTextNode(String&& characters, WhitespaceMode whitespaceMode)
</ins><span class="cx"> {
</span><span class="cx">     HTMLConstructionSiteTask task(HTMLConstructionSiteTask::Insert);
</span><span class="cx">     task.parent = &currentNode();
</span><span class="lines">@@ -596,7 +596,7 @@
</span><span class="cx">         // If we have a whole string of unbreakable characters the above could lead to an infinite loop. Exceeding the length limit is the lesser evil.
</span><span class="cx">         if (!textNode->length()) {
</span><span class="cx">             String substring = characters.substring(currentPosition);
</span><del>-            textNode = Text::create(task.parent->document(), shouldUseAtomString ? AtomString(substring).string() : substring);
</del><ins>+            textNode = Text::create(task.parent->document(), shouldUseAtomString ? AtomString(WTFMove(substring)).string() : substring);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         currentPosition += textNode->length();
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLConstructionSiteh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLConstructionSite.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLConstructionSite.h  2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLConstructionSite.h     2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -111,7 +111,7 @@
</span><span class="cx">     void insertHTMLBodyElement(AtomicHTMLToken&&);
</span><span class="cx">     void insertHTMLFormElement(AtomicHTMLToken&&, bool isDemoted = false);
</span><span class="cx">     void insertScriptElement(AtomicHTMLToken&&);
</span><del>-    void insertTextNode(const String&, WhitespaceMode = WhitespaceUnknown);
</del><ins>+    void insertTextNode(String&&, WhitespaceMode = WhitespaceUnknown);
</ins><span class="cx">     void insertForeignElement(AtomicHTMLToken&&, const AtomString& namespaceURI);
</span><span class="cx"> 
</span><span class="cx">     void insertHTMLHtmlStartTagBeforeHTML(AtomicHTMLToken&&);
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLDocumentParsercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp  2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp     2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -43,7 +43,7 @@
</span><span class="cx"> #include "NavigationScheduler.h"
</span><span class="cx"> #include "ScriptElement.h"
</span><span class="cx"> #include "ThrowOnDynamicMarkupInsertionCountIncrementer.h"
</span><del>-
</del><ins>+#include <wtf/Scope.h>
</ins><span class="cx"> #include <wtf/SystemTracing.h>
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -256,6 +256,11 @@
</span><span class="cx"> 
</span><span class="cx"> bool HTMLDocumentParser::pumpTokenizerLoop(SynchronousMode mode, bool parsingFragment, PumpSession& session)
</span><span class="cx"> {
</span><ins>+    auto scopeExit = makeScopeExit([&] {
+        if (!parsingFragment)
+            m_tokenizer.shrinkToBestFit();
+    });
+
</ins><span class="cx">     do {
</span><span class="cx">         if (UNLIKELY(isWaitingForScripts())) {
</span><span class="cx">             if (mode == AllowYield && m_parserScheduler->shouldYieldBeforeExecutingScript(m_treeBuilder->scriptToProcess(), session))
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLMetaCharsetParsercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLMetaCharsetParser.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLMetaCharsetParser.cpp       2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLMetaCharsetParser.cpp  2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> #include "HTMLParserIdioms.h"
</span><span class="cx"> #include "TextCodec.h"
</span><span class="cx"> #include "TextEncodingRegistry.h"
</span><ins>+#include <wtf/Scope.h>
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -157,6 +158,10 @@
</span><span class="cx">     bool ignoredSawErrorFlag;
</span><span class="cx">     m_input.append(m_codec->decode(data, length, false, false, ignoredSawErrorFlag));
</span><span class="cx"> 
</span><ins>+    auto scopeExit = makeScopeExit([&] {
+        m_tokenizer.shrinkToBestFit();
+    });
+
</ins><span class="cx">     while (auto token = m_tokenizer.nextToken(m_input)) {
</span><span class="cx">         bool isEnd = token->type() == HTMLToken::EndTag;
</span><span class="cx">         if (isEnd || token->type() == HTMLToken::StartTag) {
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLPreloadScannercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp  2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp     2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -514,6 +514,7 @@
</span><span class="cx">             m_tokenizer.updateStateFor(AtomString(token->name()));
</span><span class="cx">         m_scanner.scan(*token, requests, document);
</span><span class="cx">     }
</span><ins>+    m_tokenizer.shrinkToBestFit();
</ins><span class="cx"> 
</span><span class="cx">     preloader.preload(WTFMove(requests));
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLTokenh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLToken.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLToken.h     2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLToken.h        2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -68,6 +68,7 @@
</span><span class="cx">     HTMLToken();
</span><span class="cx"> 
</span><span class="cx">     void clear();
</span><ins>+    void shrinkToBestFit();
</ins><span class="cx"> 
</span><span class="cx">     Type type() const;
</span><span class="cx"> 
</span><span class="lines">@@ -170,10 +171,16 @@
</span><span class="cx"> inline void HTMLToken::clear()
</span><span class="cx"> {
</span><span class="cx">     m_type = Uninitialized;
</span><del>-    m_data.clear();
</del><ins>+    m_data.resize(0);
</ins><span class="cx">     m_data8BitCheck = 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline void HTMLToken::shrinkToBestFit()
+{
+    m_data.shrinkToBestFit();
+    m_attributes.shrinkToBestFit();
+}
+
</ins><span class="cx"> inline HTMLToken::Type HTMLToken::type() const
</span><span class="cx"> {
</span><span class="cx">     return m_type;
</span><span class="lines">@@ -273,7 +280,7 @@
</span><span class="cx">     ASSERT(m_type == Uninitialized);
</span><span class="cx">     m_type = StartTag;
</span><span class="cx">     m_selfClosing = false;
</span><del>-    m_attributes.clear();
</del><ins>+    m_attributes.resize(0);
</ins><span class="cx"> 
</span><span class="cx"> #if ASSERT_ENABLED
</span><span class="cx">     m_currentAttribute = nullptr;
</span><span class="lines">@@ -288,7 +295,7 @@
</span><span class="cx">     ASSERT(m_type == Uninitialized);
</span><span class="cx">     m_type = EndTag;
</span><span class="cx">     m_selfClosing = false;
</span><del>-    m_attributes.clear();
</del><ins>+    m_attributes.resize(0);
</ins><span class="cx"> 
</span><span class="cx"> #if ASSERT_ENABLED
</span><span class="cx">     m_currentAttribute = nullptr;
</span><span class="lines">@@ -302,7 +309,7 @@
</span><span class="cx">     ASSERT(m_type == Uninitialized);
</span><span class="cx">     m_type = EndTag;
</span><span class="cx">     m_selfClosing = false;
</span><del>-    m_attributes.clear();
</del><ins>+    m_attributes.resize(0);
</ins><span class="cx"> 
</span><span class="cx"> #if ASSERT_ENABLED
</span><span class="cx">     m_currentAttribute = nullptr;
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLTokenizerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLTokenizer.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLTokenizer.h 2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLTokenizer.h    2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -79,6 +79,11 @@
</span><span class="cx"> 
</span><span class="cx">     bool neverSkipNullCharacters() const;
</span><span class="cx"> 
</span><ins>+    void shrinkToBestFit()
+    {
+        m_token.shrinkToBestFit();
+    }
+
</ins><span class="cx"> private:
</span><span class="cx">     enum State {
</span><span class="cx">         DataState,
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLTreeBuildercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp     2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp        2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -226,11 +226,7 @@
</span><span class="cx">     template<bool characterPredicate(UChar)> void skipLeading()
</span><span class="cx">     {
</span><span class="cx">         ASSERT(!isEmpty());
</span><del>-        while (characterPredicate(m_text[0])) {
-            m_text = m_text.substring(1);
-            if (m_text.isEmpty())
-                return;
-        }
</del><ins>+        m_text = m_text.stripLeadingMatchedCharacters(characterPredicate);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     template<bool characterPredicate(UChar)> String takeLeading()
</span><span class="lines">@@ -433,13 +429,6 @@
</span><span class="cx">     processFakeEndTag(tagName.localName());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void HTMLTreeBuilder::processFakeCharacters(const String& characters)
-{
-    ASSERT(!characters.isEmpty());
-    ExternalCharacterTokenBuffer buffer(characters);
-    processCharacterBuffer(buffer);
-}
-
</del><span class="cx"> void HTMLTreeBuilder::processFakePEndTagIfPInButtonScope()
</span><span class="cx"> {
</span><span class="cx">     if (!m_tree.openElements().inButtonScope(pTag->localName()))
</span><span class="lines">@@ -2195,7 +2184,7 @@
</span><span class="cx"> 
</span><span class="cx"> // FIXME: Extract the following iOS-specific code into a separate file.
</span><span class="cx"> // From the string 4089961010, creates a link of the form <a href="tel:4089961010">4089961010</a> and inserts it.
</span><del>-void HTMLTreeBuilder::insertPhoneNumberLink(const String& string)
</del><ins>+void HTMLTreeBuilder::insertPhoneNumberLink(String&& string)
</ins><span class="cx"> {
</span><span class="cx">     Vector<Attribute> attributes;
</span><span class="cx">     attributes.append(Attribute(HTMLNames::hrefAttr, makeString("tel:"_s, string)));
</span><span class="lines">@@ -2206,7 +2195,7 @@
</span><span class="cx"> 
</span><span class="cx">     processStartTag(WTFMove(aStartToken));
</span><span class="cx">     m_tree.executeQueuedTasks();
</span><del>-    m_tree.insertTextNode(string);
</del><ins>+    m_tree.insertTextNode(WTFMove(string));
</ins><span class="cx">     processEndTag(WTFMove(aEndToken));
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -2215,7 +2204,7 @@
</span><span class="cx"> // 2. Wraps the phone number in a tel: link.
</span><span class="cx"> // 3. Goes back to step 1 if a phone number is found in the rest of the string.
</span><span class="cx"> // 4. Appends the rest of the string as a text node.
</span><del>-void HTMLTreeBuilder::linkifyPhoneNumbers(const String& string)
</del><ins>+void HTMLTreeBuilder::linkifyPhoneNumbers(String&& string)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(TelephoneNumberDetector::isSupported());
</span><span class="cx"> 
</span><span class="lines">@@ -2248,10 +2237,10 @@
</span><span class="cx">     if (scannerPosition > 0) {
</span><span class="cx">         if (scannerPosition < length) {
</span><span class="cx">             String after = string.substring(scannerPosition, length - scannerPosition);
</span><del>-            m_tree.insertTextNode(after);
</del><ins>+            m_tree.insertTextNode(WTFMove(after));
</ins><span class="cx">         }
</span><span class="cx">     } else
</span><del>-        m_tree.insertTextNode(string);
</del><ins>+        m_tree.insertTextNode(WTFMove(string));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Looks at the ancestors of the element to determine whether we're inside an element which disallows parsing phone numbers.
</span><span class="lines">@@ -2324,7 +2313,7 @@
</span><span class="cx">     case InsertionMode::InHead: {
</span><span class="cx">         String leadingWhitespace = buffer.takeLeadingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty())
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         if (buffer.isEmpty())
</span><span class="cx">             return;
</span><span class="cx">         defaultForInHead();
</span><span class="lines">@@ -2334,7 +2323,7 @@
</span><span class="cx">     case InsertionMode::AfterHead: {
</span><span class="cx">         String leadingWhitespace = buffer.takeLeadingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty())
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         if (buffer.isEmpty())
</span><span class="cx">             return;
</span><span class="cx">         defaultForAfterHead();
</span><span class="lines">@@ -2372,7 +2361,7 @@
</span><span class="cx">     case InsertionMode::InColumnGroup: {
</span><span class="cx">         String leadingWhitespace = buffer.takeLeadingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty())
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         if (buffer.isEmpty())
</span><span class="cx">             return;
</span><span class="cx">         if (!processColgroupEndTagForInColumnGroup()) {
</span><span class="lines">@@ -2395,7 +2384,7 @@
</span><span class="cx">     case InsertionMode::InHeadNoscript: {
</span><span class="cx">         String leadingWhitespace = buffer.takeLeadingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty())
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         if (buffer.isEmpty())
</span><span class="cx">             return;
</span><span class="cx">         defaultForInHeadNoscript();
</span><span class="lines">@@ -2405,7 +2394,7 @@
</span><span class="cx">     case InsertionMode::AfterFrameset: {
</span><span class="cx">         String leadingWhitespace = buffer.takeRemainingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty())
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         // FIXME: We should generate a parse error if we skipped over any
</span><span class="cx">         // non-whitespace characters.
</span><span class="cx">         break;
</span><span class="lines">@@ -2418,7 +2407,7 @@
</span><span class="cx">         String leadingWhitespace = buffer.takeRemainingWhitespace();
</span><span class="cx">         if (!leadingWhitespace.isEmpty()) {
</span><span class="cx">             m_tree.reconstructTheActiveFormattingElements();
</span><del>-            m_tree.insertTextNode(leadingWhitespace, AllWhitespace);
</del><ins>+            m_tree.insertTextNode(WTFMove(leadingWhitespace), AllWhitespace);
</ins><span class="cx">         }
</span><span class="cx">         // FIXME: We should generate a parse error if we skipped over any
</span><span class="cx">         // non-whitespace characters.
</span><span class="lines">@@ -2431,16 +2420,16 @@
</span><span class="cx"> {
</span><span class="cx">     m_tree.reconstructTheActiveFormattingElements();
</span><span class="cx">     String characters = buffer.takeRemaining();
</span><ins>+    if (m_framesetOk && !isAllWhitespaceOrReplacementCharacters(characters))
+        m_framesetOk = false;
</ins><span class="cx"> #if ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(IOS_FAMILY)
</span><span class="cx">     if (!isParsingFragment() && m_tree.isTelephoneNumberParsingEnabled() && shouldParseTelephoneNumbersInNode(m_tree.currentNode()) && TelephoneNumberDetector::isSupported())
</span><del>-        linkifyPhoneNumbers(characters);
</del><ins>+        linkifyPhoneNumbers(WTFMove(characters));
</ins><span class="cx">     else
</span><del>-        m_tree.insertTextNode(characters);
</del><ins>+        m_tree.insertTextNode(WTFMove(characters));
</ins><span class="cx"> #else
</span><del>-    m_tree.insertTextNode(characters);
</del><ins>+    m_tree.insertTextNode(WTFMove(characters));
</ins><span class="cx"> #endif
</span><del>-    if (m_framesetOk && !isAllWhitespaceOrReplacementCharacters(characters))
-        m_framesetOk = false;
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void HTMLTreeBuilder::processEndOfFile(AtomicHTMLToken&& token)
</span><span class="lines">@@ -2577,12 +2566,12 @@
</span><span class="cx">         // FIXME: parse error
</span><span class="cx">         HTMLConstructionSite::RedirectToFosterParentGuard redirecter(m_tree);
</span><span class="cx">         m_tree.reconstructTheActiveFormattingElements();
</span><del>-        m_tree.insertTextNode(characters, NotAllWhitespace);
</del><ins>+        m_tree.insertTextNode(WTFMove(characters), NotAllWhitespace);
</ins><span class="cx">         m_framesetOk = false;
</span><span class="cx">         m_insertionMode = m_originalInsertionMode;
</span><span class="cx">         return;
</span><span class="cx">     }
</span><del>-    m_tree.insertTextNode(characters);
</del><ins>+    m_tree.insertTextNode(WTFMove(characters));
</ins><span class="cx">     m_insertionMode = m_originalInsertionMode;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -2820,9 +2809,9 @@
</span><span class="cx">         return;
</span><span class="cx">     case HTMLToken::Character: {
</span><span class="cx">         String characters = String(token.characters(), token.charactersLength());
</span><del>-        m_tree.insertTextNode(characters);
</del><span class="cx">         if (m_framesetOk && !isAllWhitespaceOrReplacementCharacters(characters))
</span><span class="cx">             m_framesetOk = false;
</span><ins>+        m_tree.insertTextNode(WTFMove(characters));
</ins><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx">     case HTMLToken::EndOfFile:
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlparserHTMLTreeBuilderh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h       2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h  2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -108,8 +108,8 @@
</span><span class="cx">     bool isParsingFragmentOrTemplateContents() const;
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(IOS_FAMILY)
</span><del>-    void insertPhoneNumberLink(const String&);
-    void linkifyPhoneNumbers(const String&);
</del><ins>+    void insertPhoneNumberLink(String&&);
+    void linkifyPhoneNumbers(String&&);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     void processToken(AtomicHTMLToken&&);
</span><span class="lines">@@ -145,7 +145,6 @@
</span><span class="cx">     void processFakeStartTag(const QualifiedName&, Vector<Attribute>&& attributes = Vector<Attribute>());
</span><span class="cx">     void processFakeEndTag(const QualifiedName&);
</span><span class="cx">     void processFakeEndTag(const AtomString&);
</span><del>-    void processFakeCharacters(const String&);
</del><span class="cx">     void processFakePEndTagIfPInButtonScope();
</span><span class="cx"> 
</span><span class="cx">     void processGenericRCDATAStartTag(AtomicHTMLToken&&);
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Tools/ChangeLog       2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2021-07-22  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        Micro-optimize innerHTML
+        https://bugs.webkit.org/show_bug.cgi?id=228142
+
+        Reviewed by Simon Fraser.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp:
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::TEST):
+
</ins><span class="cx"> 2021-07-22  Peng Liu  <peng.liu6@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [ BigSur Debug ] TestWebKitAPI.GPUProcess.CrashWhilePlayingAudioViaCreateMediaElementSource is flaky, hitting ASSERTION FAILED: !isInRoutingArbitrationForToken(token)
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWTFStringViewcpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp       2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp  2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -959,6 +959,28 @@
</span><span class="cx">     EXPECT_EQ(reference.reverseFind('c', 4), notFound);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(WTF, StringViewStripLeadingMatchedCharacters)
+{
+    auto isA = [] (UChar c) {
+        return c == 'A';
+    };
+
+    EXPECT_TRUE(stringViewFromLiteral("AAABBBAAA").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("BBBAAA"));
+    EXPECT_TRUE(stringViewFromLiteral("AAABBBCCC").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("BBBCCC"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCBBBAAA").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("CCCBBBAAA"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCBBBCCC").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("CCCBBBCCC"));
+    EXPECT_TRUE(stringViewFromLiteral("AAAAAACCC").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("CCC"));
+    EXPECT_TRUE(stringViewFromLiteral("BBBAAAAAA").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("BBBAAAAAA"));
+    EXPECT_TRUE(stringViewFromLiteral("CCCAAABBB").stripLeadingMatchedCharacters(isA) == stringViewFromLiteral("CCCAAABBB"));
+    EXPECT_TRUE(stringViewFromLiteral("AAAAAAAAA").stripLeadingMatchedCharacters(isA) == StringView::empty());
+
+    StringView emptyView = StringView::empty();
+    EXPECT_TRUE(emptyView.stripLeadingMatchedCharacters(isA) == emptyView);
+
+    StringView nullView;
+    EXPECT_TRUE(nullView.stripLeadingMatchedCharacters(isA) == nullView);
+}
+
</ins><span class="cx"> TEST(WTF, StringViewStripLeadingAndTrailingMatchedCharacters)
</span><span class="cx"> {
</span><span class="cx">     auto isA = [] (UChar c) { 
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWTFVectorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (280192 => 280193)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp   2021-07-22 19:13:01 UTC (rev 280192)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp      2021-07-22 19:18:45 UTC (rev 280193)
</span><span class="lines">@@ -1458,5 +1458,41 @@
</span><span class="cx">     EXPECT_EQ(vector[3], 4);
</span><span class="cx">     EXPECT_EQ(vector[4], 5);
</span><span class="cx"> }
</span><del>-    
</del><ins>+
+TEST(WTF_Vector, ShrinkToBestFitClear)
+{
+    Vector<uint32_t> vector;
+    vector.append(20);
+    vector.append(20);
+    vector.append(20);
+    vector.resize(0);
+    vector.shrinkToBestFit();
+    EXPECT_EQ(vector.size(), 0U);
+    EXPECT_EQ(vector.capacity(), 0U);
+}
+
+TEST(WTF_Vector, ShrinkToBestFitDoesNotChangeIfCapacityMatches)
+{
+    Vector<uint32_t> vector;
+    vector.append(20);
+    vector.append(20);
+    vector.append(20);
+    unsigned capacity = vector.capacity();
+    vector.shrinkToBestFit();
+    EXPECT_EQ(vector.size(), 3U);
+    EXPECT_EQ(vector.capacity(), capacity);
+}
+
+TEST(WTF_Vector, ShrinkToBestFit)
+{
+    Vector<uint32_t> vector;
+    for (unsigned index = 0; index < 400; ++index)
+        vector.append(index);
+    vector.resize(100);
+    EXPECT_GE(vector.capacity(), 400U);
+    vector.shrinkToBestFit();
+    EXPECT_LE(vector.capacity(), 200U);
+    EXPECT_GE(vector.capacity(), 100U);
+}
+
</ins><span class="cx"> } // namespace TestWebKitAPI
</span></span></pre>
</div>
</div>

</body>
</html>