Bug 1407549 - Give MediaSegment's chunk array a default capacity. r?padenot
MozReview-Commit-ID: 5BZMFimFjpS
--- 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
{}