diff --git a/image/imgLoader.cpp b/image/imgLoader.cpp index 627c4c709900..d6f92b4dabb1 100644 --- a/image/imgLoader.cpp +++ b/image/imgLoader.cpp @@ -975,31 +975,31 @@ 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; } @@ -1015,7 +1015,7 @@ imgCacheQueue::Push(imgCacheEntry* entry) mSize += entry->GetDataSize(); RefPtr 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()) { @@ -1026,16 +1026,16 @@ imgCacheQueue::Push(imgCacheEntry* entry) already_AddRefed imgCacheQueue::Pop() { - if (mQueue.empty()) { + if (mQueue.IsEmpty()) { return nullptr; } if (IsDirty()) { Refresh(); } - RefPtr entry = mQueue[0]; std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries); - mQueue.pop_back(); + RefPtr entry = Move(mQueue.LastElement()); + mQueue.RemoveElementAt(mQueue.Length() - 1); mSize -= entry->GetDataSize(); return entry.forget(); @@ -1065,7 +1065,7 @@ imgCacheQueue::IsDirty() uint32_t imgCacheQueue::GetNumElements() const { - return mQueue.size(); + return mQueue.Length(); } imgCacheQueue::iterator @@ -2046,13 +2046,13 @@ imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) // We have to make a temporary, since RemoveFromCache removes the element // from the queue, invalidating iterators. nsTArray > 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; } } diff --git a/image/imgLoader.h b/image/imgLoader.h index bae89da7d023..a1ffa899140e 100644 --- a/image/imgLoader.h +++ b/image/imgLoader.h @@ -201,7 +201,7 @@ public: uint32_t GetSize() const; void UpdateSize(int32_t diff); uint32_t GetNumElements() const; - typedef std::vector > queueContainer; + typedef nsTArray > queueContainer; typedef queueContainer::iterator iterator; typedef queueContainer::const_iterator const_iterator;