Bug 1416084. P1 - reapply bug 1412737 P4: wake up readers only when we have blocks committed to the cache. draft
authorJW Wang <jwwang@mozilla.com>
Mon, 15 Jan 2018 10:09:02 +0800
changeset 720765 ff222a284e19b457faf0c547d28571871cbfac0c
parent 720191 21ddfb9e6cc008e47da89db50e22697dc7b38635
child 720766 8c9c8ef7277d834f37d6fdb72e8d7075c9863ee2
push id95630
push userjwwang@mozilla.com
push dateTue, 16 Jan 2018 07:50:46 +0000
bugs1416084, 1412737
milestone59.0a1
Bug 1416084. P1 - reapply bug 1412737 P4: wake up readers only when we have blocks committed to the cache. Per bug 1412737 P2 changes, a reader will always read data from the cache or from the last block in the memory. NotifyDataReceived() will be slightly more efficient if we don't wake up readers unnecessarily when there are no new blocks committed to the cache. MozReview-Commit-ID: Afcy5OOeIk3
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -2082,16 +2082,19 @@ MediaCacheStream::NotifyDataReceived(uin
     // mChannelOffset is updated to a new position when loading a new channel.
     // We should discard the data coming from the old channel so it won't be
     // stored to the wrong positoin.
     return;
   }
 
   mDownloadStatistics.AddBytes(aCount);
 
+  // True if we commit any blocks to the cache.
+  bool cacheUpdated = false;
+
   auto source = MakeSpan<const uint8_t>(aData, aCount);
 
   // We process the data one block (or part of a block) at a time
   while (!source.IsEmpty()) {
     // The data we've collected so far in the partial block.
     auto partial = MakeSpan<const uint8_t>(mPartialBlockBuffer.get(),
                                            OffsetInBlock(mChannelOffset));
 
@@ -2110,16 +2113,17 @@ MediaCacheStream::NotifyDataReceived(uin
         lock,
         this,
         OffsetToBlockIndexUnchecked(mChannelOffset),
         mMetadataInPartialBlockBuffer ? MODE_METADATA : MODE_PLAYBACK,
         partial,
         source.First(remaining));
       source = source.From(remaining);
       mChannelOffset += remaining;
+      cacheUpdated = true;
     } else {
       // The buffer to be filled in the partial block.
       auto buf = MakeSpan<uint8_t>(mPartialBlockBuffer.get() + partial.Length(),
                                    remaining);
       memcpy(buf.Elements(), source.Elements(), source.Length());
       mChannelOffset += source.Length();
       break;
     }
@@ -2129,20 +2133,22 @@ MediaCacheStream::NotifyDataReceived(uin
   while (MediaCacheStream* stream = iter.Next(lock)) {
     if (stream->mStreamLength >= 0) {
       // The stream is at least as long as what we've read
       stream->mStreamLength = std::max(stream->mStreamLength, mChannelOffset);
     }
     stream->mClient->CacheClientNotifyDataReceived();
   }
 
-  // Notify in case there's a waiting reader
   // XXX it would be fairly easy to optimize things a lot more to
   // avoid waking up reader threads unnecessarily
-  lock.NotifyAll();
+  if (cacheUpdated) {
+    // Wake up the reader who is waiting for the committed blocks.
+    lock.NotifyAll();
+  }
 }
 
 void
 MediaCacheStream::FlushPartialBlockInternal(AutoLock& aLock, bool aNotifyAll)
 {
   MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
 
   int32_t blockIndex = OffsetToBlockIndexUnchecked(mChannelOffset);