<!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>[163847] trunk/Source/WebCore</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/163847">163847</a></dd>
<dt>Author</dt> <dd>benjamin@webkit.org</dd>
<dt>Date</dt> <dd>2014-02-10 18:11:44 -0800 (Mon, 10 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Speed up DatasetDOMStringMap::item() when the element has multiple attributes
https://bugs.webkit.org/show_bug.cgi?id=128058

Reviewed by Darin Adler.

Accessing data attributes by name through DatasetDOMStringMap involes the conversion
from JavaScript property name to attribute name (done with propertyNameMatchesAttributeName()).

When there is a single data attribute, that method is efficient. When there are several attributes,
comparing names character by character becomes a bottleneck.

This patch add an efficent path for this case: instead of converting the attribute name on the fly,
the JavaScript property name is converted to an attribute name so that it can compared by its
AtomicStringImpl pointer.

This method puts a lot more pressure on convertPropertyNameToAttributeName()'s speed. The method was
improved accordingly to compensate for its new caller.

When enumerating multiple attributes by name, this patch provides about 80% speedup.
I could not measure any slow down on the simple cases.

* dom/DatasetDOMStringMap.cpp:
(WebCore::convertPropertyNameToAttributeName):
(WebCore::DatasetDOMStringMap::item):
* dom/ElementData.h:
(WebCore::AttributeIteratorAccessor::attributeCount):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomDatasetDOMStringMapcpp">trunk/Source/WebCore/dom/DatasetDOMStringMap.cpp</a></li>
<li><a href="#trunkSourceWebCoredomElementDatah">trunk/Source/WebCore/dom/ElementData.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (163846 => 163847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-02-11 01:56:31 UTC (rev 163846)
+++ trunk/Source/WebCore/ChangeLog        2014-02-11 02:11:44 UTC (rev 163847)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2014-02-10  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
+        Speed up DatasetDOMStringMap::item() when the element has multiple attributes
+        https://bugs.webkit.org/show_bug.cgi?id=128058
+
+        Reviewed by Darin Adler.
+
+        Accessing data attributes by name through DatasetDOMStringMap involes the conversion
+        from JavaScript property name to attribute name (done with propertyNameMatchesAttributeName()).
+
+        When there is a single data attribute, that method is efficient. When there are several attributes,
+        comparing names character by character becomes a bottleneck.
+
+        This patch add an efficent path for this case: instead of converting the attribute name on the fly,
+        the JavaScript property name is converted to an attribute name so that it can compared by its
+        AtomicStringImpl pointer.
+
+        This method puts a lot more pressure on convertPropertyNameToAttributeName()'s speed. The method was
+        improved accordingly to compensate for its new caller.
+
+        When enumerating multiple attributes by name, this patch provides about 80% speedup.
+        I could not measure any slow down on the simple cases.
+
+        * dom/DatasetDOMStringMap.cpp:
+        (WebCore::convertPropertyNameToAttributeName):
+        (WebCore::DatasetDOMStringMap::item):
+        * dom/ElementData.h:
+        (WebCore::AttributeIteratorAccessor::attributeCount):
+
</ins><span class="cx"> 2014-02-10  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Address the review comments for r163825.
</span></span></pre></div>
<a id="trunkSourceWebCoredomDatasetDOMStringMapcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/DatasetDOMStringMap.cpp (163846 => 163847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/DatasetDOMStringMap.cpp        2014-02-11 01:56:31 UTC (rev 163846)
+++ trunk/Source/WebCore/dom/DatasetDOMStringMap.cpp        2014-02-11 02:11:44 UTC (rev 163847)
</span><span class="lines">@@ -107,22 +107,39 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static String convertPropertyNameToAttributeName(const String&amp; name)
</del><ins>+template&lt;typename CharacterType&gt;
+static inline AtomicString convertPropertyNameToAttributeName(const StringImpl&amp; name)
</ins><span class="cx"> {
</span><del>-    StringBuilder builder;
-    builder.append(&quot;data-&quot;);
</del><ins>+    const CharacterType dataPrefix[] = { 'd', 'a', 't', 'a', '-' };
</ins><span class="cx"> 
</span><ins>+    Vector&lt;CharacterType, 32&gt; buffer;
+
</ins><span class="cx">     unsigned length = name.length();
</span><ins>+    buffer.reserveInitialCapacity(WTF_ARRAY_LENGTH(dataPrefix) + length);
+
+    buffer.append(dataPrefix, WTF_ARRAY_LENGTH(dataPrefix));
+
+    const CharacterType* characters = name.characters&lt;CharacterType&gt;();
</ins><span class="cx">     for (unsigned i = 0; i &lt; length; ++i) {
</span><del>-        UChar character = name[i];
</del><ins>+        CharacterType character = characters[i];
</ins><span class="cx">         if (isASCIIUpper(character)) {
</span><del>-            builder.append('-');
-            builder.append(toASCIILower(character));
</del><ins>+            buffer.append('-');
+            buffer.append(toASCIILower(character));
</ins><span class="cx">         } else
</span><del>-            builder.append(character);
</del><ins>+            buffer.append(character);
</ins><span class="cx">     }
</span><ins>+    return AtomicString(buffer.data(), buffer.size());
+}
</ins><span class="cx"> 
</span><del>-    return builder.toString();
</del><ins>+static AtomicString convertPropertyNameToAttributeName(const String&amp; name)
+{
+    if (name.isNull())
+        return nullAtom;
+
+    StringImpl* nameImpl = name.impl();
+    if (nameImpl-&gt;is8Bit())
+        return convertPropertyNameToAttributeName&lt;LChar&gt;(*nameImpl);
+    return convertPropertyNameToAttributeName&lt;UChar&gt;(*nameImpl);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void DatasetDOMStringMap::ref()
</span><span class="lines">@@ -146,15 +163,28 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-const AtomicString&amp; DatasetDOMStringMap::item(const String&amp; name, bool&amp; isValid)
</del><ins>+const AtomicString&amp; DatasetDOMStringMap::item(const String&amp; propertyName, bool&amp; isValid)
</ins><span class="cx"> {
</span><span class="cx">     isValid = false;
</span><span class="cx">     if (m_element.hasAttributes()) {
</span><del>-        for (const Attribute&amp; attribute : m_element.attributesIterator()) {
-            if (propertyNameMatchesAttributeName(name, attribute.localName())) {
</del><ins>+        AttributeIteratorAccessor attributeIteratorAccessor = m_element.attributesIterator();
+
+        if (attributeIteratorAccessor.attributeCount() == 1) {
+            // If the node has a single attribute, it is the dataset member accessed in most cases.
+            // Building a new AtomicString in that case is overkill so we do a direct character comparison.
+            const Attribute&amp; attribute = *attributeIteratorAccessor.begin();
+            if (propertyNameMatchesAttributeName(propertyName, attribute.localName())) {
</ins><span class="cx">                 isValid = true;
</span><span class="cx">                 return attribute.value();
</span><span class="cx">             }
</span><ins>+        } else {
+            AtomicString attributeName = convertPropertyNameToAttributeName(propertyName);
+            for (const Attribute&amp; attribute : attributeIteratorAccessor) {
+                if (attribute.localName() == attributeName) {
+                    isValid = true;
+                    return attribute.value();
+                }
+            }
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoredomElementDatah"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/ElementData.h (163846 => 163847)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/ElementData.h        2014-02-11 01:56:31 UTC (rev 163846)
+++ trunk/Source/WebCore/dom/ElementData.h        2014-02-11 02:11:44 UTC (rev 163847)
</span><span class="lines">@@ -67,6 +67,9 @@
</span><span class="cx"> 
</span><span class="cx">     AttributeConstIterator begin() const { return AttributeConstIterator(m_array, 0); }
</span><span class="cx">     AttributeConstIterator end() const { return AttributeConstIterator(m_array, m_size); }
</span><ins>+
+    unsigned attributeCount() const { return m_size; }
+
</ins><span class="cx"> private:
</span><span class="cx">     const Attribute* m_array;
</span><span class="cx">     unsigned m_size;
</span></span></pre>
</div>
</div>

</body>
</html>