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

Side by Side Diff: net/disk_cache/simple/simple_index.cc

Issue 2789683002: Speed up SimpleCache eviction set computation (Closed)
Patch Set: Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/disk_cache/simple/simple_index.h" 5 #include "net/disk_cache/simple/simple_index.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 // operation has happened. 43 // operation has happened.
44 const int kWriteToDiskDelayMSecs = 20000; 44 const int kWriteToDiskDelayMSecs = 20000;
45 const int kWriteToDiskOnBackgroundDelayMSecs = 100; 45 const int kWriteToDiskOnBackgroundDelayMSecs = 100;
46 46
47 // Divides the cache space into this amount of parts to evict when only one part 47 // Divides the cache space into this amount of parts to evict when only one part
48 // is left. 48 // is left.
49 const uint32_t kEvictionMarginDivisor = 20; 49 const uint32_t kEvictionMarginDivisor = 20;
50 50
51 const uint32_t kBytesInKb = 1024; 51 const uint32_t kBytesInKb = 1024;
52 52
53 // Utility class used for timestamp comparisons in entry metadata while sorting.
54 class CompareHashesForTimestamp {
55 typedef disk_cache::SimpleIndex SimpleIndex;
56 typedef disk_cache::SimpleIndex::EntrySet EntrySet;
57 public:
58 explicit CompareHashesForTimestamp(const EntrySet& set);
59
60 bool operator()(uint64_t hash1, uint64_t hash2);
61
62 private:
63 const EntrySet& entry_set_;
64 };
65
66 CompareHashesForTimestamp::CompareHashesForTimestamp(const EntrySet& set)
67 : entry_set_(set) {
68 }
69
70 bool CompareHashesForTimestamp::operator()(uint64_t hash1, uint64_t hash2) {
71 EntrySet::const_iterator it1 = entry_set_.find(hash1);
72 DCHECK(it1 != entry_set_.end());
73 EntrySet::const_iterator it2 = entry_set_.find(hash2);
74 DCHECK(it2 != entry_set_.end());
75 return it1->second.GetLastUsedTime() < it2->second.GetLastUsedTime();
76 }
77
78 } // namespace 53 } // namespace
79 54
80 namespace disk_cache { 55 namespace disk_cache {
81 56
82 EntryMetadata::EntryMetadata() 57 EntryMetadata::EntryMetadata()
83 : last_used_time_seconds_since_epoch_(0), 58 : last_used_time_seconds_since_epoch_(0),
84 entry_size_(0) { 59 entry_size_(0) {
85 } 60 }
86 61
87 EntryMetadata::EntryMetadata(base::Time last_used_time, 62 EntryMetadata::EntryMetadata(base::Time last_used_time,
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 return; 299 return;
325 // Take all live key hashes from the index and sort them by time. 300 // Take all live key hashes from the index and sort them by time.
326 eviction_in_progress_ = true; 301 eviction_in_progress_ = true;
327 eviction_start_time_ = base::TimeTicks::Now(); 302 eviction_start_time_ = base::TimeTicks::Now();
328 SIMPLE_CACHE_UMA( 303 SIMPLE_CACHE_UMA(
329 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, 304 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_,
330 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); 305 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb));
331 SIMPLE_CACHE_UMA( 306 SIMPLE_CACHE_UMA(
332 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, 307 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_,
333 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); 308 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb));
309
310 // 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.
311 std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries;
312 entries.reserve(entries_set_.size());
313 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
314 i != entries_set_.end(); ++i) {
315 entries.push_back(&*i);
316 }
317
318 std::sort(entries.begin(), entries.end(),
319 [](const std::pair<const uint64_t, EntryMetadata>* a,
320 const std::pair<const uint64_t, EntryMetadata>* b) -> bool {
321 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.
322 });
323
324 // Remove as many entries from the index to get below |low_watermark_|,
325 // collecting relevant hashed into entry_hashes.
334 std::vector<uint64_t> entry_hashes; 326 std::vector<uint64_t> entry_hashes;
335 entry_hashes.reserve(entries_set_.size()); 327 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,
336 for (EntrySet::const_iterator it = entries_set_.begin(), 328 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it =
337 end = entries_set_.end(); it != end; ++it) { 329 entries.begin();
338 entry_hashes.push_back(it->first);
339 }
340 std::sort(entry_hashes.begin(), entry_hashes.end(),
341 CompareHashesForTimestamp(entries_set_));
342
343 // Remove as many entries from the index to get below |low_watermark_|.
344 std::vector<uint64_t>::iterator it = entry_hashes.begin();
345 uint64_t evicted_so_far_size = 0; 330 uint64_t evicted_so_far_size = 0;
346 while (evicted_so_far_size < cache_size_ - low_watermark_) { 331 while (evicted_so_far_size < cache_size_ - low_watermark_) {
347 DCHECK(it != entry_hashes.end()); 332 DCHECK(it != entries.end());
348 EntrySet::iterator found_meta = entries_set_.find(*it); 333 entry_hashes.push_back((*it)->first);
349 DCHECK(found_meta != entries_set_.end()); 334 evicted_so_far_size += (*it)->second.GetEntrySize();
350 evicted_so_far_size += found_meta->second.GetEntrySize();
351 ++it; 335 ++it;
352 } 336 }
353 337
354 // Take out the rest of hashes from the eviction list.
355 entry_hashes.erase(it, entry_hashes.end());
356 SIMPLE_CACHE_UMA(COUNTS, 338 SIMPLE_CACHE_UMA(COUNTS,
357 "Eviction.EntryCount", cache_type_, entry_hashes.size()); 339 "Eviction.EntryCount", cache_type_, entry_hashes.size());
358 SIMPLE_CACHE_UMA(TIMES, 340 SIMPLE_CACHE_UMA(TIMES,
359 "Eviction.TimeToSelectEntries", cache_type_, 341 "Eviction.TimeToSelectEntries", cache_type_,
360 base::TimeTicks::Now() - eviction_start_time_); 342 base::TimeTicks::Now() - eviction_start_time_);
361 SIMPLE_CACHE_UMA( 343 SIMPLE_CACHE_UMA(
362 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, 344 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_,
363 static_cast<base::HistogramBase::Sample>( 345 static_cast<base::HistogramBase::Sample>(
364 evicted_so_far_size / kBytesInKb)); 346 evicted_so_far_size / kBytesInKb));
365 347
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 start - last_write_to_disk_); 506 start - last_write_to_disk_);
525 } 507 }
526 } 508 }
527 last_write_to_disk_ = start; 509 last_write_to_disk_ = start;
528 510
529 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, 511 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start,
530 app_on_background_, base::Closure()); 512 app_on_background_, base::Closure());
531 } 513 }
532 514
533 } // namespace disk_cache 515 } // namespace disk_cache
OLDNEW
« 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