Bug 1361747 - Avoid a double hashtable lookup for insertions in nsFrameMessageManager::AddMessageListener and nsFrameMessageManager::AddWeakMessageListener; r=mystor
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Wed, 03 May 2017 11:27:11 -0400
changeset 357102 5fa6bc617e2f81c82fa58e0e64f6b2352a9adc0d
parent 357101 00b0dbfee0d6ce3f0533e238d4dc4f9920cf8540
child 357103 e9adf0e056f76898658532ead3ef84965e8d5de3
push id31782
push userkwierso@gmail.com
push dateMon, 08 May 2017 23:07:35 +0000
treeherdermozilla-central@b21b974d60d3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmystor
bugs1361747
milestone55.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 1361747 - Avoid a double hashtable lookup for insertions in nsFrameMessageManager::AddMessageListener and nsFrameMessageManager::AddWeakMessageListener; r=mystor
dom/base/nsFrameMessageManager.cpp
--- a/dom/base/nsFrameMessageManager.cpp
+++ b/dom/base/nsFrameMessageManager.cpp
@@ -251,27 +251,23 @@ SameProcessCpowHolder::ToObject(JSContex
 
 // nsIMessageListenerManager
 
 NS_IMETHODIMP
 nsFrameMessageManager::AddMessageListener(const nsAString& aMessage,
                                           nsIMessageListener* aListener,
                                           bool aListenWhenClosed)
 {
-  nsAutoTObserverArray<nsMessageListenerInfo, 1>* listeners =
-    mListeners.Get(aMessage);
-  if (!listeners) {
-    listeners = new nsAutoTObserverArray<nsMessageListenerInfo, 1>();
-    mListeners.Put(aMessage, listeners);
-  } else {
-    uint32_t len = listeners->Length();
-    for (uint32_t i = 0; i < len; ++i) {
-      if (listeners->ElementAt(i).mStrongListener == aListener) {
-        return NS_OK;
-      }
+  auto listeners = mListeners.LookupForAdd(aMessage).OrInsert([]() {
+      return new nsAutoTObserverArray<nsMessageListenerInfo, 1>();
+    });
+  uint32_t len = listeners->Length();
+  for (uint32_t i = 0; i < len; ++i) {
+    if (listeners->ElementAt(i).mStrongListener == aListener) {
+      return NS_OK;
     }
   }
 
   nsMessageListenerInfo* entry = listeners->AppendElement();
   NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
   entry->mStrongListener = aListener;
   entry->mListenWhenClosed = aListenWhenClosed;
   return NS_OK;
@@ -318,27 +314,23 @@ nsFrameMessageManager::AddWeakMessageLis
       if (weakListener) {
         nsCOMPtr<nsISupports> otherCanonical = do_QueryReferent(weakListener);
         MOZ_ASSERT((canonical == otherCanonical) == (weak == weakListener));
       }
     }
   }
 #endif
 
-  nsAutoTObserverArray<nsMessageListenerInfo, 1>* listeners =
-    mListeners.Get(aMessage);
-  if (!listeners) {
-    listeners = new nsAutoTObserverArray<nsMessageListenerInfo, 1>();
-    mListeners.Put(aMessage, listeners);
-  } else {
-    uint32_t len = listeners->Length();
-    for (uint32_t i = 0; i < len; ++i) {
-      if (listeners->ElementAt(i).mWeakListener == weak) {
-        return NS_OK;
-      }
+  auto listeners = mListeners.LookupForAdd(aMessage).OrInsert([]() {
+      return new nsAutoTObserverArray<nsMessageListenerInfo, 1>();
+    });
+  uint32_t len = listeners->Length();
+  for (uint32_t i = 0; i < len; ++i) {
+    if (listeners->ElementAt(i).mWeakListener == weak) {
+      return NS_OK;
     }
   }
 
   nsMessageListenerInfo* entry = listeners->AppendElement();
   entry->mWeakListener = weak;
   entry->mListenWhenClosed = false;
   return NS_OK;
 }