Commit 2f261d21 authored by Jinsong Fan's avatar Jinsong Fan Committed by Commit Bot

dns: Check if the cache is full during restoring from the old

During restoring from the old cache, RestoreFromListValue always traverses
all entries and sets the size of the entries to restore_size_.
The CL adds check if the cache is full during restoring for performance,
and sets restore_size_ to the number of cache entries that were restored.


Change-Id: I40f33f326e8aa92a2b8897879c652b8b3b41e825
Reviewed-on: https://chromium-review.googlesource.com/c/1369747
Commit-Queue: Paul Jensen <pauljensen@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615921}
parent d4f60691
...@@ -524,7 +524,15 @@ void HostCache::GetAsListValue(base::ListValue* entry_list, ...@@ -524,7 +524,15 @@ void HostCache::GetAsListValue(base::ListValue* entry_list,
} }
bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) { bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) {
// Reset the restore size to 0.
restore_size_ = 0;
for (auto it = old_cache.begin(); it != old_cache.end(); it++) { for (auto it = old_cache.begin(); it != old_cache.end(); it++) {
// If the cache is already full, don't bother prioritizing what to evict,
// just stop restoring.
if (size() == max_entries_)
break;
const base::DictionaryValue* entry_dict; const base::DictionaryValue* entry_dict;
if (!it->GetAsDictionary(&entry_dict)) if (!it->GetAsDictionary(&entry_dict))
return false; return false;
...@@ -634,16 +642,15 @@ bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) { ...@@ -634,16 +642,15 @@ bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) {
static_cast<HostResolverSource>(host_resolver_source)); static_cast<HostResolverSource>(host_resolver_source));
// If the key is already in the cache, assume it's more recent and don't // If the key is already in the cache, assume it's more recent and don't
// replace the entry. If the cache is already full, don't bother // replace the entry.
// prioritizing what to evict, just stop restoring.
auto found = entries_.find(key); auto found = entries_.find(key);
if (found == entries_.end() && size() < max_entries_) { if (found == entries_.end()) {
AddEntry(key, Entry(error, address_list, std::move(text_records), AddEntry(key, Entry(error, address_list, std::move(text_records),
std::move(hostname_records), Entry::SOURCE_UNKNOWN, std::move(hostname_records), Entry::SOURCE_UNKNOWN,
expiration_time, network_changes_ - 1)); expiration_time, network_changes_ - 1));
restore_size_++;
} }
} }
restore_size_ = old_cache.GetSize();
return true; return true;
} }
......
...@@ -733,7 +733,7 @@ TEST(HostCacheTest, SerializeAndDeserialize) { ...@@ -733,7 +733,7 @@ TEST(HostCacheTest, SerializeAndDeserialize) {
EXPECT_EQ(1u, result4->addresses().value().size()); EXPECT_EQ(1u, result4->addresses().value().size());
EXPECT_EQ(address_ipv4, result4->addresses().value().front().address()); EXPECT_EQ(address_ipv4, result4->addresses().value().front().address());
EXPECT_EQ(3u, restored_cache.last_restore_size()); EXPECT_EQ(2u, restored_cache.last_restore_size());
} }
TEST(HostCacheTest, SerializeAndDeserialize_Text) { TEST(HostCacheTest, SerializeAndDeserialize_Text) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment