Title: [105186] trunk/Source/WebCore
Revision
105186
Author
[email protected]
Date
2012-01-17 12:57:23 -0800 (Tue, 17 Jan 2012)

Log Message

SpaceSplitString: Share equivalent string piece vectors.
<http://webkit.org/b/76458>

Reviewed by Antti Koivisto.

Make SpaceSplitStringData ref-counted and cache them in a hashmap to reduce memory
usage and avoid redundant string splitting work.  This reduces memory consumption
by 618 kB (on 64-bit) when viewing the full HTML5 spec at <http://whatwg.org/c>

* dom/SpaceSplitString.h:

    Add the source string to SpaceSplitStringData so we have a key for uncaching
    in the destructor. Also bumped the vector's inline size from 2 to 4.

* dom/SpaceSplitString.cpp:
(WebCore::sharedDataMap):
(WebCore::SpaceSplitStringData::create):
(WebCore::SpaceSplitStringData::createUnique):
(WebCore::SpaceSplitStringData::SpaceSplitStringData):
(WebCore::SpaceSplitString::SpaceSplitString):
(WebCore::SpaceSplitString::set):
(WebCore::SpaceSplitStringData::createVector):

    Added create helpers for SpaceSplitStringData. Moved case folding from
    createVector() to create(AtomicString). Added a hash map for caching
    AtomicString -> SpaceSplitStringData.

(WebCore::SpaceSplitStringData::~SpaceSplitStringData):

    Remove the SpaceSplitStringData from the sharedDataMap().

* dom/SpaceSplitString.cpp:
(WebCore::SpaceSplitString::ensureUnique):

    Added, detaches from the shared SpaceSplitStringData if necessary.

(WebCore::SpaceSplitStringData::add):
(WebCore::SpaceSplitStringData::remove):

    Assert that add() and remove() are only used on unique SpaceSplitStringData.

(WebCore::SpaceSplitString::add):
(WebCore::SpaceSplitString::remove):

    Call ensureUnique() to potentially detach from a shared SpaceSplitStringData
    before making modifications.

* dom/SpaceSplitString.cpp:
(WebCore::SpaceSplitStringData::containsAll):

    Added gratuitous fast path for containsAll(*this).

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (105185 => 105186)


--- trunk/Source/WebCore/ChangeLog	2012-01-17 20:44:49 UTC (rev 105185)
+++ trunk/Source/WebCore/ChangeLog	2012-01-17 20:57:23 UTC (rev 105186)
@@ -1,3 +1,57 @@
+2012-01-17  Andreas Kling  <[email protected]>
+
+        SpaceSplitString: Share equivalent string piece vectors.
+        <http://webkit.org/b/76458>
+
+        Reviewed by Antti Koivisto.
+
+        Make SpaceSplitStringData ref-counted and cache them in a hashmap to reduce memory
+        usage and avoid redundant string splitting work.  This reduces memory consumption
+        by 618 kB (on 64-bit) when viewing the full HTML5 spec at <http://whatwg.org/c>
+
+        * dom/SpaceSplitString.h:
+
+            Add the source string to SpaceSplitStringData so we have a key for uncaching
+            in the destructor. Also bumped the vector's inline size from 2 to 4.
+
+        * dom/SpaceSplitString.cpp:
+        (WebCore::sharedDataMap):
+        (WebCore::SpaceSplitStringData::create):
+        (WebCore::SpaceSplitStringData::createUnique):
+        (WebCore::SpaceSplitStringData::SpaceSplitStringData):
+        (WebCore::SpaceSplitString::SpaceSplitString):
+        (WebCore::SpaceSplitString::set):
+        (WebCore::SpaceSplitStringData::createVector):
+
+            Added create helpers for SpaceSplitStringData. Moved case folding from
+            createVector() to create(AtomicString). Added a hash map for caching
+            AtomicString -> SpaceSplitStringData.
+
+        (WebCore::SpaceSplitStringData::~SpaceSplitStringData):
+
+            Remove the SpaceSplitStringData from the sharedDataMap().
+
+        * dom/SpaceSplitString.cpp:
+        (WebCore::SpaceSplitString::ensureUnique):
+
+            Added, detaches from the shared SpaceSplitStringData if necessary.
+
+        (WebCore::SpaceSplitStringData::add):
+        (WebCore::SpaceSplitStringData::remove):
+
+            Assert that add() and remove() are only used on unique SpaceSplitStringData.
+
+        (WebCore::SpaceSplitString::add):
+        (WebCore::SpaceSplitString::remove):
+
+            Call ensureUnique() to potentially detach from a shared SpaceSplitStringData
+            before making modifications.
+
+        * dom/SpaceSplitString.cpp:
+        (WebCore::SpaceSplitStringData::containsAll):
+
+            Added gratuitous fast path for containsAll(*this).
+
 2012-01-12  David Hyatt  <[email protected]>
 
         https://bugs.webkit.org/show_bug.cgi?id=76197

Modified: trunk/Source/WebCore/dom/SpaceSplitString.cpp (105185 => 105186)


--- trunk/Source/WebCore/dom/SpaceSplitString.cpp	2012-01-17 20:44:49 UTC (rev 105185)
+++ trunk/Source/WebCore/dom/SpaceSplitString.cpp	2012-01-17 20:57:23 UTC (rev 105186)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2007 David Smith ([email protected])
- * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
+ * Copyright (C) 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -23,6 +23,8 @@
 
 #include "HTMLParserIdioms.h"
 #include <wtf/ASCIICType.h>
+#include <wtf/HashMap.h>
+#include <wtf/text/AtomicStringHash.h>
 #include <wtf/text/StringBuilder.h>
 
 using namespace WTF;
@@ -43,13 +45,8 @@
     return hasUpper || (ored & ~0x7F);
 }
 
-void SpaceSplitStringData::createVector(const String& inputString, bool shouldFoldCase)
+void SpaceSplitStringData::createVector(const String& string)
 {
-    String string(inputString);
-
-    if (shouldFoldCase && hasNonASCIIOrUpper(inputString))
-        string = inputString.foldCase();
-
     const UChar* characters = string.characters();
     unsigned length = string.length();
     unsigned start = 0;
@@ -70,6 +67,9 @@
 
 bool SpaceSplitStringData::containsAll(SpaceSplitStringData& other)
 {
+    if (this == &other)
+        return true;
+
     size_t thisSize = m_vector.size();
     size_t otherSize = other.m_vector.size();
     for (size_t i = 0; i < otherSize; ++i) {
@@ -87,6 +87,7 @@
 
 void SpaceSplitStringData::add(const AtomicString& string)
 {
+    ASSERT(hasOneRef());
     if (contains(string))
         return;
 
@@ -95,6 +96,7 @@
 
 void SpaceSplitStringData::remove(const AtomicString& string)
 {
+    ASSERT(hasOneRef());
     size_t position = 0;
     while (position < m_vector.size()) {
         if (m_vector[position] == string)
@@ -106,14 +108,74 @@
 
 void SpaceSplitString::add(const AtomicString& string)
 {
+    ensureUnique();
     if (m_data)
         m_data->add(string);
 }
 
 void SpaceSplitString::remove(const AtomicString& string)
 {
+    ensureUnique();
     if (m_data)
         m_data->remove(string);
 }
 
+typedef HashMap<AtomicString, SpaceSplitStringData*> SpaceSplitStringDataMap;
+
+static SpaceSplitStringDataMap& sharedDataMap()
+{
+    DEFINE_STATIC_LOCAL(SpaceSplitStringDataMap, map, ());
+    return map;
+}
+
+void SpaceSplitString::set(const AtomicString& inputString, bool shouldFoldCase)
+{
+    if (inputString.isNull()) {
+        clear();
+        return;
+    }
+
+    String string(inputString.string());
+    if (shouldFoldCase && hasNonASCIIOrUpper(string))
+        string = string.foldCase();
+
+    m_data = SpaceSplitStringData::create(string);
+}
+
+SpaceSplitStringData::~SpaceSplitStringData()
+{
+    if (!m_keyString.isNull())
+        sharedDataMap().remove(m_keyString);
+}
+
+PassRefPtr<SpaceSplitStringData> SpaceSplitStringData::create(const AtomicString& string)
+{
+    SpaceSplitStringData*& data = "" 0).first->second;
+    if (!data) {
+        data = "" SpaceSplitStringData(string);
+        return adoptRef(data);
+    }
+    return data;
+}
+
+PassRefPtr<SpaceSplitStringData> SpaceSplitStringData::createUnique(const SpaceSplitStringData& other)
+{
+    return adoptRef(new SpaceSplitStringData(other));
+}
+
+SpaceSplitStringData::SpaceSplitStringData(const AtomicString& string)
+    : m_keyString(string)
+{
+    ASSERT(!string.isNull());
+    createVector(string);
+}
+
+SpaceSplitStringData::SpaceSplitStringData(const SpaceSplitStringData& other)
+    : RefCounted<SpaceSplitStringData>()
+    , m_vector(other.m_vector)
+{
+    // Note that we don't copy m_keyString to indicate to the destructor that there's nothing
+    // to be removed from the sharedDataMap().
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/dom/SpaceSplitString.h (105185 => 105186)


--- trunk/Source/WebCore/dom/SpaceSplitString.h	2012-01-17 20:44:49 UTC (rev 105185)
+++ trunk/Source/WebCore/dom/SpaceSplitString.h	2012-01-17 20:57:23 UTC (rev 105186)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007, 2008, 2010, 2011 Apple Inc. All rights reserved.
+ * Copyright (C) 2007, 2008, 2010, 2011, 2012 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -21,21 +21,19 @@
 #ifndef SpaceSplitString_h
 #define SpaceSplitString_h
 
-#include <wtf/OwnPtr.h>
-#include <wtf/PassOwnPtr.h>
+#include <wtf/RefCounted.h>
 #include <wtf/Vector.h>
 #include <wtf/text/AtomicString.h>
 
 namespace WebCore {
 
-    class SpaceSplitStringData {
-        WTF_MAKE_NONCOPYABLE(SpaceSplitStringData); WTF_MAKE_FAST_ALLOCATED;
+    class SpaceSplitStringData : public RefCounted<SpaceSplitStringData> {
     public:
-        SpaceSplitStringData(const String& string, bool shouldFoldCase)
-        {
-            createVector(string, shouldFoldCase);
-        }
+        static PassRefPtr<SpaceSplitStringData> create(const AtomicString&);
+        static PassRefPtr<SpaceSplitStringData> createUnique(const SpaceSplitStringData&);
 
+        ~SpaceSplitStringData();
+
         bool contains(const AtomicString& string)
         {
             size_t size = m_vector.size();
@@ -55,17 +53,21 @@
         const AtomicString& operator[](size_t i) { ASSERT(i < size()); return m_vector[i]; }
 
     private:
-        void createVector(const String&, bool shouldFoldCase);
+        SpaceSplitStringData(const AtomicString&);
+        SpaceSplitStringData(const SpaceSplitStringData&);
 
-        Vector<AtomicString, 2> m_vector;
+        void createVector(const String&);
+
+        AtomicString m_keyString;
+        Vector<AtomicString, 4> m_vector;
     };
 
     class SpaceSplitString {
     public:
         SpaceSplitString() { }
-        SpaceSplitString(const String& string, bool shouldFoldCase) : m_data(adoptPtr(new SpaceSplitStringData(string, shouldFoldCase))) { }
+        SpaceSplitString(const AtomicString& string, bool shouldFoldCase) { set(string, shouldFoldCase); }
 
-        void set(const String& string, bool shouldFoldCase) { m_data = adoptPtr(new SpaceSplitStringData(string, shouldFoldCase)); }
+        void set(const AtomicString&, bool shouldFoldCase);
         void clear() { m_data.clear(); }
 
         bool contains(const AtomicString& string) const { return m_data && m_data->contains(string); }
@@ -78,7 +80,13 @@
         const AtomicString& operator[](size_t i) const { ASSERT(i < size()); return (*m_data)[i]; }
 
     private:
-        OwnPtr<SpaceSplitStringData> m_data;
+        void ensureUnique()
+        {
+            if (m_data && !m_data->hasOneRef())
+                m_data = SpaceSplitStringData::createUnique(*m_data);
+        }
+
+        RefPtr<SpaceSplitStringData> m_data;
     };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to