Bug 1371492 - Use AutoTArray to save current listener in NotifySelectionListeners. r=smaug
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 09 Jun 2017 13:15:49 +0900
changeset 411517 11e9775fd375003197c01c05ccd4091ba6dd0222
parent 411514 799d43edb324395cf02da6b028e803712334615f
child 411518 dae49146c17ca29cf317249630e142dbcac84946
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1371492
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 1371492 - Use AutoTArray to save current listener in NotifySelectionListeners. r=smaug Actually, we use nsCOMArray for mSelectionListeners. NotifySelectionListeners saves current listeners to another nsCOMArray then use it for observing. When I debug it, array length is mostly ~5, and nsCOMArray uses malloc according to profiler. So since array length is small, we should use AutoTArray instead. MozReview-Commit-ID: As5PS1KVTt8
layout/generic/Selection.h
layout/generic/nsSelection.cpp
--- a/layout/generic/Selection.h
+++ b/layout/generic/Selection.h
@@ -432,17 +432,17 @@ private:
   // proves to be a performance concern, then an interval tree may be a
   // possible solution, allowing the calculation of the overlap interval in
   // O(log n) time, though this would require rebalancing and other overhead.
   nsTArray<RangeData> mRanges;
 
   RefPtr<nsRange> mAnchorFocusRange;
   RefPtr<nsFrameSelection> mFrameSelection;
   RefPtr<nsAutoScrollTimer> mAutoScrollTimer;
-  nsCOMArray<nsISelectionListener> mSelectionListeners;
+  FallibleTArray<nsCOMPtr<nsISelectionListener>> mSelectionListeners;
   nsRevocableEventPtr<ScrollSelectionIntoViewEvent> mScrollEvent;
   CachedOffsetForFrame* mCachedOffsetForFrame;
   nsDirection mDirection;
   SelectionType mSelectionType;
   UniquePtr<SelectionCustomColors> mCustomColors;
 
   /**
    * True if the current selection operation was initiated by user action.
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -18,17 +18,16 @@
 #include "nsString.h"
 #include "nsFrameSelection.h"
 #include "nsISelectionListener.h"
 #include "nsContentCID.h"
 #include "nsDeviceContext.h"
 #include "nsIContent.h"
 #include "nsIDOMNode.h"
 #include "nsRange.h"
-#include "nsCOMArray.h"
 #include "nsITableCellLayout.h"
 #include "nsTArray.h"
 #include "nsTableWrapperFrame.h"
 #include "nsTableCellFrame.h"
 #include "nsIScrollableFrame.h"
 #include "nsCCUncollectableMarker.h"
 #include "nsIContentIterator.h"
 #include "nsIDocumentEncoder.h"
@@ -6360,17 +6359,17 @@ Selection::AddSelectionListener(nsISelec
   }
   return NS_OK;
 }
 
 void
 Selection::AddSelectionListener(nsISelectionListener* aNewListener,
                                 ErrorResult& aRv)
 {
-  bool result = mSelectionListeners.AppendObject(aNewListener); // AddRefs
+  bool result = mSelectionListeners.AppendElement(aNewListener, fallible); // AddRefs
   if (!result) {
     aRv.Throw(NS_ERROR_FAILURE);
   }
 }
 
 NS_IMETHODIMP
 Selection::RemoveSelectionListener(nsISelectionListener* aListenerToRemove)
 {
@@ -6383,17 +6382,17 @@ Selection::RemoveSelectionListener(nsISe
   }
   return NS_OK;
 }
 
 void
 Selection::RemoveSelectionListener(nsISelectionListener* aListenerToRemove,
                                    ErrorResult& aRv)
 {
-  bool result = mSelectionListeners.RemoveObject(aListenerToRemove); // Releases
+  bool result = mSelectionListeners.RemoveElement(aListenerToRemove); // Releases
   if (!result) {
     aRv.Throw(NS_ERROR_FAILURE);
   }
 }
 
 Element*
 Selection::GetCommonEditingHostForAllRanges()
 {
@@ -6485,31 +6484,28 @@ Selection::NotifySelectionListeners()
     }
   }
 
   RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
   if (frameSelection->GetBatching()) {
     frameSelection->SetDirty();
     return NS_OK;
   }
-  nsCOMArray<nsISelectionListener> selectionListeners(mSelectionListeners);
-  int32_t cnt = selectionListeners.Count();
-  if (cnt != mSelectionListeners.Count()) {
-    return NS_ERROR_OUT_OF_MEMORY;  // nsCOMArray is fallible
-  }
+  AutoTArray<nsCOMPtr<nsISelectionListener>, 8>
+    selectionListeners(mSelectionListeners);
 
   nsCOMPtr<nsIDOMDocument> domdoc;
   nsIPresShell* ps = GetPresShell();
   if (ps) {
     domdoc = do_QueryInterface(ps->GetDocument());
   }
 
   short reason = frameSelection->PopReason();
-  for (int32_t i = 0; i < cnt; i++) {
-    selectionListeners[i]->NotifySelectionChanged(domdoc, this, reason);
+  for (auto& listener : selectionListeners) {
+    listener->NotifySelectionChanged(domdoc, this, reason);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 Selection::StartBatchChanges()
 {
   if (mFrameSelection) {