<!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>[175067] 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/175067">175067</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2014-10-22 14:16:48 -0700 (Wed, 22 Oct 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Avoid repeated is&lt;MutableStyleProperties&gt;() checks in StyleProperties
https://bugs.webkit.org/show_bug.cgi?id=137978

Reviewed by Andreas Kling.

Reduce the amount of is&lt;MutableStyleProperties&gt;() checks in
StyleProperties by:
- Removing the checks in each method of StyleProperties::PropertyReference,
  and replace them by a single check in StyleProperties::propertyAt().
- Providing faster propertyCount() / propertyAt() / isEmpty() methods on
  MutableStyleProperties / ImmutableStyleProperties as a lot of callers use
  tight typing.
- Using tighter typing at call sites whenever possible.

Ideally, we could be able to use iterators instead of propertyCount() /
propertyAt() but this is not trivial to do efficiently as the
representation in memory is fundamentally different for MutableStyleProperties
/ ImmutableStyleProperties. We may be able to do better longer term, but
this is already more efficient short term at least.

No new tests, no behavior change.

* css/StyleProperties.cpp:
(WebCore::MutableStyleProperties::MutableStyleProperties):
(WebCore::StyleProperties::PropertyReference::cssText):
* css/StyleProperties.h:
(WebCore::StyleProperties::PropertyReference::PropertyReference):
(WebCore::StyleProperties::PropertyReference::id):
(WebCore::StyleProperties::PropertyReference::shorthandID):
(WebCore::StyleProperties::PropertyReference::isImportant):
(WebCore::StyleProperties::PropertyReference::isInherited):
(WebCore::StyleProperties::PropertyReference::isImplicit):
(WebCore::StyleProperties::PropertyReference::value):
(WebCore::StyleProperties::PropertyReference::toCSSProperty):
(WebCore::StyleProperties::isEmpty):
(WebCore::ImmutableStyleProperties::isEmpty):
(WebCore::MutableStyleProperties::isEmpty):
(WebCore::ImmutableStyleProperties::propertyAt):
(WebCore::MutableStyleProperties::propertyAt):
(WebCore::StyleProperties::propertyAt):
(WebCore::StyleProperties::propertyCount):
(WebCore::StyleProperties::PropertyReference::propertyMetadata): Deleted.
(WebCore::StyleProperties::PropertyReference::propertyValue): Deleted.
* editing/EditingStyle.cpp:
(WebCore::removePropertiesInStyle):
(WebCore::EditingStyle::removePropertiesInElementDefaultStyle):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssStylePropertiescpp">trunk/Source/WebCore/css/StyleProperties.cpp</a></li>
<li><a href="#trunkSourceWebCorecssStylePropertiesh">trunk/Source/WebCore/css/StyleProperties.h</a></li>
<li><a href="#trunkSourceWebCoreeditingEditingStylecpp">trunk/Source/WebCore/editing/EditingStyle.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (175066 => 175067)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-10-22 21:14:07 UTC (rev 175066)
+++ trunk/Source/WebCore/ChangeLog        2014-10-22 21:16:48 UTC (rev 175067)
</span><span class="lines">@@ -1,3 +1,52 @@
</span><ins>+2014-10-22  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        Avoid repeated is&lt;MutableStyleProperties&gt;() checks in StyleProperties
+        https://bugs.webkit.org/show_bug.cgi?id=137978
+
+        Reviewed by Andreas Kling.
+
+        Reduce the amount of is&lt;MutableStyleProperties&gt;() checks in
+        StyleProperties by:
+        - Removing the checks in each method of StyleProperties::PropertyReference,
+          and replace them by a single check in StyleProperties::propertyAt().
+        - Providing faster propertyCount() / propertyAt() / isEmpty() methods on
+          MutableStyleProperties / ImmutableStyleProperties as a lot of callers use
+          tight typing.
+        - Using tighter typing at call sites whenever possible.
+
+        Ideally, we could be able to use iterators instead of propertyCount() /
+        propertyAt() but this is not trivial to do efficiently as the
+        representation in memory is fundamentally different for MutableStyleProperties
+        / ImmutableStyleProperties. We may be able to do better longer term, but
+        this is already more efficient short term at least.
+
+        No new tests, no behavior change.
+
+        * css/StyleProperties.cpp:
+        (WebCore::MutableStyleProperties::MutableStyleProperties):
+        (WebCore::StyleProperties::PropertyReference::cssText):
+        * css/StyleProperties.h:
+        (WebCore::StyleProperties::PropertyReference::PropertyReference):
+        (WebCore::StyleProperties::PropertyReference::id):
+        (WebCore::StyleProperties::PropertyReference::shorthandID):
+        (WebCore::StyleProperties::PropertyReference::isImportant):
+        (WebCore::StyleProperties::PropertyReference::isInherited):
+        (WebCore::StyleProperties::PropertyReference::isImplicit):
+        (WebCore::StyleProperties::PropertyReference::value):
+        (WebCore::StyleProperties::PropertyReference::toCSSProperty):
+        (WebCore::StyleProperties::isEmpty):
+        (WebCore::ImmutableStyleProperties::isEmpty):
+        (WebCore::MutableStyleProperties::isEmpty):
+        (WebCore::ImmutableStyleProperties::propertyAt):
+        (WebCore::MutableStyleProperties::propertyAt):
+        (WebCore::StyleProperties::propertyAt):
+        (WebCore::StyleProperties::propertyCount):
+        (WebCore::StyleProperties::PropertyReference::propertyMetadata): Deleted.
+        (WebCore::StyleProperties::PropertyReference::propertyValue): Deleted.
+        * editing/EditingStyle.cpp:
+        (WebCore::removePropertiesInStyle):
+        (WebCore::EditingStyle::removePropertiesInElementDefaultStyle):
+
</ins><span class="cx"> 2014-10-22  Eric Carlson  &lt;eric.carlson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Mac][MediaStream] clean up bit rot
</span></span></pre></div>
<a id="trunkSourceWebCorecssStylePropertiescpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleProperties.cpp (175066 => 175067)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleProperties.cpp        2014-10-22 21:14:07 UTC (rev 175066)
+++ trunk/Source/WebCore/css/StyleProperties.cpp        2014-10-22 21:16:48 UTC (rev 175067)
</span><span class="lines">@@ -109,9 +109,11 @@
</span><span class="cx">     if (is&lt;MutableStyleProperties&gt;(other))
</span><span class="cx">         m_propertyVector = downcast&lt;MutableStyleProperties&gt;(other).m_propertyVector;
</span><span class="cx">     else {
</span><del>-        m_propertyVector.reserveInitialCapacity(other.propertyCount());
-        for (unsigned i = 0; i &lt; other.propertyCount(); ++i)
-            m_propertyVector.uncheckedAppend(other.propertyAt(i).toCSSProperty());
</del><ins>+        const auto&amp; immutableOther = downcast&lt;ImmutableStyleProperties&gt;(other);
+        unsigned propertyCount = immutableOther.propertyCount();
+        m_propertyVector.reserveInitialCapacity(propertyCount);
+        for (unsigned i = 0; i &lt; propertyCount; ++i)
+            m_propertyVector.uncheckedAppend(immutableOther.propertyAt(i).toCSSProperty());
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -1257,7 +1259,7 @@
</span><span class="cx">     StringBuilder result;
</span><span class="cx">     result.append(cssName());
</span><span class="cx">     result.appendLiteral(&quot;: &quot;);
</span><del>-    result.append(propertyValue()-&gt;cssText());
</del><ins>+    result.append(m_value-&gt;cssText());
</ins><span class="cx">     if (isImportant())
</span><span class="cx">         result.appendLiteral(&quot; !important&quot;);
</span><span class="cx">     result.append(';');
</span></span></pre></div>
<a id="trunkSourceWebCorecssStylePropertiesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleProperties.h (175066 => 175067)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleProperties.h        2014-10-22 21:14:07 UTC (rev 175066)
+++ trunk/Source/WebCore/css/StyleProperties.h        2014-10-22 21:16:48 UTC (rev 175067)
</span><span class="lines">@@ -55,40 +55,36 @@
</span><span class="cx"> 
</span><span class="cx">     class PropertyReference {
</span><span class="cx">     public:
</span><del>-        PropertyReference(const StyleProperties&amp; propertySet, unsigned index)
-            : m_propertySet(propertySet)
-            , m_index(index)
-        {
-        }
</del><ins>+        PropertyReference(const StylePropertyMetadata&amp; metadata, const CSSValue* value)
+            : m_metadata(metadata)
+            , m_value(value)
+        { }
</ins><span class="cx"> 
</span><del>-        CSSPropertyID id() const { return static_cast&lt;CSSPropertyID&gt;(propertyMetadata().m_propertyID); }
-        CSSPropertyID shorthandID() const { return propertyMetadata().shorthandID(); }
</del><ins>+        CSSPropertyID id() const { return static_cast&lt;CSSPropertyID&gt;(m_metadata.m_propertyID); }
+        CSSPropertyID shorthandID() const { return m_metadata.shorthandID(); }
</ins><span class="cx"> 
</span><del>-        bool isImportant() const { return propertyMetadata().m_important; }
-        bool isInherited() const { return propertyMetadata().m_inherited; }
-        bool isImplicit() const { return propertyMetadata().m_implicit; }
</del><ins>+        bool isImportant() const { return m_metadata.m_important; }
+        bool isInherited() const { return m_metadata.m_inherited; }
+        bool isImplicit() const { return m_metadata.m_implicit; }
</ins><span class="cx"> 
</span><span class="cx">         String cssName() const;
</span><span class="cx">         String cssText() const;
</span><span class="cx"> 
</span><del>-        const CSSValue* value() const { return propertyValue(); }
</del><ins>+        const CSSValue* value() const { return m_value; }
</ins><span class="cx">         // FIXME: We should try to remove this mutable overload.
</span><del>-        CSSValue* value() { return const_cast&lt;CSSValue*&gt;(propertyValue()); }
</del><ins>+        CSSValue* value() { return const_cast&lt;CSSValue*&gt;(m_value); }
</ins><span class="cx"> 
</span><span class="cx">         // FIXME: Remove this.
</span><del>-        CSSProperty toCSSProperty() const { return CSSProperty(id(), const_cast&lt;CSSValue*&gt;(propertyValue()), isImportant(), propertyMetadata().m_isSetFromShorthand, propertyMetadata().m_indexInShorthandsVector, isImplicit()); }
-        const StylePropertyMetadata&amp; propertyMetadata() const;
</del><ins>+        CSSProperty toCSSProperty() const { return CSSProperty(id(), const_cast&lt;CSSValue*&gt;(m_value), isImportant(), m_metadata.m_isSetFromShorthand, m_metadata.m_indexInShorthandsVector, isImplicit()); }
</ins><span class="cx"> 
</span><span class="cx">     private:
</span><del>-        const CSSValue* propertyValue() const;
-
-        const StyleProperties&amp; m_propertySet;
-        unsigned m_index;
</del><ins>+        const StylePropertyMetadata&amp; m_metadata;
+        const CSSValue* m_value;
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     unsigned propertyCount() const;
</span><del>-    bool isEmpty() const;
-    PropertyReference propertyAt(unsigned index) const { return PropertyReference(*this, index); }
</del><ins>+    bool isEmpty() const { return !propertyCount(); }
+    PropertyReference propertyAt(unsigned) const;
</ins><span class="cx"> 
</span><span class="cx">     PassRefPtr&lt;CSSValue&gt; getPropertyCSSValue(CSSPropertyID) const;
</span><span class="cx">     String getPropertyValue(CSSPropertyID) const;
</span><span class="lines">@@ -161,6 +157,8 @@
</span><span class="cx">     static PassRef&lt;ImmutableStyleProperties&gt; create(const CSSProperty* properties, unsigned count, CSSParserMode);
</span><span class="cx"> 
</span><span class="cx">     unsigned propertyCount() const { return m_arraySize; }
</span><ins>+    bool isEmpty() const { return !propertyCount(); }
+    PropertyReference propertyAt(unsigned index) const;
</ins><span class="cx"> 
</span><span class="cx">     const CSSValue** valueArray() const;
</span><span class="cx">     const StylePropertyMetadata* metadataArray() const;
</span><span class="lines">@@ -190,6 +188,8 @@
</span><span class="cx">     WEBCORE_EXPORT ~MutableStyleProperties();
</span><span class="cx"> 
</span><span class="cx">     unsigned propertyCount() const { return m_propertyVector.size(); }
</span><ins>+    bool isEmpty() const { return !propertyCount(); }
+    PropertyReference propertyAt(unsigned index) const;
</ins><span class="cx"> 
</span><span class="cx">     PropertySetCSSStyleDeclaration* cssStyleDeclaration();
</span><span class="cx"> 
</span><span class="lines">@@ -236,30 +236,29 @@
</span><span class="cx">     friend class StyleProperties;
</span><span class="cx"> };
</span><span class="cx"> 
</span><del>-inline const StylePropertyMetadata&amp; StyleProperties::PropertyReference::propertyMetadata() const
</del><ins>+inline ImmutableStyleProperties::PropertyReference ImmutableStyleProperties::propertyAt(unsigned index) const
</ins><span class="cx"> {
</span><del>-    if (is&lt;MutableStyleProperties&gt;(m_propertySet))
-        return downcast&lt;MutableStyleProperties&gt;(m_propertySet).m_propertyVector.at(m_index).metadata();
-    return downcast&lt;ImmutableStyleProperties&gt;(m_propertySet).metadataArray()[m_index];
</del><ins>+    return PropertyReference(metadataArray()[index], valueArray()[index]);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline const CSSValue* StyleProperties::PropertyReference::propertyValue() const
</del><ins>+inline MutableStyleProperties::PropertyReference MutableStyleProperties::propertyAt(unsigned index) const
</ins><span class="cx"> {
</span><del>-    if (is&lt;MutableStyleProperties&gt;(m_propertySet))
-        return downcast&lt;MutableStyleProperties&gt;(m_propertySet).m_propertyVector.at(m_index).value();
-    return downcast&lt;ImmutableStyleProperties&gt;(m_propertySet).valueArray()[m_index];
</del><ins>+    const CSSProperty&amp; property = m_propertyVector[index];
+    return PropertyReference(property.metadata(), property.value());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline unsigned StyleProperties::propertyCount() const
</del><ins>+inline StyleProperties::PropertyReference StyleProperties::propertyAt(unsigned index) const
</ins><span class="cx"> {
</span><span class="cx">     if (is&lt;MutableStyleProperties&gt;(*this))
</span><del>-        return downcast&lt;MutableStyleProperties&gt;(*this).m_propertyVector.size();
-    return m_arraySize;
</del><ins>+        return downcast&lt;MutableStyleProperties&gt;(*this).propertyAt(index);
+    return downcast&lt;ImmutableStyleProperties&gt;(*this).propertyAt(index);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline bool StyleProperties::isEmpty() const
</del><ins>+inline unsigned StyleProperties::propertyCount() const
</ins><span class="cx"> {
</span><del>-    return !propertyCount();
</del><ins>+    if (is&lt;MutableStyleProperties&gt;(*this))
+        return downcast&lt;MutableStyleProperties&gt;(*this).propertyCount();
+    return downcast&lt;ImmutableStyleProperties&gt;(*this).propertyCount();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> inline void StyleProperties::deref()
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingEditingStylecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/EditingStyle.cpp (175066 => 175067)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/EditingStyle.cpp        2014-10-22 21:14:07 UTC (rev 175066)
+++ trunk/Source/WebCore/editing/EditingStyle.cpp        2014-10-22 21:16:48 UTC (rev 175067)
</span><span class="lines">@@ -1142,7 +1142,7 @@
</span><span class="cx">     m_mutableStyle-&gt;mergeAndOverrideOnConflict(*fromComputedStyle);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void removePropertiesInStyle(MutableStyleProperties* styleToRemovePropertiesFrom, StyleProperties* style)
</del><ins>+static void removePropertiesInStyle(MutableStyleProperties* styleToRemovePropertiesFrom, MutableStyleProperties* style)
</ins><span class="cx"> {
</span><span class="cx">     unsigned propertyCount = style-&gt;propertyCount();
</span><span class="cx">     Vector&lt;CSSPropertyID&gt; propertiesToRemove(propertyCount);
</span><span class="lines">@@ -1188,7 +1188,7 @@
</span><span class="cx">     if (!m_mutableStyle || m_mutableStyle-&gt;isEmpty())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    RefPtr&lt;StyleProperties&gt; defaultStyle = styleFromMatchedRulesForElement(element, StyleResolver::UAAndUserCSSRules);
</del><ins>+    RefPtr&lt;MutableStyleProperties&gt; defaultStyle = styleFromMatchedRulesForElement(element, StyleResolver::UAAndUserCSSRules);
</ins><span class="cx"> 
</span><span class="cx">     removePropertiesInStyle(m_mutableStyle.get(), defaultStyle.get());
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>