Bug 795128: Add memory reporting for attribute maps and nodes. r=smaug
authorKyle Huey <khuey@kylehuey.com>
Sun, 30 Sep 2012 09:43:47 -0700
changeset 108806 96f3db193c755b22956b50155b1ad36250df34db
parent 108805 72e482dbd38460db058c95827a3e4e6857986fa4
child 108807 a9c4d0c92df1ab7cbe63193384ddbea24526795b
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewerssmaug
bugs795128
milestone18.0a1
Bug 795128: Add memory reporting for attribute maps and nodes. r=smaug
content/base/public/FragmentOrElement.h
content/base/src/FragmentOrElement.cpp
content/base/src/nsDOMAttributeMap.cpp
content/base/src/nsDOMAttributeMap.h
--- a/content/base/public/FragmentOrElement.h
+++ b/content/base/public/FragmentOrElement.h
@@ -343,16 +343,18 @@ public:
   {
   public:
     nsDOMSlots();
     virtual ~nsDOMSlots();
 
     void Traverse(nsCycleCollectionTraversalCallback &cb, bool aIsXUL);
     void Unlink(bool aIsXUL);
 
+    size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
+
     /**
      * The .style attribute (an interface that forwards to the actual
      * style rules)
      * @see nsGenericHTMLElement::GetStyle
      */
     nsCOMPtr<nsICSSDeclaration> mStyle;
 
     /**
--- a/content/base/src/FragmentOrElement.cpp
+++ b/content/base/src/FragmentOrElement.cpp
@@ -593,16 +593,40 @@ FragmentOrElement::nsDOMSlots::Unlink(bo
     NS_IF_RELEASE(mControllers);
   mChildrenList = nullptr;
   if (mClassList) {
     mClassList->DropReference();
     mClassList = nullptr;
   }
 }
 
+size_t
+FragmentOrElement::nsDOMSlots::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
+{
+  size_t n = aMallocSizeOf(this);
+
+  if (mAttributeMap) {
+    n += mAttributeMap->SizeOfIncludingThis(aMallocSizeOf);
+  }
+
+  // Measurement of the following members may be added later if DMD finds it is
+  // worthwhile:
+  // - Superclass members (nsINode::nsSlots)
+  // - mStyle
+  // - mDataSet
+  // - mSMILOverrideStyle
+  // - mSMILOverrideStyleRule
+  // - mChildrenList
+  // - mClassList
+
+  // The following members are not measured:
+  // - mBindingParent / mControllers: because they're   non-owning
+  return n;
+}
+
 FragmentOrElement::FragmentOrElement(already_AddRefed<nsINodeInfo> aNodeInfo)
   : nsIContent(aNodeInfo)
 {
 }
 
 FragmentOrElement::~FragmentOrElement()
 {
   NS_PRECONDITION(!IsInDoc(),
@@ -1933,11 +1957,19 @@ FragmentOrElement::FireNodeRemovedForChi
        child = child->GetNextSibling()) {
     nsContentUtils::MaybeFireNodeRemoved(child, this, doc);
   }
 }
 
 size_t
 FragmentOrElement::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
 {
-  return nsIContent::SizeOfExcludingThis(aMallocSizeOf) +
-         mAttrsAndChildren.SizeOfExcludingThis(aMallocSizeOf);
+  size_t n = 0;
+  n += nsIContent::SizeOfExcludingThis(aMallocSizeOf);
+  n += mAttrsAndChildren.SizeOfExcludingThis(aMallocSizeOf);
+
+  nsDOMSlots* slots = GetExistingDOMSlots();
+  if (slots) {
+    n += slots->SizeOfIncludingThis(aMallocSizeOf);
+  }
+
+  return n;
 }
--- a/content/base/src/nsDOMAttributeMap.cpp
+++ b/content/base/src/nsDOMAttributeMap.cpp
@@ -496,8 +496,28 @@ nsDOMAttributeMap::Count() const
 }
 
 uint32_t
 nsDOMAttributeMap::Enumerate(AttrCache::EnumReadFunction aFunc,
                              void *aUserArg) const
 {
   return mAttributeCache.EnumerateRead(aFunc, aUserArg);
 }
+
+size_t
+AttrCacheSizeEnumerator(const nsAttrKey& aKey,
+                        const nsRefPtr<nsDOMAttribute>& aValue,
+                        nsMallocSizeOfFun aMallocSizeOf,
+                        void* aUserArg)
+{
+  return aMallocSizeOf(aValue.get());
+}
+
+size_t
+nsDOMAttributeMap::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
+{
+  size_t n = aMallocSizeOf(this);
+  n += mAttributeCache.SizeOfExcludingThis(AttrCacheSizeEnumerator,
+                                           aMallocSizeOf);
+
+  // NB: mContent is non-owning and thus not counted.
+  return n;
+}
--- a/content/base/src/nsDOMAttributeMap.h
+++ b/content/base/src/nsDOMAttributeMap.h
@@ -154,16 +154,18 @@ public:
     }
 #endif
 
     return static_cast<nsDOMAttributeMap*>(aSupports);
   }
 
   NS_DECL_CYCLE_COLLECTION_CLASS(nsDOMAttributeMap)
 
+  size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
+
 private:
   Element *mContent; // Weak reference
 
   /**
    * Cache of nsDOMAttributes.
    */
   AttrCache mAttributeCache;