Shrink MediaQuerySets to fit after parsing.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Aug 2016 01:44:43 +0000 (01:44 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Aug 2016 01:44:43 +0000 (01:44 +0000)
<https://webkit.org/b/160419>

Reviewed by Antti Koivisto.

Add shrinkToFit() helpers to MediaQuerySet and MediaQuery
and call it after parsing to remove extra capacity from their
internal vectors.

This reduces memory usage by 1.24MB on theverge.com.

* css/CSSParser.cpp:
(WebCore::CSSParser::createMediaRule):
* css/MediaList.cpp:
(WebCore::MediaQuerySet::parse):
(WebCore::MediaQuerySet::shrinkToFit):
* css/MediaList.h:
* css/MediaQuery.h:
(WebCore::MediaQuery::shrinkToFit):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@204006 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/css/CSSParser.cpp
Source/WebCore/css/MediaList.cpp
Source/WebCore/css/MediaList.h
Source/WebCore/css/MediaQuery.h

index 5ed8a67..cc54b40 100644 (file)
@@ -1,3 +1,25 @@
+2016-08-01  Andreas Kling  <akling@apple.com>
+
+        Shrink MediaQuerySets to fit after parsing.
+        <https://webkit.org/b/160419>
+
+        Reviewed by Antti Koivisto.
+
+        Add shrinkToFit() helpers to MediaQuerySet and MediaQuery
+        and call it after parsing to remove extra capacity from their
+        internal vectors.
+
+        This reduces memory usage by 1.24MB on theverge.com.
+
+        * css/CSSParser.cpp:
+        (WebCore::CSSParser::createMediaRule):
+        * css/MediaList.cpp:
+        (WebCore::MediaQuerySet::parse):
+        (WebCore::MediaQuerySet::shrinkToFit):
+        * css/MediaList.h:
+        * css/MediaQuery.h:
+        (WebCore::MediaQuery::shrinkToFit):
+
 2016-08-01  Tim Horton  <timothy_horton@apple.com>
 
         <attachment> action label doesn't wrap, but needs to for some localizations
index 188d224..2771aae 100644 (file)
@@ -12905,8 +12905,10 @@ Ref<StyleRuleMedia> CSSParser::createMediaRule(RefPtr<MediaQuerySet>&& media, Ru
         // To comply with w3c test suite expectation, create an empty media query
         // even when it is syntactically incorrect.
         rule = StyleRuleMedia::create(MediaQuerySet::create(), emptyRules);
-    } else
+    } else {
+        media->shrinkToFit();
         rule = StyleRuleMedia::create(media.releaseNonNull(), rules ? *rules : emptyRules);
+    }
     processAndAddNewRuleToSourceTreeIfNeeded();
     return rule.releaseNonNull();
 }
index 2fe9d96..b173c52 100644 (file)
@@ -158,6 +158,7 @@ bool MediaQuerySet::parse(const String& mediaString)
             return false;
     }
     m_queries = WTFMove(result);
+    shrinkToFit();
     return true;
 }
 
@@ -198,6 +199,13 @@ String MediaQuerySet::mediaText() const
     return text.toString();
 }
 
+void MediaQuerySet::shrinkToFit()
+{
+    m_queries.shrinkToFit();
+    for (auto& query : m_queries)
+        query.shrinkToFit();
+}
+
 MediaList::MediaList(MediaQuerySet* mediaQueries, CSSStyleSheet* parentSheet)
     : m_mediaQueries(mediaQueries)
     , m_parentStyleSheet(parentSheet)
index da76550..c26ce2f 100644 (file)
@@ -66,6 +66,8 @@ public:
 
     Ref<MediaQuerySet> copy() const { return adoptRef(*new MediaQuerySet(*this)); }
 
+    void shrinkToFit();
+
 private:
     MediaQuerySet();
     MediaQuerySet(const String& mediaQuery, bool fallbackToDescription);
index 20984d3..365a3a2 100644 (file)
@@ -49,6 +49,8 @@ public:
 
     bool operator==(const MediaQuery& other) const;
 
+    void shrinkToFit() { m_expressions.shrinkToFit(); }
+
 private:
     String serialize() const;