Title: [185078] trunk/Source/WebCore
Revision
185078
Author
achristen...@apple.com
Date
2015-06-01 15:29:37 -0700 (Mon, 01 Jun 2015)

Log Message

[Content Extensions] Reduce DFA memory usage.
https://bugs.webkit.org/show_bug.cgi?id=145526

Reviewed by Benjamin Poulain.

* contentextensions/DFA.cpp:
(WebCore::ContentExtensions::DFA::memoryUsed):
(WebCore::ContentExtensions::DFANode::transitions):
(WebCore::ContentExtensions::DFANode::fallbackTransitionDestination):
(WebCore::ContentExtensions::DFANode::changeFallbackTransition):
(WebCore::ContentExtensions::DFANode::addFallbackTransition):
(WebCore::ContentExtensions::DFANode::containsTransition):
(WebCore::ContentExtensions::DFANode::kill):
* contentextensions/DFA.h:
* contentextensions/DFAMinimizer.cpp:
(WebCore::ContentExtensions::DFAMinimizer::minimize):
* contentextensions/NFAToDFA.cpp:
(WebCore::ContentExtensions::NFAToDFA::convert):
Use separate Vectors for the transition characters and destinations to avoid wasting memory to padding a std::pair.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (185077 => 185078)


--- trunk/Source/WebCore/ChangeLog	2015-06-01 22:26:20 UTC (rev 185077)
+++ trunk/Source/WebCore/ChangeLog	2015-06-01 22:29:37 UTC (rev 185078)
@@ -1,3 +1,25 @@
+2015-06-01  Alex Christensen  <achristen...@webkit.org>
+
+        [Content Extensions] Reduce DFA memory usage.
+        https://bugs.webkit.org/show_bug.cgi?id=145526
+
+        Reviewed by Benjamin Poulain.
+
+        * contentextensions/DFA.cpp:
+        (WebCore::ContentExtensions::DFA::memoryUsed):
+        (WebCore::ContentExtensions::DFANode::transitions):
+        (WebCore::ContentExtensions::DFANode::fallbackTransitionDestination):
+        (WebCore::ContentExtensions::DFANode::changeFallbackTransition):
+        (WebCore::ContentExtensions::DFANode::addFallbackTransition):
+        (WebCore::ContentExtensions::DFANode::containsTransition):
+        (WebCore::ContentExtensions::DFANode::kill):
+        * contentextensions/DFA.h:
+        * contentextensions/DFAMinimizer.cpp:
+        (WebCore::ContentExtensions::DFAMinimizer::minimize):
+        * contentextensions/NFAToDFA.cpp:
+        (WebCore::ContentExtensions::NFAToDFA::convert):
+        Use separate Vectors for the transition characters and destinations to avoid wasting memory to padding a std::pair.
+
 2015-06-01  Matt Rajca  <mra...@apple.com>
 
         Implemented the `eventTargetInterface` and `scriptExecutionContext` methods required by EventTarget, as well as

Modified: trunk/Source/WebCore/contentextensions/DFA.cpp (185077 => 185078)


--- trunk/Source/WebCore/contentextensions/DFA.cpp	2015-06-01 22:26:20 UTC (rev 185077)
+++ trunk/Source/WebCore/contentextensions/DFA.cpp	2015-06-01 22:29:37 UTC (rev 185078)
@@ -38,9 +38,10 @@
 size_t DFA::memoryUsed() const
 {
     return sizeof(DFA)
-        + actions.size() * sizeof(uint64_t)
-        + transitions.size() * sizeof(std::pair<uint8_t, uint32_t>)
-        + nodes.size() * sizeof(DFANode);
+        + actions.capacity() * sizeof(uint64_t)
+        + transitionCharacters.capacity() * sizeof(uint8_t)
+        + transitionDestinations.capacity() * sizeof(uint32_t)
+        + nodes.capacity() * sizeof(DFANode);
 }
 
 // FIXME: Make DFANode.cpp.
@@ -60,7 +61,7 @@
     Vector<std::pair<uint8_t, uint32_t>> vector;
     vector.reserveInitialCapacity(transitionsLength());
     for (uint32_t i = m_transitionsStart; i < m_transitionsStart + m_transitionsLength; ++i)
-        vector.uncheckedAppend(dfa.transitions[i]);
+        vector.uncheckedAppend(std::make_pair(dfa.transitionCharacters[i], dfa.transitionDestinations[i]));
     return vector;
 }
 
@@ -69,21 +70,24 @@
     RELEASE_ASSERT(hasFallbackTransition());
 
     // If there is a fallback transition, it is just after the other transitions and has an invalid ASCII character to mark it as a fallback transition.
-    ASSERT(dfa.transitions[m_transitionsStart + m_transitionsLength].first == std::numeric_limits<uint8_t>::max());
-    return dfa.transitions[m_transitionsStart + m_transitionsLength].second;
+    ASSERT(dfa.transitionCharacters[m_transitionsStart + m_transitionsLength] == std::numeric_limits<uint8_t>::max());
+    return dfa.transitionDestinations[m_transitionsStart + m_transitionsLength];
 }
 
 void DFANode::changeFallbackTransition(DFA& dfa, uint32_t newDestination)
 {
     RELEASE_ASSERT(hasFallbackTransition());
-    ASSERT_WITH_MESSAGE(dfa.transitions[m_transitionsStart + m_transitionsLength].first == std::numeric_limits<uint8_t>::max(), "When changing a fallback transition, the fallback transition should already be marked as such");
-    dfa.transitions[m_transitionsStart + m_transitionsLength] = std::pair<uint8_t, uint32_t>(std::numeric_limits<uint8_t>::max(), newDestination);
+    ASSERT_WITH_MESSAGE(dfa.transitionCharacters[m_transitionsStart + m_transitionsLength] == std::numeric_limits<uint8_t>::max(), "When changing a fallback transition, the fallback transition should already be marked as such");
+    dfa.transitionCharacters[m_transitionsStart + m_transitionsLength] = std::numeric_limits<uint8_t>::max();
+    dfa.transitionDestinations[m_transitionsStart + m_transitionsLength] = newDestination;
 }
 
 void DFANode::addFallbackTransition(DFA& dfa, uint32_t destination)
 {
-    RELEASE_ASSERT_WITH_MESSAGE(dfa.transitions.size() == m_transitionsStart + m_transitionsLength, "Adding a fallback transition should only happen if the node is at the end");
-    dfa.transitions.append(std::pair<uint8_t, uint32_t>(std::numeric_limits<uint8_t>::max(), destination));
+    RELEASE_ASSERT(dfa.transitionCharacters.size() == dfa.transitionDestinations.size());
+    RELEASE_ASSERT_WITH_MESSAGE(dfa.transitionCharacters.size() == m_transitionsStart + m_transitionsLength, "Adding a fallback transition should only happen if the node is at the end");
+    dfa.transitionCharacters.append(std::numeric_limits<uint8_t>::max());
+    dfa.transitionDestinations.append(destination);
     ASSERT(!(m_flags & HasFallbackTransition));
     m_flags |= HasFallbackTransition;
 }
@@ -93,7 +97,7 @@
     // Called from DFAMinimizer, this loops though a maximum of 128 transitions, so it's not too slow.
     ASSERT(m_transitionsLength <= 128);
     for (unsigned i = m_transitionsStart; i < m_transitionsStart + m_transitionsLength; ++i) {
-        if (dfa.transitions[i].first == transition)
+        if (dfa.transitionCharacters[i] == transition)
             return true;
     }
     return false;
@@ -105,8 +109,10 @@
     m_flags = IsKilled; // Killed nodes don't have any other flags.
     
     // Invalidate the now-unused memory in the DFA to make finding bugs easier.
-    for (unsigned i = m_transitionsStart; i < m_transitionsStart + m_transitionsLength; ++i)
-        dfa.transitions[i] = std::make_pair(std::numeric_limits<uint8_t>::max(), std::numeric_limits<uint32_t>::max());
+    for (unsigned i = m_transitionsStart; i < m_transitionsStart + m_transitionsLength; ++i) {
+        dfa.transitionCharacters[i] = std::numeric_limits<uint8_t>::max();
+        dfa.transitionDestinations[i] = std::numeric_limits<uint32_t>::max();
+    }
     for (unsigned i = m_actionsStart; i < m_actionsStart + m_actionsLength; ++i)
         dfa.actions[i] = std::numeric_limits<uint64_t>::max();
 

Modified: trunk/Source/WebCore/contentextensions/DFA.h (185077 => 185078)


--- trunk/Source/WebCore/contentextensions/DFA.h	2015-06-01 22:26:20 UTC (rev 185077)
+++ trunk/Source/WebCore/contentextensions/DFA.h	2015-06-01 22:29:37 UTC (rev 185078)
@@ -46,8 +46,8 @@
 #endif
     
     Vector<uint64_t> actions;
-    // FIXME: transitions could be two Vectors to save even more memory.
-    Vector<std::pair<uint8_t, uint32_t>> transitions;
+    Vector<uint8_t> transitionCharacters;
+    Vector<uint32_t> transitionDestinations;
     Vector<DFANode> nodes;
     unsigned root { 0 };
 };

Modified: trunk/Source/WebCore/contentextensions/DFAMinimizer.cpp (185077 => 185078)


--- trunk/Source/WebCore/contentextensions/DFAMinimizer.cpp	2015-06-01 22:26:20 UTC (rev 185077)
+++ trunk/Source/WebCore/contentextensions/DFAMinimizer.cpp	2015-06-01 22:29:37 UTC (rev 185078)
@@ -100,14 +100,19 @@
         
             unsigned firstSlot = dfaNode.transitionsStart();
             dfaNode.resetTransitions(firstSlot, transitions.size());
-            for (unsigned i = 0; i < transitions.size(); ++i)
-                dfa.transitions[firstSlot + i] = transitions[i];
+            for (unsigned i = 0; i < transitions.size(); ++i) {
+                dfa.transitionCharacters[firstSlot + i] = transitions[i].first;
+                dfa.transitionDestinations[firstSlot + i] = transitions[i].second;
+            }
             for (unsigned i = transitions.size(); i < availableSlotCount; ++i) {
                 // Invalidate now-unused memory to make finding bugs easier.
-                dfa.transitions[firstSlot + i] = std::make_pair(std::numeric_limits<uint8_t>::max(), std::numeric_limits<uint32_t>::max());
+                dfa.transitionCharacters[firstSlot + i] = std::numeric_limits<uint8_t>::max();
+                dfa.transitionDestinations[firstSlot + i] = std::numeric_limits<uint32_t>::max();
             }
-            if (willHaveFallback)
-                dfa.transitions[firstSlot + transitions.size()] = std::make_pair(std::numeric_limits<uint8_t>::max(), newFallbackDestination);
+            if (willHaveFallback) {
+                dfa.transitionCharacters[firstSlot + transitions.size()] = std::numeric_limits<uint8_t>::max();
+                dfa.transitionDestinations[firstSlot + transitions.size()] = newFallbackDestination;
+            }
         }
     }
 }
@@ -541,7 +546,7 @@
     for (DFANode& node : dfa.nodes) {
         auto nodeTransitions = node.transitions(dfa);
         for (unsigned i = 0; i < node.transitionsLength(); ++i)
-            dfa.transitions[node.transitionsStart() + i].second = relocationVector[nodeTransitions[i].second];
+            dfa.transitionDestinations[node.transitionsStart() + i] = relocationVector[nodeTransitions[i].second];
         if (node.hasFallbackTransition())
             node.changeFallbackTransition(dfa, relocationVector[node.fallbackTransitionDestination(dfa)]);
     }

Modified: trunk/Source/WebCore/contentextensions/NFAToDFA.cpp (185077 => 185078)


--- trunk/Source/WebCore/contentextensions/NFAToDFA.cpp	2015-06-01 22:26:20 UTC (rev 185077)
+++ trunk/Source/WebCore/contentextensions/NFAToDFA.cpp	2015-06-01 22:29:37 UTC (rev 185078)
@@ -373,7 +373,8 @@
         NodeIdSet setFallbackTransition;
         populateTransitions(transitionsFromClosedSet, setFallbackTransition, *uniqueNodeIdSetImpl, nfaGraph, nfaNodeClosures);
 
-        unsigned transitionsStart = dfa.transitions.size();
+        unsigned transitionsStart = dfa.transitionCharacters.size();
+        ASSERT(dfa.transitionCharacters.size() == dfa.transitionDestinations.size());
         for (unsigned key = 0; key < transitionsFromClosedSet.size(); ++key) {
             NodeIdSet& targetNodeSet = transitionsFromClosedSet[key];
 
@@ -382,11 +383,13 @@
 
             unsigned targetNodeId = getOrCreateDFANode(targetNodeSet, nfaGraph, dfa, uniqueNodeIdSetTable, unprocessedNodes);
             RELEASE_ASSERT(key <= 127);
-            dfa.transitions.append(std::make_pair(static_cast<uint8_t>(key), targetNodeId));
+            dfa.transitionCharacters.append(static_cast<uint8_t>(key));
+            dfa.transitionDestinations.append(targetNodeId);
 
             targetNodeSet.clear();
         }
-        unsigned transitionsEnd = dfa.transitions.size();
+        unsigned transitionsEnd = dfa.transitionCharacters.size();
+        ASSERT(dfa.transitionCharacters.size() == dfa.transitionDestinations.size());
         unsigned transitionsLength = transitionsEnd - transitionsStart;
         RELEASE_ASSERT(transitionsLength <= 127);
         dfa.nodes[dfaNodeId].setTransitions(transitionsStart, static_cast<uint8_t>(transitionsLength));
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to