Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Unified Diff: net/disk_cache/simple/simple_index.cc

Issue 2789683002: Speed up SimpleCache eviction set computation (Closed)
Patch Set: git cl try Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/simple/simple_index.cc
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index 57f9603a391e44c99c99192b7e28a54f5878ba8c..4e5c833d2aca11ae6374f11b21cb103d56d0cfba 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -50,31 +50,6 @@ const uint32_t kEvictionMarginDivisor = 20;
const uint32_t kBytesInKb = 1024;
-// Utility class used for timestamp comparisons in entry metadata while sorting.
-class CompareHashesForTimestamp {
- typedef disk_cache::SimpleIndex SimpleIndex;
- typedef disk_cache::SimpleIndex::EntrySet EntrySet;
- public:
- explicit CompareHashesForTimestamp(const EntrySet& set);
-
- bool operator()(uint64_t hash1, uint64_t hash2);
-
- private:
- const EntrySet& entry_set_;
-};
-
-CompareHashesForTimestamp::CompareHashesForTimestamp(const EntrySet& set)
- : entry_set_(set) {
-}
-
-bool CompareHashesForTimestamp::operator()(uint64_t hash1, uint64_t hash2) {
- EntrySet::const_iterator it1 = entry_set_.find(hash1);
- DCHECK(it1 != entry_set_.end());
- EntrySet::const_iterator it2 = entry_set_.find(hash2);
- DCHECK(it2 != entry_set_.end());
- return it1->second.GetLastUsedTime() < it2->second.GetLastUsedTime();
-}
-
} // namespace
namespace disk_cache {
@@ -331,28 +306,35 @@ void SimpleIndex::StartEvictionIfNeeded() {
SIMPLE_CACHE_UMA(
MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_,
static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb));
- std::vector<uint64_t> entry_hashes;
- entry_hashes.reserve(entries_set_.size());
- for (EntrySet::const_iterator it = entries_set_.begin(),
- end = entries_set_.end(); it != end; ++it) {
- entry_hashes.push_back(it->first);
+
+ // Flatten for sorting.
+ std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries;
+ entries.reserve(entries_set_.size());
+ for (EntrySet::const_iterator i = entries_set_.begin();
+ i != entries_set_.end(); ++i) {
+ entries.push_back(&*i);
}
- std::sort(entry_hashes.begin(), entry_hashes.end(),
- CompareHashesForTimestamp(entries_set_));
- // Remove as many entries from the index to get below |low_watermark_|.
- std::vector<uint64_t>::iterator it = entry_hashes.begin();
+ std::sort(entries.begin(), entries.end(),
+ [](const std::pair<const uint64_t, EntryMetadata>* a,
+ const std::pair<const uint64_t, EntryMetadata>* b) -> bool {
+ return a->second.RawTimeForSorting() <
+ b->second.RawTimeForSorting();
+ });
+
+ // Remove as many entries from the index to get below |low_watermark_|,
+ // collecting least recently used hashes into |entry_hashes|.
+ std::vector<uint64_t> entry_hashes;
+ std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it =
+ entries.begin();
uint64_t evicted_so_far_size = 0;
while (evicted_so_far_size < cache_size_ - low_watermark_) {
- DCHECK(it != entry_hashes.end());
- EntrySet::iterator found_meta = entries_set_.find(*it);
- DCHECK(found_meta != entries_set_.end());
- evicted_so_far_size += found_meta->second.GetEntrySize();
+ DCHECK(it != entries.end());
+ entry_hashes.push_back((*it)->first);
+ evicted_so_far_size += (*it)->second.GetEntrySize();
++it;
}
- // Take out the rest of hashes from the eviction list.
- entry_hashes.erase(it, entry_hashes.end());
SIMPLE_CACHE_UMA(COUNTS,
"Eviction.EntryCount", cache_type_, entry_hashes.size());
SIMPLE_CACHE_UMA(TIMES,
@@ -403,6 +385,13 @@ void SimpleIndex::InsertInEntrySet(
entry_set->insert(std::make_pair(entry_hash, entry_metadata));
}
+void SimpleIndex::InsertEntryForTesting(uint64_t entry_hash,
+ const EntryMetadata& entry_metadata) {
+ DCHECK(entries_set_.find(entry_hash) == entries_set_.end());
+ InsertInEntrySet(entry_hash, entry_metadata, &entries_set_);
+ cache_size_ += entry_metadata.GetEntrySize();
+}
+
void SimpleIndex::PostponeWritingToDisk() {
if (!initialized_)
return;
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698