Bug 1292301 - reduce reference-counting in Places; r=adw
authorNathan Froyd <froydnj@gmail.com>
Fri, 05 Aug 2016 22:03:44 -0400
changeset 397521 5a915c1da4ebb8cb00fdc63c685bd0f8e9854044
parent 397520 8ee6f2eb8b349a351f6f76585eb85e0ec2b5c475
child 397522 8a63d6398481690023698a5790be5b185b846a14
push id25332
push usermaglione.k@gmail.com
push dateSat, 06 Aug 2016 21:21:51 +0000
reviewersadw
bugs1292301
milestone51.0a1
Bug 1292301 - reduce reference-counting in Places; r=adw By using nsCOMArray::Forget, we can pass the references we hold in an nsCOMArray directly to the XPCOM caller, without having to go through an extra cycle of AddRef/Release calls. This way should be slightly faster and it also results in smaller code. Notice that we don't have to worry about removing the OOM check in the places that use Forget(), because we were already using moz_xmalloc to allocate the outparam array, and moz_xmalloc aborts the process on OOM, just as Forget() does. The nsNavHistoryFolderResultNode::GetQueries change is thrown in for fun.
toolkit/components/places/nsAnnotationService.cpp
toolkit/components/places/nsNavBookmarks.cpp
toolkit/components/places/nsNavHistory.cpp
toolkit/components/places/nsNavHistoryResult.cpp
--- a/toolkit/components/places/nsAnnotationService.cpp
+++ b/toolkit/components/places/nsAnnotationService.cpp
@@ -972,25 +972,18 @@ nsAnnotationService::GetPagesWithAnnotat
 
   nsresult rv = GetPagesWithAnnotationCOMArray(aName, &results);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Convert to raw array.
   if (results.Count() == 0)
     return NS_OK;
 
-  *_results = static_cast<nsIURI**>
-                         (moz_xmalloc(results.Count() * sizeof(nsIURI*)));
-  NS_ENSURE_TRUE(*_results, NS_ERROR_OUT_OF_MEMORY);
-
   *_resultCount = results.Count();
-  for (uint32_t i = 0; i < *_resultCount; i ++) {
-    (*_results)[i] = results[i];
-    NS_ADDREF((*_results)[i]);
-  }
+  results.Forget(_results);
 
   return NS_OK;
 }
 
 
 nsresult
 nsAnnotationService::GetPagesWithAnnotationCOMArray(const nsACString& aName,
                                                     nsCOMArray<nsIURI>* _results)
@@ -1150,24 +1143,18 @@ nsAnnotationService::GetAnnotationsWithN
                                                              aName, variant);
     NS_ENSURE_TRUE(annotations.AppendObject(anno), NS_ERROR_OUT_OF_MEMORY);
   }
 
   // Convert to raw array.
   if (annotations.Count() == 0)
     return NS_OK;
 
-  *_annotations = static_cast<mozIAnnotatedResult**>
-    (moz_xmalloc(annotations.Count() * sizeof(mozIAnnotatedResult*)));
-  NS_ENSURE_TRUE(*_annotations, NS_ERROR_OUT_OF_MEMORY);
-
   *_count = annotations.Count();
-  for (uint32_t i = 0; i < *_count; ++i) {
-    NS_ADDREF((*_annotations)[i] = annotations[i]);
-  }
+  annotations.Forget(_annotations);
 
   return NS_OK;
 }
 
 
 nsresult
 nsAnnotationService::GetItemsWithAnnotationTArray(const nsACString& aName,
                                                   nsTArray<int64_t>* _results)
--- a/toolkit/components/places/nsNavBookmarks.cpp
+++ b/toolkit/components/places/nsNavBookmarks.cpp
@@ -2560,24 +2560,18 @@ nsNavBookmarks::GetObservers(uint32_t* _
     // Skip nullified weak observers.
     if (observer)
       observers.AppendElement(observer);
   }
 
   if (observers.Count() == 0)
     return NS_OK;
 
-  *_observers = static_cast<nsINavBookmarkObserver**>
-    (moz_xmalloc(observers.Count() * sizeof(nsINavBookmarkObserver*)));
-  NS_ENSURE_TRUE(*_observers, NS_ERROR_OUT_OF_MEMORY);
-
   *_count = observers.Count();
-  for (uint32_t i = 0; i < *_count; ++i) {
-    NS_ADDREF((*_observers)[i] = observers[i]);
-  }
+  observers.Forget(_observers);
 
   return NS_OK;
 }
 
 void
 nsNavBookmarks::NotifyItemVisited(const ItemVisitData& aData)
 {
   nsCOMPtr<nsIURI> uri;
--- a/toolkit/components/places/nsNavHistory.cpp
+++ b/toolkit/components/places/nsNavHistory.cpp
@@ -2308,24 +2308,18 @@ nsNavHistory::GetObservers(uint32_t* _co
     // Skip nullified weak observers.
     if (observer)
       observers.AppendElement(observer);
   }
 
   if (observers.Count() == 0)
     return NS_OK;
 
-  *_observers = static_cast<nsINavHistoryObserver**>
-    (moz_xmalloc(observers.Count() * sizeof(nsINavHistoryObserver*)));
-  NS_ENSURE_TRUE(*_observers, NS_ERROR_OUT_OF_MEMORY);
-
   *_count = observers.Count();
-  for (uint32_t i = 0; i < *_count; ++i) {
-    NS_ADDREF((*_observers)[i] = observers[i]);
-  }
+  observers.Forget(_observers);
 
   return NS_OK;
 }
 
 // See RunInBatchMode
 nsresult
 nsNavHistory::BeginUpdateBatch()
 {
--- a/toolkit/components/places/nsNavHistoryResult.cpp
+++ b/toolkit/components/places/nsNavHistoryResult.cpp
@@ -3161,17 +3161,17 @@ nsNavHistoryFolderResultNode::GetQueries
   rv = query->SetFolders(&mTargetFolderItemId, 1);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // make array of our 1 query
   *queries = static_cast<nsINavHistoryQuery**>
                         (moz_xmalloc(sizeof(nsINavHistoryQuery*)));
   if (!*queries)
     return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF((*queries)[0] = query);
+  (*queries)[0] = query.forget().take();
   *queryCount = 1;
   return NS_OK;
 }
 
 
 /**
  * Options for the query that gives you this bookmarks folder.  This is just
  * the options for the folder with the current folder ID set.