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

Side by Side 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, 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
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 least recently used hashes into |entry_hashes|.
334 std::vector<uint64_t> entry_hashes; 327 std::vector<uint64_t> entry_hashes;
335 entry_hashes.reserve(entries_set_.size()); 328 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it =
336 for (EntrySet::const_iterator it = entries_set_.begin(), 329 entries.begin();
337 end = entries_set_.end(); it != end; ++it) {
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 30 matching lines...) Expand all
396 378
397 // static 379 // static
398 void SimpleIndex::InsertInEntrySet( 380 void SimpleIndex::InsertInEntrySet(
399 uint64_t entry_hash, 381 uint64_t entry_hash,
400 const disk_cache::EntryMetadata& entry_metadata, 382 const disk_cache::EntryMetadata& entry_metadata,
401 EntrySet* entry_set) { 383 EntrySet* entry_set) {
402 DCHECK(entry_set); 384 DCHECK(entry_set);
403 entry_set->insert(std::make_pair(entry_hash, entry_metadata)); 385 entry_set->insert(std::make_pair(entry_hash, entry_metadata));
404 } 386 }
405 387
388 void SimpleIndex::InsertEntryForTesting(uint64_t entry_hash,
389 const EntryMetadata& entry_metadata) {
390 DCHECK(entries_set_.find(entry_hash) == entries_set_.end());
391 InsertInEntrySet(entry_hash, entry_metadata, &entries_set_);
392 cache_size_ += entry_metadata.GetEntrySize();
393 }
394
406 void SimpleIndex::PostponeWritingToDisk() { 395 void SimpleIndex::PostponeWritingToDisk() {
407 if (!initialized_) 396 if (!initialized_)
408 return; 397 return;
409 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs 398 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs
410 : kWriteToDiskDelayMSecs; 399 : kWriteToDiskDelayMSecs;
411 // If the timer is already active, Start() will just Reset it, postponing it. 400 // If the timer is already active, Start() will just Reset it, postponing it.
412 write_to_disk_timer_.Start( 401 write_to_disk_timer_.Start(
413 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); 402 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_);
414 } 403 }
415 404
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 start - last_write_to_disk_); 513 start - last_write_to_disk_);
525 } 514 }
526 } 515 }
527 last_write_to_disk_ = start; 516 last_write_to_disk_ = start;
528 517
529 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, 518 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start,
530 app_on_background_, base::Closure()); 519 app_on_background_, base::Closure());
531 } 520 }
532 521
533 } // namespace disk_cache 522 } // namespace disk_cache
OLDNEW
« 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