<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>86434</bug_id>
          
          <creation_ts>2012-05-14 21:40:53 -0700</creation_ts>
          <short_desc>RuleSet::addToRuleSet wastes a bit of Vector capacity</short_desc>
          <delta_ts>2012-05-15 10:03:59 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>CSS</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>86281</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Simon Fraser (smfr)">simon.fraser</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>darin</cc>
    
    <cc>kling</cc>
    
    <cc>koivisto</cc>
    
    <cc>macpherson</cc>
    
    <cc>menard</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>webkit.review.bot</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>623625</commentid>
    <comment_count>0</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2012-05-14 21:40:53 -0700</bug_when>
    <thetext>Data collected via bug 86281 show that RuleSet::addToRuleSet() is the most wasteful call site for excess vector capacity.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>623629</commentid>
    <comment_count>1</comment_count>
    <who name="Andreas Kling">kling</who>
    <bug_when>2012-05-14 21:44:18 -0700</bug_when>
    <thetext>Hm. This is somewhat surprising since we shrinkToFit() most of the vectors in RuleSet::shrinkToFit(). Something must be going wrong here. :/</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>623790</commentid>
    <comment_count>2</comment_count>
    <who name="Antti Koivisto">koivisto</who>
    <bug_when>2012-05-15 01:30:28 -0700</bug_when>
    <thetext>Simon, does your logging work correctly when shrinkToFit is used?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624100</commentid>
    <comment_count>3</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2012-05-15 08:14:07 -0700</bug_when>
    <thetext>Apparently not (you can see the patch in bug 86281).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624140</commentid>
    <comment_count>4</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2012-05-15 08:44:31 -0700</bug_when>
    <thetext>Previous data was not taking shrinkToFit into account. New data shows addToRuleSet() to be a lot less wasteful, but there is one stack that shows up in the top 20:

117 vectors, 7.11KB used of 73.12KB, 66.02KB wasted at:
1   0x108c1a255 WTF::Vector&lt;WebCore::RuleData, 0ul&gt;::Vector()
2   0x108c07a55 WTF::Vector&lt;WebCore::RuleData, 0ul&gt;::Vector()
3   0x108bfb4e1 WebCore::RuleSet::addToRuleSet(WTF::AtomicStringImpl*, WTF::HashMap&lt;WTF::AtomicStringImpl*, WTF::OwnPtr&lt;WTF::Vector&lt;WebCore::RuleData, 0ul&gt; &gt;, WTF::PtrHash&lt;WTF::AtomicStringImpl*&gt;, WTF::HashTraits&lt;WTF::AtomicStringImpl*&gt;, WTF::HashTraits&lt;WTF::OwnPtr&lt;WTF::Vector&lt;WebCore::RuleData, 0ul&gt; &gt; &gt; &gt;&amp;, WebCore::RuleData const&amp;)
4   0x108bfb6fb WebCore::RuleSet::addRule(WebCore::StyleRule*, WebCore::CSSSelector*, bool, bool, bool)
5   0x108beac71 _ZN7WebCoreL11makeRuleSetERKN3WTF6VectorINS_13StyleResolver11RuleFeatureELm0EEE</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624151</commentid>
    <comment_count>5</comment_count>
      <attachid>141983</attachid>
    <who name="Andreas Kling">kling</who>
    <bug_when>2012-05-15 08:52:28 -0700</bug_when>
    <thetext>Created attachment 141983
Proposed patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624163</commentid>
    <comment_count>6</comment_count>
      <attachid>141983</attachid>
    <who name="Antti Koivisto">koivisto</who>
    <bug_when>2012-05-15 09:09:01 -0700</bug_when>
    <thetext>Comment on attachment 141983
Proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=141983&amp;action=review

r=me

&gt; Source/WebCore/ChangeLog:3
&gt; +        RuleSet::addToRuleSet wastes a lot of Vector capacity.

You might want to update the title. This hardly wastes &quot;a lot&quot;.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624189</commentid>
    <comment_count>7</comment_count>
    <who name="Andreas Kling">kling</who>
    <bug_when>2012-05-15 09:45:35 -0700</bug_when>
    <thetext>Committed r117084: &lt;http://trac.webkit.org/changeset/117084&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624192</commentid>
    <comment_count>8</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2012-05-15 09:50:22 -0700</bug_when>
    <thetext>In cases like this where we know the size the RuleSet will be before we create it, it would be more efficient to reserve capacity than to shrink to fit. All we’d have to do is make a RuleSet::create function overload that takes a capacity.

What do you guys think of that idea?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624195</commentid>
    <comment_count>9</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2012-05-15 09:54:45 -0700</bug_when>
    <thetext>Well maybe this is the only call site like that; the point is that you save more memory by not allocating the larger block and shrinking. I suspect that in many cases the memory saved by shrinking after the fact can’t be reused due to fragmentation.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624198</commentid>
    <comment_count>10</comment_count>
    <who name="Andreas Kling">kling</who>
    <bug_when>2012-05-15 09:58:42 -0700</bug_when>
    <thetext>(In reply to comment #8)
&gt; In cases like this where we know the size the RuleSet will be before we create it, it would be more efficient to reserve capacity than to shrink to fit. All we’d have to do is make a RuleSet::create function overload that takes a capacity.
&gt; 
&gt; What do you guys think of that idea?

I like the idea in principle. If you look at the implementation of RuleSet::shrinkToFit() however, it&apos;s nontrivial to predict at RuleSet::create() time how big each subvector should be.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624204</commentid>
    <comment_count>11</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2012-05-15 10:03:28 -0700</bug_when>
    <thetext>OK. Got it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>624205</commentid>
    <comment_count>12</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2012-05-15 10:03:59 -0700</bug_when>
    <thetext>One last thought on this: The prediction can be wrong some of the time as long as it’s right most of the time.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>141983</attachid>
            <date>2012-05-15 08:52:28 -0700</date>
            <delta_ts>2012-05-15 09:09:00 -0700</delta_ts>
            <desc>Proposed patch</desc>
            <filename>bug-86434.diff</filename>
            <type>text/plain</type>
            <size>1331</size>
            <attacher name="Andreas Kling">kling</attacher>
            
              <data encoding="base64">ZGlmZiAtLWdpdCBhL1NvdXJjZS9XZWJDb3JlL0NoYW5nZUxvZyBiL1NvdXJjZS9XZWJDb3JlL0No
YW5nZUxvZwppbmRleCA3YmJjNzI5Li40YjQ1NTYwIDEwMDY0NAotLS0gYS9Tb3VyY2UvV2ViQ29y
ZS9DaGFuZ2VMb2cKKysrIGIvU291cmNlL1dlYkNvcmUvQ2hhbmdlTG9nCkBAIC0xLDMgKzEsMTYg
QEAKKzIwMTItMDUtMTUgIEFuZHJlYXMgS2xpbmcgIDxrbGluZ0B3ZWJraXQub3JnPgorCisgICAg
ICAgIFJ1bGVTZ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Chjb25zdCBWZWN0b3I8U3R5bGVSZXNvbHZlcjo6UnVsZUZlYXR1cmU+JgogICAgIE93blB0
cjxSdWxlU2V0PiBydWxlU2V0ID0gUnVsZVNldDo6Y3JlYXRlKCk7CiAgICAgZm9yIChzaXplX3Qg
aSA9IDA7IGkgPCBzaXplOyArK2kpCiAgICAgICAgIHJ1bGVTZXQtPmFkZFJ1bGUocnVsZXNbaV0u
cnVsZSwgcnVsZXNbaV0uc2VsZWN0b3IsIHJ1bGVzW2ldLmhhc0RvY3VtZW50U2VjdXJpdHlPcmln
aW4sIGZhbHNlKTsKKyAgICBydWxlU2V0LT5zaHJpbmtUb0ZpdCgpOwogICAgIHJldHVybiBydWxl
U2V0LnJlbGVhc2UoKTsKIH0KIAo=
</data>
<flag name="review"
          id="148202"
          type_id="1"
          status="+"
          setter="koivisto"
    />
          </attachment>
      

    </bug>

</bugzilla>