Bug 1622114 - Improve array manipulation in CleanupIDBTransactions. r=dom-workers-and-storage-reviewers,smaug,asuth a=jcristau
authorSimon Giesecke <sgiesecke@mozilla.com>
Thu, 19 Mar 2020 12:55:12 +0000
changeset 580706 04d1f66cf93c9f4684cafd620229e707ff5ab573
parent 580705 5e50531f8c329fb5f84e1bacd69a2a1415857866
child 580707 bb8f832e48dc540373b5c0bf1c32ec010abd931a
push id12927
push userapavel@mozilla.com
push dateThu, 26 Mar 2020 17:37:55 +0000
treeherdermozilla-beta@45be288db1b7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, smaug, asuth, jcristau
bugs1622114
milestone75.0
Bug 1622114 - Improve array manipulation in CleanupIDBTransactions. r=dom-workers-and-storage-reviewers,smaug,asuth a=jcristau - Do not remove elements one-by-one but use remove-erase pattern. - Move mPendingIDBTransactions array instead of copying. Differential Revision: https://phabricator.services.mozilla.com/D67159
xpcom/base/CycleCollectedJSContext.cpp
--- a/xpcom/base/CycleCollectedJSContext.cpp
+++ b/xpcom/base/CycleCollectedJSContext.cpp
@@ -412,34 +412,37 @@ void CycleCollectedJSContext::ProcessSta
 void CycleCollectedJSContext::CleanupIDBTransactions(uint32_t aRecursionDepth) {
   MOZ_ASSERT(mJSContext);
   MOZ_RELEASE_ASSERT(!mDoingStableStates);
   mDoingStableStates = true;
 
   nsTArray<PendingIDBTransactionData> localQueue =
       std::move(mPendingIDBTransactions);
 
-  for (uint32_t i = 0; i < localQueue.Length(); ++i) {
-    PendingIDBTransactionData& data = localQueue[i];
-    if (data.mRecursionDepth != aRecursionDepth) {
-      continue;
-    }
+  localQueue.RemoveElementsAt(
+      std::remove_if(localQueue.begin(), localQueue.end(),
+                     [aRecursionDepth](PendingIDBTransactionData& data) {
+                       if (data.mRecursionDepth != aRecursionDepth) {
+                         return false;
+                       }
 
-    {
-      nsCOMPtr<nsIRunnable> transaction = std::move(data.mTransaction);
-      transaction->Run();
-    }
+                       {
+                         nsCOMPtr<nsIRunnable> transaction =
+                             std::move(data.mTransaction);
+                         transaction->Run();
+                       }
 
-    localQueue.RemoveElementAt(i--);
-  }
+                       return true;
+                     }),
+      localQueue.end());
 
-  // If the queue has events in it now, they were added from something we
-  // called, so they belong at the end of the queue.
-  localQueue.AppendElements(mPendingIDBTransactions);
-  localQueue.SwapElements(mPendingIDBTransactions);
+  // If mPendingIDBTransactions has events in it now, they were added from
+  // something we called, so they belong at the end of the queue.
+  localQueue.AppendElements(std::move(mPendingIDBTransactions));
+  mPendingIDBTransactions = std::move(localQueue);
   mDoingStableStates = false;
 }
 
 void CycleCollectedJSContext::BeforeProcessTask(bool aMightBlock) {
   // If ProcessNextEvent was called during a microtask callback, we
   // must process any pending microtasks before blocking in the event loop,
   // otherwise we may deadlock until an event enters the queue later.
   if (aMightBlock && PerformMicroTaskCheckPoint()) {