Bug 1396982 Make imageCacheQueue use nsTArray instead of std::vector. r=tnikkel
authorBen Kelly <ben@wanderview.com>
Tue, 05 Sep 2017 16:20:18 -0700
changeset 421469 eb36a2188f6f4eb40f092561f9020ec23bcda712
parent 421468 5ac7748ad699020b6f0cd9637ef281baf0c52558
child 421470 b191c933df2076747864be07b8e7fc402a54a687
push id82
push userfmarier@mozilla.com
push dateFri, 08 Sep 2017 01:10:49 +0000
reviewerstnikkel
bugs1396982
milestone57.0a1
Bug 1396982 Make imageCacheQueue use nsTArray instead of std::vector. r=tnikkel
image/imgLoader.cpp
image/imgLoader.h
--- a/image/imgLoader.cpp
+++ b/image/imgLoader.cpp
@@ -970,77 +970,77 @@ imgCacheQueue::GetSize() const
 }
 
 #include <algorithm>
 using namespace std;
 
 void
 imgCacheQueue::Remove(imgCacheEntry* entry)
 {
-  auto it = find(mQueue.begin(), mQueue.end(), entry);
-  if (it == mQueue.end()) {
+  uint64_t index = mQueue.IndexOf(entry);
+  if (index == queueContainer::NoIndex) {
     return;
   }
 
-  mSize -= (*it)->GetDataSize();
+  mSize -= mQueue[index]->GetDataSize();
 
   // If the queue is clean and this is the first entry,
   // then we can efficiently remove the entry without
   // dirtying the sort order.
-  if (!IsDirty() && it == mQueue.begin()) {
+  if (!IsDirty() && index == 0) {
     std::pop_heap(mQueue.begin(), mQueue.end(),
                   imgLoader::CompareCacheEntries);
-    mQueue.pop_back();
+    mQueue.RemoveElementAt(mQueue.Length() - 1);
     return;
   }
 
   // Remove from the middle of the list.  This potentially
   // breaks the binary heap sort order.
-  mQueue.erase(it);
+  mQueue.RemoveElementAt(index);
 
   // If we only have one entry or the queue is empty, though,
   // then the sort order is still effectively good.  Simply
   // refresh the list to clear the dirty flag.
-  if (mQueue.size() <= 1) {
+  if (mQueue.Length() <= 1) {
     Refresh();
     return;
   }
 
   // Otherwise we must mark the queue dirty and potentially
   // trigger an expensive sort later.
   MarkDirty();
 }
 
 void
 imgCacheQueue::Push(imgCacheEntry* entry)
 {
   mSize += entry->GetDataSize();
 
   RefPtr<imgCacheEntry> refptr(entry);
-  mQueue.push_back(refptr);
+  mQueue.AppendElement(Move(refptr));
   // If we're not dirty already, then we can efficiently add this to the
   // binary heap immediately.  This is only O(log n).
   if (!IsDirty()) {
     std::push_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
   }
 }
 
 already_AddRefed<imgCacheEntry>
 imgCacheQueue::Pop()
 {
-  if (mQueue.empty()) {
+  if (mQueue.IsEmpty()) {
     return nullptr;
   }
   if (IsDirty()) {
     Refresh();
   }
 
-  RefPtr<imgCacheEntry> entry = mQueue[0];
   std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
-  mQueue.pop_back();
+  RefPtr<imgCacheEntry> entry = Move(mQueue.LastElement());
+  mQueue.RemoveElementAt(mQueue.Length() - 1);
 
   mSize -= entry->GetDataSize();
   return entry.forget();
 }
 
 void
 imgCacheQueue::Refresh()
 {
@@ -1060,17 +1060,17 @@ bool
 imgCacheQueue::IsDirty()
 {
   return mDirty;
 }
 
 uint32_t
 imgCacheQueue::GetNumElements() const
 {
-  return mQueue.size();
+  return mQueue.Length();
 }
 
 imgCacheQueue::iterator
 imgCacheQueue::begin()
 {
   return mQueue.begin();
 }
 
@@ -2041,23 +2041,23 @@ imgLoader::EvictEntries(imgCacheTable& a
 nsresult
 imgLoader::EvictEntries(imgCacheQueue& aQueueToClear)
 {
   LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
 
   // We have to make a temporary, since RemoveFromCache removes the element
   // from the queue, invalidating iterators.
   nsTArray<RefPtr<imgCacheEntry> > entries(aQueueToClear.GetNumElements());
-  for (imgCacheQueue::const_iterator i = aQueueToClear.begin();
-       i != aQueueToClear.end(); ++i) {
+  for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
     entries.AppendElement(*i);
   }
 
-  for (uint32_t i = 0; i < entries.Length(); ++i) {
-    if (!RemoveFromCache(entries[i])) {
+  // Iterate in reverse order to minimize array copying.
+  for (auto& entry : entries) {
+    if (!RemoveFromCache(entry)) {
       return NS_ERROR_FAILURE;
     }
   }
 
   MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
 
   return NS_OK;
 }
--- a/image/imgLoader.h
+++ b/image/imgLoader.h
@@ -196,17 +196,17 @@ public:
   void Push(imgCacheEntry*);
   void MarkDirty();
   bool IsDirty();
   already_AddRefed<imgCacheEntry> Pop();
   void Refresh();
   uint32_t GetSize() const;
   void UpdateSize(int32_t diff);
   uint32_t GetNumElements() const;
-  typedef std::vector<RefPtr<imgCacheEntry> > queueContainer;
+  typedef nsTArray<RefPtr<imgCacheEntry> > queueContainer;
   typedef queueContainer::iterator iterator;
   typedef queueContainer::const_iterator const_iterator;
 
   iterator begin();
   const_iterator begin() const;
   iterator end();
   const_iterator end() const;