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

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

Issue 2789683002: Speed up SimpleCache eviction set computation (Closed)
Patch Set: 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
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..241658d2046699905d790bec1a03de3be77c5c33 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
pasko 2017/03/31 17:41:56 nit: period at the end of the sentence
Maks Orlovich 2017/03/31 19:01:29 Done.
+ std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries;
+ entries.reserve(entries_set_.size());
+ for (EntrySet::const_iterator i = entries_set_.begin();
pasko 2017/03/31 17:41:56 how about: for (auto& entry : entries_set_) { ...
Maks Orlovich 2017/03/31 19:01:29 Hmm, I think it'll work (with a const... might not
pasko 2017/04/03 09:39:08 After looking at it 2nd time I think the explicit
+ 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.GetLastUsedTime() < b->second.GetLastUsedTime();
Maks Orlovich 2017/03/30 18:51:29 Can get it further down to 2.9-3ms range by adding
pasko 2017/03/31 17:41:56 yeah, comparing the seconds directly sounds worth
Maks Orlovich 2017/03/31 19:01:29 Done. By inline I just meant int Accessor() const
pasko 2017/04/03 09:39:08 Acknowledged.
+ });
+
+ // Remove as many entries from the index to get below |low_watermark_|,
+ // collecting relevant hashed into entry_hashes.
+ std::vector<uint64_t> entry_hashes;
+ entry_hashes.reserve(entries_set_.size() / 16);
pasko 2017/03/31 17:41:56 why 16? The low_watermark_ is based on kEvictionMa
Maks Orlovich 2017/03/31 19:01:29 Mostly just going with a nearest power of 2. Proba
pasko 2017/04/03 09:39:08 Hm, I did not think of the division overhead here,
+ 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,
« net/disk_cache/simple/simple_index.h ('K') | « 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