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

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

Issue 2922973003: RFC: use some in-memory state in SimpleCache to quickly cache-miss some CantConditionalize cases
Patch Set: cleanup naming in SimpleCache impl of this some Created 3 years, 6 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 namespace disk_cache { 54 namespace disk_cache {
55 55
56 EntryMetadata::EntryMetadata() 56 EntryMetadata::EntryMetadata()
57 : last_used_time_seconds_since_epoch_(0), 57 : last_used_time_seconds_since_epoch_(0),
58 entry_size_(0) { 58 entry_size_(0) {
59 } 59 }
60 60
61 EntryMetadata::EntryMetadata(base::Time last_used_time, 61 EntryMetadata::EntryMetadata(base::Time last_used_time,
62 base::StrictNumeric<uint32_t> entry_size) 62 base::StrictNumeric<uint32_t> entry_size)
63 : last_used_time_seconds_since_epoch_(0), entry_size_(entry_size) { 63 : last_used_time_seconds_since_epoch_(0), entry_size_(0) {
64 SetEntrySize(entry_size); // to round/pack properly.
64 SetLastUsedTime(last_used_time); 65 SetLastUsedTime(last_used_time);
65 } 66 }
66 67
67 base::Time EntryMetadata::GetLastUsedTime() const { 68 base::Time EntryMetadata::GetLastUsedTime() const {
68 // Preserve nullity. 69 // Preserve nullity.
69 if (last_used_time_seconds_since_epoch_ == 0) 70 if (last_used_time_seconds_since_epoch_ == 0)
70 return base::Time(); 71 return base::Time();
71 72
72 return base::Time::UnixEpoch() + 73 return base::Time::UnixEpoch() +
73 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_); 74 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_);
74 } 75 }
75 76
76 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { 77 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) {
77 // Preserve nullity. 78 // Preserve nullity.
78 if (last_used_time.is_null()) { 79 if (last_used_time.is_null()) {
79 last_used_time_seconds_since_epoch_ = 0; 80 last_used_time_seconds_since_epoch_ = 0;
80 return; 81 return;
81 } 82 }
82 83
83 last_used_time_seconds_since_epoch_ = base::saturated_cast<uint32_t>( 84 last_used_time_seconds_since_epoch_ = base::saturated_cast<uint32_t>(
84 (last_used_time - base::Time::UnixEpoch()).InSeconds()); 85 (last_used_time - base::Time::UnixEpoch()).InSeconds());
85 // Avoid accidental nullity. 86 // Avoid accidental nullity.
86 if (last_used_time_seconds_since_epoch_ == 0) 87 if (last_used_time_seconds_since_epoch_ == 0)
87 last_used_time_seconds_since_epoch_ = 1; 88 last_used_time_seconds_since_epoch_ = 1;
88 } 89 }
89 90
90 uint32_t EntryMetadata::GetEntrySize() const { 91 uint32_t EntryMetadata::GetEntrySize() const {
91 return entry_size_; 92 return entry_size_ << 8;
92 } 93 }
93 94
94 void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { 95 void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) {
95 entry_size_ = entry_size; 96 // ### what happens if we overflow here?
97 entry_size_ = (static_cast<uint32_t>(entry_size) + 255) >> 8;
96 } 98 }
97 99
98 void EntryMetadata::Serialize(base::Pickle* pickle) const { 100 void EntryMetadata::Serialize(base::Pickle* pickle) const {
99 DCHECK(pickle); 101 DCHECK(pickle);
100 int64_t internal_last_used_time = GetLastUsedTime().ToInternalValue(); 102 int64_t internal_last_used_time = GetLastUsedTime().ToInternalValue();
101 // If you modify the size of the size of the pickle, be sure to update 103 // If you modify the size of the size of the pickle, be sure to update
102 // kOnDiskSizeBytes. 104 // kOnDiskSizeBytes.
103 pickle->WriteInt64(internal_last_used_time); 105 pickle->WriteInt64(internal_last_used_time);
104 pickle->WriteUInt64(entry_size_); 106 pickle->WriteUInt64(entry_size_);
105 } 107 }
106 108
107 bool EntryMetadata::Deserialize(base::PickleIterator* it) { 109 bool EntryMetadata::Deserialize(base::PickleIterator* it,
110 bool has_memory_entry_data) {
108 DCHECK(it); 111 DCHECK(it);
109 int64_t tmp_last_used_time; 112 int64_t tmp_last_used_time;
110 uint64_t tmp_entry_size; 113 uint64_t tmp_entry_size;
111 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size) || 114 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size) ||
112 tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max()) 115 tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max())
113 return false; 116 return false;
114 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); 117 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time));
115 entry_size_ = static_cast<uint32_t>(tmp_entry_size); 118 if (has_memory_entry_data) {
119 // tmp_entry_size actually packs entry_size_ and memory_entry_data_.
120 SetEntrySize(static_cast<uint32_t>(tmp_entry_size & 0xFFFFFF00));
121 SetMemoryEntryData(static_cast<uint8_t>(tmp_entry_size & 0xFF));
122 } else {
123 SetEntrySize(static_cast<uint32_t>(tmp_entry_size));
124 SetMemoryEntryData(0);
125 }
116 return true; 126 return true;
117 } 127 }
118 128
119 SimpleIndex::SimpleIndex( 129 SimpleIndex::SimpleIndex(
120 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, 130 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread,
121 SimpleIndexDelegate* delegate, 131 SimpleIndexDelegate* delegate,
122 net::CacheType cache_type, 132 net::CacheType cache_type,
123 std::unique_ptr<SimpleIndexFile> index_file) 133 std::unique_ptr<SimpleIndexFile> index_file)
124 : delegate_(delegate), 134 : delegate_(delegate),
125 cache_type_(cache_type), 135 cache_type_(cache_type),
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 removed_entries_.insert(entry_hash); 284 removed_entries_.insert(entry_hash);
275 PostponeWritingToDisk(); 285 PostponeWritingToDisk();
276 } 286 }
277 287
278 bool SimpleIndex::Has(uint64_t hash) const { 288 bool SimpleIndex::Has(uint64_t hash) const {
279 DCHECK(io_thread_checker_.CalledOnValidThread()); 289 DCHECK(io_thread_checker_.CalledOnValidThread());
280 // If not initialized, always return true, forcing it to go to the disk. 290 // If not initialized, always return true, forcing it to go to the disk.
281 return !initialized_ || entries_set_.count(hash) > 0; 291 return !initialized_ || entries_set_.count(hash) > 0;
282 } 292 }
283 293
294 uint8_t SimpleIndex::GetMemoryEntryData(uint64_t entry_hash) const {
295 DCHECK(io_thread_checker_.CalledOnValidThread());
296 EntrySet::const_iterator it = entries_set_.find(entry_hash);
297 if (it == entries_set_.end())
298 return 0;
299 return it->second.GetMemoryEntryData();
300 }
301
302 void SimpleIndex::SetMemoryEntryData(uint64_t entry_hash, uint8_t value) {
303 DCHECK(io_thread_checker_.CalledOnValidThread());
304 EntrySet::iterator it = entries_set_.find(entry_hash);
305 if (it == entries_set_.end())
306 return;
307 return it->second.SetMemoryEntryData(value);
308 }
309
284 bool SimpleIndex::UseIfExists(uint64_t entry_hash) { 310 bool SimpleIndex::UseIfExists(uint64_t entry_hash) {
285 DCHECK(io_thread_checker_.CalledOnValidThread()); 311 DCHECK(io_thread_checker_.CalledOnValidThread());
286 // Always update the last used time, even if it is during initialization. 312 // Always update the last used time, even if it is during initialization.
287 // It will be merged later. 313 // It will be merged later.
288 EntrySet::iterator it = entries_set_.find(entry_hash); 314 EntrySet::iterator it = entries_set_.find(entry_hash);
289 if (it == entries_set_.end()) 315 if (it == entries_set_.end())
290 // If not initialized, always return true, forcing it to go to the disk. 316 // If not initialized, always return true, forcing it to go to the disk.
291 return !initialized_; 317 return !initialized_;
292 it->second.SetLastUsedTime(base::Time::Now()); 318 it->second.SetLastUsedTime(base::Time::Now());
293 PostponeWritingToDisk(); 319 PostponeWritingToDisk();
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); 429 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_);
404 } 430 }
405 431
406 void SimpleIndex::UpdateEntryIteratorSize( 432 void SimpleIndex::UpdateEntryIteratorSize(
407 EntrySet::iterator* it, 433 EntrySet::iterator* it,
408 base::StrictNumeric<uint32_t> entry_size) { 434 base::StrictNumeric<uint32_t> entry_size) {
409 // Update the total cache size with the new entry size. 435 // Update the total cache size with the new entry size.
410 DCHECK(io_thread_checker_.CalledOnValidThread()); 436 DCHECK(io_thread_checker_.CalledOnValidThread());
411 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); 437 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize());
412 cache_size_ -= (*it)->second.GetEntrySize(); 438 cache_size_ -= (*it)->second.GetEntrySize();
413 cache_size_ += static_cast<uint32_t>(entry_size);
414 (*it)->second.SetEntrySize(entry_size); 439 (*it)->second.SetEntrySize(entry_size);
440 // We use GetEntrySize to get consistent rounding.
441 cache_size_ += (*it)->second.GetEntrySize();
415 } 442 }
416 443
417 void SimpleIndex::MergeInitializingSet( 444 void SimpleIndex::MergeInitializingSet(
418 std::unique_ptr<SimpleIndexLoadResult> load_result) { 445 std::unique_ptr<SimpleIndexLoadResult> load_result) {
419 DCHECK(io_thread_checker_.CalledOnValidThread()); 446 DCHECK(io_thread_checker_.CalledOnValidThread());
420 DCHECK(load_result->did_load); 447 DCHECK(load_result->did_load);
421 448
422 EntrySet* index_file_entries = &load_result->entries; 449 EntrySet* index_file_entries = &load_result->entries;
423 450
424 for (std::unordered_set<uint64_t>::const_iterator it = 451 for (std::unordered_set<uint64_t>::const_iterator it =
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 start - last_write_to_disk_); 541 start - last_write_to_disk_);
515 } 542 }
516 } 543 }
517 last_write_to_disk_ = start; 544 last_write_to_disk_ = start;
518 545
519 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, 546 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start,
520 app_on_background_, base::Closure()); 547 app_on_background_, base::Closure());
521 } 548 }
522 549
523 } // namespace disk_cache 550 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698