Bug 1407549 - Give MediaSegment's chunk array a default capacity. r?padenot draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Wed, 28 Mar 2018 14:39:54 +0200
changeset 776609 74f617894a3a76bcd8a90cea4b45f989541e4d00
parent 771093 7771df14ea181add1dc4133f0f5559bf620bf976
child 776610 931c56f7113f6d4331062f0e2a5b9fc2b30416da
push id104925
push userbmo:apehrson@mozilla.com
push dateTue, 03 Apr 2018 13:43:03 +0000
reviewerspadenot
bugs1407549
milestone61.0a1
Bug 1407549 - Give MediaSegment's chunk array a default capacity. r?padenot MozReview-Commit-ID: 5BZMFimFjpS
dom/media/MediaSegment.h
--- a/dom/media/MediaSegment.h
+++ b/dom/media/MediaSegment.h
@@ -52,16 +52,26 @@ const StreamTime STREAM_TIME_MAX = MEDIA
 
 /**
  * Media time relative to the start of the graph timeline.
  */
 typedef MediaTime GraphTime;
 const GraphTime GRAPH_TIME_MAX = MEDIA_TIME_MAX;
 
 /**
+ * The number of chunks allocated by default for a MediaSegment.
+ * Appending more chunks than this will cause further allocations.
+ *
+ * 16 is an arbitrary number intended to cover the most common cases in the
+ * MediaStreamGraph (1 with silence and 1-2 with data for a realtime track)
+ * with some margin.
+ */
+const size_t DEFAULT_SEGMENT_CAPACITY = 16;
+
+/**
  * We pass the principal through the MediaStreamGraph by wrapping it in a thread
  * safe nsMainThreadPtrHandle, since it cannot be used directly off the main
  * thread. We can compare two PrincipalHandles to each other on any thread, but
  * they can only be created and converted back to nsIPrincipal* on main thread.
  */
 typedef nsMainThreadPtrHandle<nsIPrincipal> PrincipalHandle;
 
 inline PrincipalHandle MakePrincipalHandle(nsIPrincipal* aPrincipal)
@@ -349,17 +359,18 @@ public:
   {
     StreamTime duration = GetDuration();
     Clear();
     AppendNullData(duration);
   }
   void Clear() override
   {
     mDuration = 0;
-    mChunks.Clear();
+    mChunks.ClearAndRetainStorage();
+    mChunks.SetCapacity(DEFAULT_SEGMENT_CAPACITY);
   }
 
   class ChunkIterator {
   public:
     explicit ChunkIterator(MediaSegmentBase<C, Chunk>& aSegment)
       : mSegment(aSegment), mIndex(0) {}
     bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
     void Next() { ++mIndex; }
@@ -431,17 +442,20 @@ public:
   {
     if (mChunks.IsEmpty()) {
       return nullptr;
     }
     return &mChunks[mChunks.Length() - 1];
   }
 
 protected:
-  explicit MediaSegmentBase(Type aType) : MediaSegment(aType) {}
+  explicit MediaSegmentBase(Type aType)
+    : MediaSegment(aType)
+    , mChunks(DEFAULT_SEGMENT_CAPACITY)
+  {}
 
   MediaSegmentBase(MediaSegmentBase&& aSegment)
     : MediaSegment(Move(aSegment))
     , mChunks(Move(aSegment.mChunks))
 #ifdef MOZILLA_INTERNAL_API
     , mTimeStamp(Move(aSegment.mTimeStamp))
 #endif
   {}