Bug 1417279 - Remove an odd second case from nsArray::IndexOf. r=froydnj draft
authorBlake Kaplan <mrbkap@gmail.com>
Tue, 14 Nov 2017 16:34:42 -0800
changeset 697943 6fd53dd513b887c58f7fbf8d1ee029504f5c2e16
parent 692325 b2f459b88cab5525c785a7fa70a01be3e9cdcb23
child 740249 7f00416438624430d241026f9cff797c2c3eb40a
push id89147
push userbmo:mrbkap@mozilla.com
push dateWed, 15 Nov 2017 01:06:25 +0000
reviewersfroydnj
bugs1417279
milestone58.0a1
Bug 1417279 - Remove an odd second case from nsArray::IndexOf. r=froydnj Now that nsArray uses nsCOMArray under the hood, we don't have to do weird ForwardEnumeration hacks to start IndexOf at a non-zero index. MozReview-Commit-ID: 3ReDV0BT0hn
xpcom/ds/nsArray.cpp
--- a/xpcom/ds/nsArray.cpp
+++ b/xpcom/ds/nsArray.cpp
@@ -3,27 +3,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsArray.h"
 #include "nsArrayEnumerator.h"
 #include "nsThreadUtils.h"
 
-// used by IndexOf()
-struct MOZ_STACK_CLASS findIndexOfClosure
-{
-  // This is only used for pointer comparison, so we can just use a void*.
-  void* targetElement;
-  uint32_t startIndex;
-  uint32_t resultIndex;
-};
-
-static bool FindElementCallback(void* aElement, void* aClosure);
-
 NS_INTERFACE_MAP_BEGIN(nsArray)
   NS_INTERFACE_MAP_ENTRY(nsIArray)
   NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions)
   NS_INTERFACE_MAP_ENTRY(nsIMutableArray)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray)
 NS_INTERFACE_MAP_END
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsArrayCC)
@@ -75,34 +64,22 @@ nsArrayBase::QueryElementAt(uint32_t aIn
   // doesn't addref its result
   return obj->QueryInterface(aIID, aResult);
 }
 
 NS_IMETHODIMP
 nsArrayBase::IndexOf(uint32_t aStartIndex, nsISupports* aElement,
                      uint32_t* aResult)
 {
-  // optimize for the common case by forwarding to mArray
-  if (aStartIndex == 0) {
-    uint32_t idx = mArray.IndexOf(aElement);
-    if (idx == UINT32_MAX) {
-      return NS_ERROR_FAILURE;
-    }
-
-    *aResult = idx;
-    return NS_OK;
-  }
-
-  findIndexOfClosure closure = { aElement, aStartIndex, 0 };
-  bool notFound = mArray.EnumerateForwards(FindElementCallback, &closure);
-  if (notFound) {
+  int32_t idx = mArray.IndexOf(aElement, aStartIndex);
+  if (idx == -1) {
     return NS_ERROR_FAILURE;
   }
 
-  *aResult = closure.resultIndex;
+  *aResult = static_cast<uint32_t>(idx);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsArrayBase::Enumerate(nsISimpleEnumerator** aResult)
 {
   return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this));
 }
@@ -155,35 +132,16 @@ nsArrayBase::Count(uint32_t* aResult)
 NS_IMETHODIMP
 nsArrayBase::GetElementAt(uint32_t aIndex, nsISupports** aResult)
 {
   nsCOMPtr<nsISupports> obj = mArray.SafeObjectAt(aIndex);
   obj.forget(aResult);
   return NS_OK;
 }
 
-//
-// static helper routines
-//
-bool
-FindElementCallback(void* aElement, void* aClosure)
-{
-  findIndexOfClosure* closure = static_cast<findIndexOfClosure*>(aClosure);
-  nsISupports* element = static_cast<nsISupports*>(aElement);
-
-  // don't start searching until we're past the startIndex
-  if (closure->resultIndex >= closure->startIndex &&
-      element == closure->targetElement) {
-    return false;    // stop! We found it
-  }
-  closure->resultIndex++;
-
-  return true;
-}
-
 nsresult
 nsArrayBase::XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID,
                               void** aResult)
 {
   if (aOuter) {
     return NS_ERROR_NO_AGGREGATION;
   }