Bug 1372268 - Use LookupForAdd instead of Get+Put to avoid unnecessary hashtable lookups. r=froydnj
authorMats Palmgren <mats@mozilla.com>
Wed, 14 Jun 2017 16:11:22 +0200
changeset 364038 5e9e085bf790aeb9ad773ea3d457dbc23bf502db
parent 364037 0a6455bd6b7331c0c1bcdccd731f5baaf72dda8b
child 364039 fccb81aa0a57b6846820eee3e1c9fa6fb50ffc58
push id32030
push userarchaeopteryx@coole-files.de
push dateThu, 15 Jun 2017 09:12:02 +0000
treeherdermozilla-central@035c25bef7b5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1372268
milestone56.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 1372268 - Use LookupForAdd instead of Get+Put to avoid unnecessary hashtable lookups. r=froydnj MozReview-Commit-ID: 1WVLTschGsj
dom/commandhandler/nsCommandManager.cpp
--- a/dom/commandhandler/nsCommandManager.cpp
+++ b/dom/commandhandler/nsCommandManager.cpp
@@ -97,21 +97,19 @@ NS_IMETHODIMP
 nsCommandManager::AddCommandObserver(nsIObserver* aCommandObserver,
                                      const char* aCommandToObserve)
 {
   NS_ENSURE_ARG(aCommandObserver);
 
   // XXX todo: handle special cases of aCommandToObserve being null, or empty
 
   // for each command in the table, we make a list of observers for that command
-  ObserverList* commandObservers;
-  if (!mObserversTable.Get(aCommandToObserve, &commandObservers)) {
-    commandObservers = new ObserverList;
-    mObserversTable.Put(aCommandToObserve, commandObservers);
-  }
+  ObserverList* commandObservers =
+    mObserversTable.LookupForAdd(aCommandToObserve).OrInsert(
+      [] () { return new ObserverList; });
 
   // need to check that this command observer hasn't already been registered
   int32_t existingIndex = commandObservers->IndexOf(aCommandObserver);
   if (existingIndex == -1) {
     commandObservers->AppendElement(aCommandObserver);
   } else {
     NS_WARNING("Registering command observer twice on the same command");
   }