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

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

Issue 2789683002: Speed up SimpleCache eviction set computation (Closed)
Patch Set: Apply review feedback. 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.
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();
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.RawTimeForSorting() <
322 b->second.RawTimeForSorting();
323 });
324
325 // Remove as many entries from the index to get below |low_watermark_|,
326 // collecting relevant hashed into entry_hashes.
pasko 2017/04/03 09:39:08 s/relevant/least recently used/ <- reduces guessin
Maks Orlovich 2017/04/03 14:32:40 Done.
334 std::vector<uint64_t> entry_hashes; 327 std::vector<uint64_t> entry_hashes;
335 entry_hashes.reserve(entries_set_.size()); 328 entry_hashes.reserve(entries_set_.size() / 16);
336 for (EntrySet::const_iterator it = entries_set_.begin(), 329 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it =
337 end = entries_set_.end(); it != end; ++it) { 330 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; 331 uint64_t evicted_so_far_size = 0;
346 while (evicted_so_far_size < cache_size_ - low_watermark_) { 332 while (evicted_so_far_size < cache_size_ - low_watermark_) {
347 DCHECK(it != entry_hashes.end()); 333 DCHECK(it != entries.end());
348 EntrySet::iterator found_meta = entries_set_.find(*it); 334 entry_hashes.push_back((*it)->first);
349 DCHECK(found_meta != entries_set_.end()); 335 evicted_so_far_size += (*it)->second.GetEntrySize();
350 evicted_so_far_size += found_meta->second.GetEntrySize();
351 ++it; 336 ++it;
352 } 337 }
353 338
354 // Take out the rest of hashes from the eviction list.
355 entry_hashes.erase(it, entry_hashes.end());
356 SIMPLE_CACHE_UMA(COUNTS, 339 SIMPLE_CACHE_UMA(COUNTS,
357 "Eviction.EntryCount", cache_type_, entry_hashes.size()); 340 "Eviction.EntryCount", cache_type_, entry_hashes.size());
358 SIMPLE_CACHE_UMA(TIMES, 341 SIMPLE_CACHE_UMA(TIMES,
359 "Eviction.TimeToSelectEntries", cache_type_, 342 "Eviction.TimeToSelectEntries", cache_type_,
360 base::TimeTicks::Now() - eviction_start_time_); 343 base::TimeTicks::Now() - eviction_start_time_);
361 SIMPLE_CACHE_UMA( 344 SIMPLE_CACHE_UMA(
362 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, 345 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_,
363 static_cast<base::HistogramBase::Sample>( 346 static_cast<base::HistogramBase::Sample>(
364 evicted_so_far_size / kBytesInKb)); 347 evicted_so_far_size / kBytesInKb));
365 348
(...skipping 30 matching lines...) Expand all
396 379
397 // static 380 // static
398 void SimpleIndex::InsertInEntrySet( 381 void SimpleIndex::InsertInEntrySet(
399 uint64_t entry_hash, 382 uint64_t entry_hash,
400 const disk_cache::EntryMetadata& entry_metadata, 383 const disk_cache::EntryMetadata& entry_metadata,
401 EntrySet* entry_set) { 384 EntrySet* entry_set) {
402 DCHECK(entry_set); 385 DCHECK(entry_set);
403 entry_set->insert(std::make_pair(entry_hash, entry_metadata)); 386 entry_set->insert(std::make_pair(entry_hash, entry_metadata));
404 } 387 }
405 388
389 void SimpleIndex::InsertEntryForTesting(uint64_t entry_hash,
390 const EntryMetadata& entry_metadata) {
391 InsertInEntrySet(entry_hash, entry_metadata, &entries_set_);
392 }
393
406 void SimpleIndex::PostponeWritingToDisk() { 394 void SimpleIndex::PostponeWritingToDisk() {
407 if (!initialized_) 395 if (!initialized_)
408 return; 396 return;
409 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs 397 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs
410 : kWriteToDiskDelayMSecs; 398 : kWriteToDiskDelayMSecs;
411 // If the timer is already active, Start() will just Reset it, postponing it. 399 // If the timer is already active, Start() will just Reset it, postponing it.
412 write_to_disk_timer_.Start( 400 write_to_disk_timer_.Start(
413 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); 401 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_);
414 } 402 }
415 403
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 start - last_write_to_disk_); 512 start - last_write_to_disk_);
525 } 513 }
526 } 514 }
527 last_write_to_disk_ = start; 515 last_write_to_disk_ = start;
528 516
529 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, 517 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start,
530 app_on_background_, base::Closure()); 518 app_on_background_, base::Closure());
531 } 519 }
532 520
533 } // namespace disk_cache 521 } // namespace disk_cache
OLDNEW
« net/disk_cache/disk_cache_perftest.cc ('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