From 68e9c4e7cda8607f395a75781809fabbcc2bbeb8 Mon Sep 17 00:00:00 2001 From: Kulanthaivel Palanichamy Date: Tue, 27 Nov 2012 11:50:21 -0800 Subject: [WebKit] Fix for the memory leak Memory leak was introduced by "DOM traversal optimizations DOM Core optimizations Prefetch optimization for DOM Tree Traversal" Cachelist was being mismanaged. Removing it. Change-Id: I8abb2b6b30b1fbeb931a19be3567c78c3f697d77 --- Source/WebCore/dom/ChildNodeList.cpp | 17 +++-------------- Source/WebCore/dom/ChildNodeList.h | 8 +++----- Source/WebCore/dom/Node.cpp | 21 +++------------------ Source/WebCore/dom/Node.h | 1 - Source/WebCore/dom/NodeRareData.h | 6 ++---- 5 files changed, 11 insertions(+), 42 deletions(-) diff --git a/Source/WebCore/dom/ChildNodeList.cpp b/Source/WebCore/dom/ChildNodeList.cpp index 253537c..3328c7c 100644 --- a/Source/WebCore/dom/ChildNodeList.cpp +++ b/Source/WebCore/dom/ChildNodeList.cpp @@ -27,27 +27,19 @@ namespace WebCore { -ChildNodeList::ChildNodeList(PassRefPtr rootNode) - : DynamicNodeList(rootNode) +ChildNodeList::ChildNodeList(PassRefPtr rootNode, DynamicNodeList::Caches* info) + : DynamicNodeList(rootNode, info) { } -ChildNodeList::~ChildNodeList() -{ - m_rootNode->removeCachedChildNodeList(this); -} - unsigned ChildNodeList::length() const { if (m_caches->isLengthCacheValid) return m_caches->cachedLength; unsigned len = 0; - Vector& cachedNodes = m_caches->cachedNodes; - for (Node* n = m_rootNode->firstChild(); n; n = n->nextSibling()) { - cachedNodes.append(n); + for (Node* n = m_rootNode->firstChild(); n; n = n->nextSibling()) len++; - } m_caches->cachedLength = len; m_caches->isLengthCacheValid = true; @@ -57,9 +49,6 @@ unsigned ChildNodeList::length() const Node* ChildNodeList::item(unsigned index) const { - if (m_caches->isLengthCacheValid && index < m_caches->cachedLength) - return m_caches->cachedNodes[index]; - unsigned int pos = 0; Node* n = m_rootNode->firstChild(); diff --git a/Source/WebCore/dom/ChildNodeList.h b/Source/WebCore/dom/ChildNodeList.h index df3d67b..f38106d 100644 --- a/Source/WebCore/dom/ChildNodeList.h +++ b/Source/WebCore/dom/ChildNodeList.h @@ -31,18 +31,16 @@ namespace WebCore { class ChildNodeList : public DynamicNodeList { public: - static PassRefPtr create(PassRefPtr rootNode) + static PassRefPtr create(PassRefPtr rootNode, Caches* caches) { - return adoptRef(new ChildNodeList(rootNode)); + return adoptRef(new ChildNodeList(rootNode, caches)); } - virtual ~ChildNodeList(); - virtual unsigned length() const; virtual Node* item(unsigned index) const; protected: - ChildNodeList(PassRefPtr rootNode); + ChildNodeList(PassRefPtr rootNode, Caches*); virtual bool nodeMatches(Element*) const; }; diff --git a/Source/WebCore/dom/Node.cpp b/Source/WebCore/dom/Node.cpp index 5b173bf..facb694 100644 --- a/Source/WebCore/dom/Node.cpp +++ b/Source/WebCore/dom/Node.cpp @@ -628,12 +628,8 @@ void Node::setNodeValue(const String& /*nodeValue*/, ExceptionCode& ec) PassRefPtr Node::childNodes() { NodeListsNodeData* data = ensureRareData()->ensureNodeLists(this); - if (data->m_childNodeListCache) - return PassRefPtr(data->m_childNodeListCache); - RefPtr childNodeList = ChildNodeList::create(this); - data->m_childNodeListCache = childNodeList.get(); - return childNodeList.release(); + return ChildNodeList::create(this, data->m_childNodeListCaches.get()); } Node *Node::lastDescendant() const @@ -1149,16 +1145,6 @@ void Node::removeCachedLabelsNodeList(DynamicNodeList* list) data->m_labelsNodeListCache = 0; } -void Node::removeCachedChildNodeList(DynamicNodeList* list) -{ - ASSERT(rareData()); - ASSERT(rareData()->nodeLists()); - ASSERT_UNUSED(list, list->hasOwnCaches()); - - NodeListsNodeData* data = rareData()->nodeLists(); - data->m_childNodeListCache = 0; -} - Node* Node::traverseNextNode(const Node* stayWithin) const { prefetchTarget(); @@ -2504,8 +2490,7 @@ void Node::formatForDebugger(char* buffer, unsigned length) const void NodeListsNodeData::invalidateCaches() { - if (m_childNodeListCache) - m_childNodeListCache->invalidateCache(); + m_childNodeListCaches->reset(); if (m_labelsNodeListCache) m_labelsNodeListCache->invalidateCache(); @@ -2536,7 +2521,7 @@ bool NodeListsNodeData::isEmpty() const if (!m_listsWithCaches.isEmpty()) return false; - if (m_childNodeListCache) + if (m_childNodeListCaches->refCount()) return false; TagNodeListCacheNS::const_iterator tagCacheEndNS = m_tagNodeListCacheNS.end(); diff --git a/Source/WebCore/dom/Node.h b/Source/WebCore/dom/Node.h index ad5ee9d..d75d064 100644 --- a/Source/WebCore/dom/Node.h +++ b/Source/WebCore/dom/Node.h @@ -563,7 +563,6 @@ public: void removeCachedTagNodeList(TagNodeList*, const AtomicString&); void removeCachedTagNodeListNS(TagNodeListNS*, const QualifiedName&); void removeCachedLabelsNodeList(DynamicNodeList*); - void removeCachedChildNodeList(DynamicNodeList*); PassRefPtr getElementsByTagName(const AtomicString&); PassRefPtr getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName); diff --git a/Source/WebCore/dom/NodeRareData.h b/Source/WebCore/dom/NodeRareData.h index b81dd3f..7bbd0c1 100644 --- a/Source/WebCore/dom/NodeRareData.h +++ b/Source/WebCore/dom/NodeRareData.h @@ -22,7 +22,6 @@ #ifndef NodeRareData_h #define NodeRareData_h -#include "ChildNodeList.h" #include "ClassNodeList.h" #include "DynamicNodeList.h" #include "NameNodeList.h" @@ -43,7 +42,7 @@ public: typedef HashSet NodeListSet; NodeListSet m_listsWithCaches; - RefPtr m_childNodeListCache; + RefPtr m_childNodeListCaches; typedef HashMap ClassNodeListCache; ClassNodeListCache m_classNodeListCache; @@ -70,8 +69,7 @@ public: private: NodeListsNodeData() - : m_childNodeListCache(0) - , m_labelsNodeListCache(0) + : m_childNodeListCaches(DynamicNodeList::Caches::create()), m_labelsNodeListCache(0) { } }; -- cgit v1.1