Bug 1499848 - make TimeoutExecutor timers slightly more efficient; r=baku
authorNathan Froyd <froydnj@mozilla.com>
Wed, 17 Oct 2018 19:57:36 -0400
changeset 500338 18c94f6d82046c4986c0c55f41a6fbf4039c8288
parent 500337 fd914881db604175bdd129d62ec1cb3c8d0a0386
child 500339 8b037400fdbed6b9fd14c36a8b32acd78a742e1d
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1499848
milestone64.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 1499848 - make TimeoutExecutor timers slightly more efficient; r=baku Instead of creating a timer and then setting the timer's target, we can determine the timer's target and pass it in directly when the timer is created. This reordering of steps is slightly more efficient, since SetTarget() is both a virtual call and requires locking, both of which can be skipped if we know the target at timer creation time.
dom/base/TimeoutExecutor.cpp
--- a/dom/base/TimeoutExecutor.cpp
+++ b/dom/base/TimeoutExecutor.cpp
@@ -51,32 +51,28 @@ TimeoutExecutor::ScheduleDelayed(const T
   MOZ_DIAGNOSTIC_ASSERT(mDeadline.IsNull());
   MOZ_DIAGNOSTIC_ASSERT(mMode == Mode::None);
   MOZ_DIAGNOSTIC_ASSERT(!aMinDelay.IsZero() ||
                         aDeadline > (aNow + mAllowedEarlyFiringTime));
 
   nsresult rv = NS_OK;
 
   if (!mTimer) {
-    mTimer = NS_NewTimer();
+    mTimer = NS_NewTimer(mOwner->EventTarget());
     NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
 
     uint32_t earlyMicros = 0;
     MOZ_ALWAYS_SUCCEEDS(mTimer->GetAllowedEarlyFiringMicroseconds(&earlyMicros));
     mAllowedEarlyFiringTime = TimeDuration::FromMicroseconds(earlyMicros);
+  } else {
+    // Always call Cancel() in case we are re-using a timer.
+    rv = mTimer->Cancel();
+    NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  // Always call Cancel() in case we are re-using a timer.  Otherwise
-  // the subsequent SetTarget() may fail.
-  rv = mTimer->Cancel();
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = mTimer->SetTarget(mOwner->EventTarget());
-  NS_ENSURE_SUCCESS(rv, rv);
-
   // Calculate the delay based on the deadline and current time.  If we have
   // a minimum delay set then clamp to that value.
   //
   // Note, we don't actually adjust our mDeadline for the minimum delay, just
   // the nsITimer value.  This is necessary to avoid lots of needless
   // rescheduling if more deadlines come in between now and the minimum delay
   // firing time.
   TimeDuration delay = TimeDuration::Max(aMinDelay, aDeadline - aNow);