Bug 1187152 (part 3) - Replace nsBaseHashtable::Enumerate() calls in modules/ with iterators. r=froydnj.
authorNicholas Nethercote <nnethercote@mozilla.com>
Sun, 22 Nov 2015 18:52:40 -0800
changeset 308030 0aec4fd3842fcfc8d8626b6d715fae7168b31ab0
parent 308029 ce754612fecfc340b6de8c30aa6c7b944c37f747
child 308031 1c0c87d8cd2be1f8e9f67534d6f67496b64866c5
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1187152
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1187152 (part 3) - Replace nsBaseHashtable::Enumerate() calls in modules/ with iterators. r=froydnj.
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -256,70 +256,20 @@ class PreferenceServiceReporter final : 
   ~PreferenceServiceReporter() {}
 
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIMEMORYREPORTER
 
 protected:
   static const uint32_t kSuspectReferentCount = 1000;
-  static PLDHashOperator CountReferents(PrefCallback* aKey,
-                                        nsAutoPtr<PrefCallback>& aCallback,
-                                        void* aClosure);
 };
 
 NS_IMPL_ISUPPORTS(PreferenceServiceReporter, nsIMemoryReporter)
 
-struct PreferencesReferentCount {
-  PreferencesReferentCount() : numStrong(0), numWeakAlive(0), numWeakDead(0) {}
-  size_t numStrong;
-  size_t numWeakAlive;
-  size_t numWeakDead;
-  nsTArray<nsCString> suspectPreferences;
-  // Count of the number of referents for each preference.
-  nsDataHashtable<nsCStringHashKey, uint32_t> prefCounter;
-};
-
-PLDHashOperator
-PreferenceServiceReporter::CountReferents(PrefCallback* aKey,
-                                          nsAutoPtr<PrefCallback>& aCallback,
-                                          void* aClosure)
-{
-  PreferencesReferentCount* referentCount =
-    static_cast<PreferencesReferentCount*>(aClosure);
-
-  nsPrefBranch* prefBranch = aCallback->GetPrefBranch();
-  const char* pref = prefBranch->getPrefName(aCallback->GetDomain().get());
-
-  if (aCallback->IsWeak()) {
-    nsCOMPtr<nsIObserver> callbackRef = do_QueryReferent(aCallback->mWeakRef);
-    if (callbackRef) {
-      referentCount->numWeakAlive++;
-    } else {
-      referentCount->numWeakDead++;
-    }
-  } else {
-    referentCount->numStrong++;
-  }
-
-  nsDependentCString prefString(pref);
-  uint32_t oldCount = 0;
-  referentCount->prefCounter.Get(prefString, &oldCount);
-  uint32_t currentCount = oldCount + 1;
-  referentCount->prefCounter.Put(prefString, currentCount);
-
-  // Keep track of preferences that have a suspiciously large
-  // number of referents (symptom of leak).
-  if (currentCount == kSuspectReferentCount) {
-    referentCount->suspectPreferences.AppendElement(prefString);
-  }
-
-  return PL_DHASH_NEXT;
-}
-
 MOZ_DEFINE_MALLOC_SIZE_OF(PreferenceServiceMallocSizeOf)
 
 NS_IMETHODIMP
 PreferenceServiceReporter::CollectReports(nsIMemoryReporterCallback* aCb,
                                           nsISupports* aClosure,
                                           bool aAnonymize)
 {
 #define REPORT(_path, _kind, _units, _amount, _desc)                          \
@@ -337,44 +287,77 @@ PreferenceServiceReporter::CollectReport
          "Memory used by the preferences system.");
 
   nsPrefBranch* rootBranch =
     static_cast<nsPrefBranch*>(Preferences::GetRootBranch());
   if (!rootBranch) {
     return NS_OK;
   }
 
-  PreferencesReferentCount referentCount;
-  rootBranch->mObservers.Enumerate(&CountReferents, &referentCount);
+  size_t numStrong = 0;
+  size_t numWeakAlive = 0;
+  size_t numWeakDead = 0;
+  nsTArray<nsCString> suspectPreferences;
+  // Count of the number of referents for each preference.
+  nsDataHashtable<nsCStringHashKey, uint32_t> prefCounter;
+
+  for (auto iter = rootBranch->mObservers.Iter(); !iter.Done(); iter.Next()) {
+    nsAutoPtr<PrefCallback>& callback = iter.Data();
+    nsPrefBranch* prefBranch = callback->GetPrefBranch();
+    const char* pref = prefBranch->getPrefName(callback->GetDomain().get());
 
-  for (uint32_t i = 0; i < referentCount.suspectPreferences.Length(); i++) {
-    nsCString& suspect = referentCount.suspectPreferences[i];
+    if (callback->IsWeak()) {
+      nsCOMPtr<nsIObserver> callbackRef = do_QueryReferent(callback->mWeakRef);
+      if (callbackRef) {
+        numWeakAlive++;
+      } else {
+        numWeakDead++;
+      }
+    } else {
+      numStrong++;
+    }
+
+    nsDependentCString prefString(pref);
+    uint32_t oldCount = 0;
+    prefCounter.Get(prefString, &oldCount);
+    uint32_t currentCount = oldCount + 1;
+    prefCounter.Put(prefString, currentCount);
+
+    // Keep track of preferences that have a suspiciously large number of
+    // referents (a symptom of a leak).
+    if (currentCount == kSuspectReferentCount) {
+      suspectPreferences.AppendElement(prefString);
+    }
+  }
+
+  for (uint32_t i = 0; i < suspectPreferences.Length(); i++) {
+    nsCString& suspect = suspectPreferences[i];
     uint32_t totalReferentCount = 0;
-    referentCount.prefCounter.Get(suspect, &totalReferentCount);
+    prefCounter.Get(suspect, &totalReferentCount);
 
     nsPrintfCString suspectPath("preference-service-suspect/"
                                 "referent(pref=%s)", suspect.get());
 
     REPORT(suspectPath,
            KIND_OTHER, UNITS_COUNT, totalReferentCount,
            "A preference with a suspiciously large number "
            "referents (symptom of a leak).");
   }
 
   REPORT(NS_LITERAL_CSTRING("preference-service/referent/strong"),
-         KIND_OTHER, UNITS_COUNT, referentCount.numStrong,
+         KIND_OTHER, UNITS_COUNT, numStrong,
          "The number of strong referents held by the preference service.");
 
   REPORT(NS_LITERAL_CSTRING("preference-service/referent/weak/alive"),
-         KIND_OTHER, UNITS_COUNT, referentCount.numWeakAlive,
+         KIND_OTHER, UNITS_COUNT, numWeakAlive,
          "The number of weak referents held by the preference service "
          "that are still alive.");
 
   REPORT(NS_LITERAL_CSTRING("preference-service/referent/weak/dead"),
-         KIND_OTHER, UNITS_COUNT, referentCount.numWeakDead,
+         KIND_OTHER, UNITS_COUNT, numWeakDead,
          "The number of weak referents held by the preference service "
          "that are dead.");
 
 #undef REPORT
 
   return NS_OK;
 }