Bug 1412737. P4 - wake up readers only when we have blocks committed to the cache. draft
authorJW Wang <jwwang@mozilla.com>
Mon, 06 Nov 2017 14:17:35 +0800
changeset 694686 bcbed61d6c7213e4374b40c496dae351c4816223
parent 694685 a2495a92941035af485e4172ed6c1f1d44f1cc0e
child 694687 61d57e6605718afd65c91257482f618c16cfd06a
child 694763 6c2577bd71024ad031a86836c9c604bfef726b5a
push id88196
push userjwwang@mozilla.com
push dateWed, 08 Nov 2017 02:29:19 +0000
bugs1412737
milestone58.0a1
Bug 1412737. P4 - wake up readers only when we have blocks committed to the cache. Per 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: 3UWHbvtEUmu
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -2013,16 +2013,19 @@ MediaCacheStream::NotifyDataReceived(uin
 
   if (mLoadID != aLoadID) {
     // 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;
   }
 
+  // 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));
 
@@ -2040,16 +2043,17 @@ MediaCacheStream::NotifyDataReceived(uin
       mMediaCache->AllocateAndWriteBlock(
         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;
     }
@@ -2059,20 +2063,22 @@ MediaCacheStream::NotifyDataReceived(uin
   while (MediaCacheStream* stream = iter.Next()) {
     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
-  mon.NotifyAll();
+  if (cacheUpdated) {
+    // Wake up the reader who is waiting for the committed blocks.
+    mon.NotifyAll();
+  }
 }
 
 void
 MediaCacheStream::FlushPartialBlockInternal(bool aNotifyAll,
                                             ReentrantMonitorAutoEnter& aReentrantMonitor)
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");