Bug 1053682 - Optimize SourceBufferResource::ReadAt/ReadFromCache a little. r=cajbir
authorMatthew Gregan <kinetik@flim.org>
Thu, 14 Aug 2014 01:41:00 +1200
changeset 200043 19f58dab5e16
parent 200042 0f59a9eb1658
child 200044 447169009d2b
push id27330
push useremorley@mozilla.com
push dateMon, 18 Aug 2014 13:22:34 +0000
treeherdermozilla-central@6b75f748388a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscajbir
bugs1053682
milestone34.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 1053682 - Optimize SourceBufferResource::ReadAt/ReadFromCache a little. r=cajbir
content/media/mediasource/SourceBufferResource.cpp
content/media/mediasource/SourceBufferResource.h
--- a/content/media/mediasource/SourceBufferResource.cpp
+++ b/content/media/mediasource/SourceBufferResource.cpp
@@ -82,62 +82,71 @@ SourceBufferResource::Read(char* aBuffer
 }
 
 nsresult
 SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes)
 {
   SBR_DEBUG("SourceBufferResource(%p)::ReadAt(aOffset=%lld, aBuffer=%p, aCount=%u, aBytes=%p)",
             this, aOffset, aBytes, aCount, aBytes);
   ReentrantMonitorAutoEnter mon(mMonitor);
-  nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
+  nsresult rv = SeekInternal(aOffset);
   if (NS_FAILED(rv)) {
     return rv;
   }
   return Read(aBuffer, aCount, aBytes);
 }
 
 nsresult
 SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset)
 {
   SBR_DEBUG("SourceBufferResource(%p)::Seek(aWhence=%d, aOffset=%lld)", this, aWhence, aOffset);
   ReentrantMonitorAutoEnter mon(mMonitor);
-  if (mClosed) {
-    return NS_ERROR_FAILURE;
-  }
 
   int64_t newOffset = mOffset;
   switch (aWhence) {
   case nsISeekableStream::NS_SEEK_END:
     newOffset = GetLength() - aOffset;
     break;
   case nsISeekableStream::NS_SEEK_CUR:
     newOffset += aOffset;
     break;
   case nsISeekableStream::NS_SEEK_SET:
     newOffset = aOffset;
     break;
   }
 
   SBR_DEBUGV("SourceBufferResource(%p)::Seek() newOffset=%lld GetOffset()=%llu GetLength()=%llu)",
              this, newOffset, mInputBuffer.GetOffset(), GetLength());
-  if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) {
+  nsresult rv = SeekInternal(newOffset);
+  mon.NotifyAll();
+  return rv;
+}
+
+nsresult
+SourceBufferResource::SeekInternal(int64_t aOffset)
+{
+  mMonitor.AssertCurrentThreadIn();
+
+  if (mClosed ||
+      aOffset < 0 ||
+      uint64_t(aOffset) < mInputBuffer.GetOffset() ||
+      aOffset > GetLength()) {
     return NS_ERROR_FAILURE;
   }
 
-  mOffset = newOffset;
-  mon.NotifyAll();
-
+  mOffset = aOffset;
   return NS_OK;
 }
 
 nsresult
 SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
 {
   SBR_DEBUG("SourceBufferResource(%p)::ReadFromCache(aBuffer=%p, aOffset=%lld, aCount=%u)",
             this, aBuffer, aOffset, aCount);
+  ReentrantMonitorAutoEnter mon(mMonitor);
   int64_t oldOffset = mOffset;
   nsresult rv = ReadAt(aOffset, aBuffer, aCount, nullptr);
   mOffset = oldOffset;
   return rv;
 }
 
 bool
 SourceBufferResource::EvictData(uint32_t aThreshold)
--- a/content/media/mediasource/SourceBufferResource.h
+++ b/content/media/mediasource/SourceBufferResource.h
@@ -268,16 +268,18 @@ public:
   // Remove data from resource if it holds more than the threshold
   // number of bytes. Returns true if some data was evicted.
   bool EvictData(uint32_t aThreshold);
 
   // Remove data from resource before the given offset.
   void EvictBefore(uint64_t aOffset);
 
 private:
+  nsresult SeekInternal(int64_t aOffset);
+
   nsCOMPtr<nsIPrincipal> mPrincipal;
   const nsCString mType;
 
   // Provides synchronization between SourceBuffers and InputAdapters.
   // Protects all of the member variables below.  Read() will await a
   // Notify() (from Seek, AppendData, Ended, or Close) when insufficient
   // data is available in mData.
   mutable ReentrantMonitor mMonitor;