<?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>224159</bug_id>
          
          <creation_ts>2021-04-03 12:48:12 -0700</creation_ts>
          <short_desc>Wasted vector capacity in MatchedDeclarationsCache::MatchResult</short_desc>
          <delta_ts>2021-04-08 08:44:53 -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>Safari Technology Preview</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>224313</dup_id>
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=224313</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Simon Fraser (smfr)">simon.fraser</reporter>
          <assigned_to name="Simon Fraser (smfr)">simon.fraser</assigned_to>
          <cc>darin</cc>
    
    <cc>jonlee</cc>
    
    <cc>koivisto</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1746813</commentid>
    <comment_count>0</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2021-04-03 12:48:12 -0700</bug_when>
    <thetext>With the patch from bug 186698 and loading nytimes article pages, we see wasted vector capacity under MatchResult:

Wasted capacity: 12144 bytes (used 992 of 13136 bytes, utilization: 7.55%) - 55 allocations
5   0x3ddd1abb2 WTF::VectorBuffer&lt;WebCore::Style::MatchedProperties, 0ul, WTF::FastMalloc&gt;::VectorBuffer(unsigned long, unsigned long)
6   0x3ddd1aa65 WTF::Vector&lt;WebCore::Style::MatchedProperties, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc&gt;::Vector(WTF::Vector&lt;WebCore::Style::MatchedProperties, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc&gt; const&amp;)
7   0x3ddd1aa0d WTF::Vector&lt;WebCore::Style::MatchedProperties, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc&gt;::Vector(WTF::Vector&lt;WebCore::Style::MatchedProperties, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc&gt; const&amp;)
8   0x3ddd1a9e0 WebCore::Style::MatchResult::MatchResult(WebCore::Style::MatchResult const&amp;)
9   0x3ddd1519d WebCore::Style::MatchResult::MatchResult(WebCore::Style::MatchResult const&amp;)
10  0x3ddd15056 WebCore::Style::MatchedDeclarationsCache::add(WebCore::RenderStyle const&amp;, WebCore::RenderStyle const&amp;, unsigned int, WebCore::Style::MatchResult const&amp;)
11  0x3ddd71e8e WebCore::Style::Resolver::applyMatchedProperties(WebCore::Style::Resolver::State&amp;, WebCore::Style::MatchResult const&amp;, WebCore::Style::Resolver::UseMatchedDeclarationsCache)
12  0x3ddd709ab WebCore::Style::Resolver::styleForElement(WebCore::Element const&amp;, WebCore::RenderStyle const*, WebCore::RenderStyle const*, WebCore::RuleMatchingBehavior, WebCore::SelectorFilter const*)
13  0x3dc2ca424 WebCore::Element::resolveStyle(WebCore::RenderStyle const*)
14  0x3dde53661 WebCore::SVGElement::resolveCustomStyle(WebCore::RenderStyle const&amp;, WebCore::RenderStyle const*)
15  0x3ddd8c5a6 WebCore::Style::TreeResolver::styleForStyleable(WebCore::Styleable const&amp;, WebCore::RenderStyle const&amp;)
16  0x3ddd8cbca WebCore::Style::TreeResolver::resolveElement(WebCore::Element&amp;)
17  0x3ddd8f46c WebCore::Style::TreeResolver::resolveComposedTree()
18  0x3ddd902d2 WebCore::Style::TreeResolver::resolve()
19  0x3dc1e6b07 WebCore::Document::resolveStyle(WebCore::Document::ResolveStyleType)
20  0x3dc1e7720 WebCore::Document::updateStyleIfNeeded()

Debugging suggests this is matchResult.authorDeclarations.

Nothing shrinks the vectors in the m_entries.add() Entry.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1746899</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-04-04 22:16:47 -0700</bug_when>
    <thetext>&lt;rdar://problem/76205835&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747102</commentid>
    <comment_count>2</comment_count>
      <attachid>425203</attachid>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2021-04-05 14:03:44 -0700</bug_when>
    <thetext>Created attachment 425203
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747109</commentid>
    <comment_count>3</comment_count>
      <attachid>425203</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-04-05 14:26:10 -0700</bug_when>
    <thetext>Comment on attachment 425203
Patch

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

&gt; Source/WebCore/style/ElementRuleCollector.cpp:602
&gt; +    m_result.userAgentDeclarations.shrinkToFit();
&gt; +    m_result.userDeclarations.shrinkToFit();
&gt; +    m_result.authorDeclarations.shrinkToFit();

Great to solve this: I don’t fully understand the choice of putting it here.

Presumably the reason we shrink these is that we are keeping these around for a while. Also, why don’t we need this when matchUARules is called directly?

It seems the place we want these calls are when these vectors go from being temporary things to being long-lived, and, to me, the end of this function is not obviously that place.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747127</commentid>
    <comment_count>4</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2021-04-05 15:10:12 -0700</bug_when>
    <thetext>That place is:
    m_entries.add(hash, Entry { matchResult, RenderStyle::clonePtr(style), RenderStyle::clonePtr(parentStyle) });

So we could have Entry construct compacted vectors.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747128</commentid>
    <comment_count>5</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-04-05 15:13:04 -0700</bug_when>
    <thetext>(In reply to Simon Fraser (smfr) from comment #4)
&gt; That place is:
&gt;     m_entries.add(hash, Entry { matchResult, RenderStyle::clonePtr(style),
&gt; RenderStyle::clonePtr(parentStyle) });
&gt; 
&gt; So we could have Entry construct compacted vectors.

Yes, or make a ElementRuleCollector function that does allthe shrinkToFit calls and call it there.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747149</commentid>
    <comment_count>6</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-04-05 16:05:41 -0700</bug_when>
    <thetext>It seems safe to do a shrinkToFit on a const object.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1747932</commentid>
    <comment_count>7</comment_count>
    <who name="Antti Koivisto">koivisto</who>
    <bug_when>2021-04-07 09:56:57 -0700</bug_when>
    <thetext>Maybe just add MatchResult::shrinkToFit and call it before creating the cache entry? In most cases MatchResults are temporaries so any work to shrink-to-fit is wasted.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1748305</commentid>
    <comment_count>8</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2021-04-08 08:44:53 -0700</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 224313 ***</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>425203</attachid>
            <date>2021-04-05 14:03:44 -0700</date>
            <delta_ts>2021-04-05 15:46:53 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-224159-20210405140343.patch</filename>
            <type>text/plain</type>
            <size>1700</size>
            <attacher name="Simon Fraser (smfr)">simon.fraser</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZ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