Bug 1425623 - Don't allocate array on the heap. r=padenot
authorJean-Yves Avenard <jyavenard@mozilla.com>
Sun, 17 Dec 2017 15:47:44 +0100
changeset 448459 017630a4b3712b5763b5bf2a66af1ede585a8ddc
parent 448458 dd25e7aba0033b423e09f457ad298787c57a86c9
child 448460 6b1f9a0e13469a001e6ca2feda6a05153a4d9414
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1425623
milestone59.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 1425623 - Don't allocate array on the heap. r=padenot Instead allocate it on the stack and provide it as out parameter. MozReview-Commit-ID: 9fSJ68EfAga
dom/media/MediaStreamGraph.cpp
dom/media/MediaStreamGraph.h
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -1136,32 +1136,29 @@ MediaStreamGraphImpl::RunMessagesInQueue
 }
 
 void
 MediaStreamGraphImpl::UpdateGraph(GraphTime aEndBlockingDecisions)
 {
   MOZ_ASSERT(OnGraphThread());
   MOZ_ASSERT(aEndBlockingDecisions >= mProcessedTime);
   // The next state computed time can be the same as the previous: it
-  // means the driver would be have been blocking indefinitly, but the graph has
+  // means the driver would have been blocking indefinitly, but the graph has
   // been woken up right after having been to sleep.
   MOZ_ASSERT(aEndBlockingDecisions >= mStateComputedTime);
 
   UpdateStreamOrder();
 
   bool ensureNextIteration = false;
 
   // Grab pending stream input and compute blocking time
-  // TODO: Ensure that heap memory allocations isn't going to be a problem.
-  // Maybe modify code to use nsAutoTArray as out parameters.
-  nsTArray<RefPtr<SourceMediaStream::NotifyPullPromise>> promises;
+  AutoTArray<RefPtr<SourceMediaStream::NotifyPullPromise>, 64> promises;
   for (MediaStream* stream : mStreams) {
     if (SourceMediaStream* is = stream->AsSourceStream()) {
-      promises.AppendElements(
-        is->PullNewData(aEndBlockingDecisions, &ensureNextIteration));
+      ensureNextIteration |= is->PullNewData(aEndBlockingDecisions, promises);
     }
   }
 
   // Wait until all PullEnabled stream's listeners have completed.
   if (!promises.IsEmpty()) {
     AwaitAll(do_AddRef(mThreadPool), promises);
   }
 
@@ -2707,58 +2704,56 @@ SourceMediaStream::SetPullEnabled(bool a
 {
   MutexAutoLock lock(mMutex);
   mPullEnabled = aEnabled;
   if (mPullEnabled && GraphImpl()) {
     GraphImpl()->EnsureNextIteration();
   }
 }
 
-nsTArray<RefPtr<SourceMediaStream::NotifyPullPromise>>
-SourceMediaStream::PullNewData(StreamTime aDesiredUpToTime,
-                               bool* aEnsureNextIteration)
+bool
+SourceMediaStream::PullNewData(
+  StreamTime aDesiredUpToTime,
+  nsTArray<RefPtr<SourceMediaStream::NotifyPullPromise>>& aPromises)
 {
-  // 2 is the average number of listeners per SourceMediaStream.
-  nsTArray<RefPtr<SourceMediaStream::NotifyPullPromise>> promises(2);
   MutexAutoLock lock(mMutex);
   if (!mPullEnabled || mFinished) {
-    return promises;
+    return false;
   }
   // Compute how much stream time we'll need assuming we don't block
   // the stream at all.
   StreamTime t = GraphTimeToStreamTime(aDesiredUpToTime);
   StreamTime current = mTracks.GetEnd();
   LOG(LogLevel::Verbose,
       ("Calling NotifyPull aStream=%p t=%f current end=%f",
         this,
         GraphImpl()->MediaTimeToSeconds(t),
         GraphImpl()->MediaTimeToSeconds(current)));
   if (t <= current) {
-    return promises;
+    return false;
   }
-  *aEnsureNextIteration = true;
 #ifdef DEBUG
   if (mListeners.Length() == 0) {
     LOG(
       LogLevel::Error,
       ("No listeners in NotifyPull aStream=%p desired=%f current end=%f",
         this,
         GraphImpl()->MediaTimeToSeconds(t),
         GraphImpl()->MediaTimeToSeconds(current)));
     DumpTrackInfo();
   }
 #endif
   for (uint32_t j = 0; j < mListeners.Length(); ++j) {
     MediaStreamListener* l = mListeners[j];
     {
       MutexAutoUnlock unlock(mMutex);
-      promises.AppendElement(l->AsyncNotifyPull(GraphImpl(), t));
+      aPromises.AppendElement(l->AsyncNotifyPull(GraphImpl(), t));
     }
   }
-  return promises;
+  return true;
 }
 
 void
 SourceMediaStream::ExtractPendingInput()
 {
   MutexAutoLock lock(mMutex);
 
   bool finished = mFinishPending;
--- a/dom/media/MediaStreamGraph.h
+++ b/dom/media/MediaStreamGraph.h
@@ -697,20 +697,24 @@ public:
    * Due to unavoidable race conditions, after a call to SetPullEnabled(false)
    * it is still possible for a NotifyPull to occur.
    */
   void SetPullEnabled(bool aEnabled);
 
   /**
    * Call all MediaStreamListeners to request new data via the NotifyPull API
    * (if enabled).
+   * aDesiredUpToTime (in): end time of new data requested.
+   * aPromises (out): NotifyPullPromises if async API is enabled.
+   *
+   * Returns true if new data is about to be added.
    */
   typedef MozPromise<bool, bool, true /* is exclusive */ > NotifyPullPromise;
-  nsTArray<RefPtr<NotifyPullPromise>> PullNewData(StreamTime aDesiredUpToTime,
-                                                  bool* aEnsureNextIteration);
+  bool PullNewData(StreamTime aDesiredUpToTime,
+                   nsTArray<RefPtr<NotifyPullPromise>>& aPromises);
 
   /**
    * Extract any state updates pending in the stream, and apply them.
    */
   void ExtractPendingInput();
 
   /**
    * These add/remove DirectListeners, which allow bypassing the graph and any