-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Estimate the potential performance impact for memory-backed code cache
Add UMA histograms to measure the potential performance gain. Change-Id: I3b035137c2b617fe5c944b3c5bf9b8b88458f126 Bug: 1324058 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3634940 Reviewed-by: Leszek Swirski <leszeks@chromium.org> Commit-Queue: Yutaka Hirano <yhirano@chromium.org> Reviewed-by: Takashi Toyoshima <toyoshim@chromium.org> Reviewed-by: Kouhei Ueno <kouhei@chromium.org> Reviewed-by: Avi Drissman <avi@chromium.org> Reviewed-by: Kenichi Ishibashi <bashi@chromium.org> Cr-Commit-Position: refs/heads/main@{#1002412}
- Loading branch information
1 parent
d9f9663
commit 3550a8f
Showing
8 changed files
with
386 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/code_cache/simple_lru_cache_index.h" | ||
|
||
#include <limits> | ||
|
||
#include "net/base/url_util.h" | ||
|
||
namespace content { | ||
|
||
SimpleLruCacheIndex::SimpleLruCacheIndex(uint64_t capacity) | ||
: capacity_(capacity) {} | ||
SimpleLruCacheIndex::~SimpleLruCacheIndex() = default; | ||
|
||
bool SimpleLruCacheIndex::Get(const std::string& key) { | ||
const auto it = entries_.find(key); | ||
if (it == entries_.end()) { | ||
return false; | ||
} | ||
const Age age = GetNextAge(); | ||
access_list_.erase(it->second.age); | ||
it->second.age = age; | ||
access_list_.emplace(age, it->first); | ||
return true; | ||
} | ||
|
||
void SimpleLruCacheIndex::Put(const std::string& key, uint32_t payload_size) { | ||
Delete(key); | ||
|
||
const Age age = GetNextAge(); | ||
const uint32_t size = | ||
std::min(payload_size, | ||
std::numeric_limits<uint32_t>::max() - kEmptyEntrySize) + | ||
kEmptyEntrySize; | ||
|
||
entries_.emplace(key, Value(age, size)); | ||
access_list_.emplace(age, std::move(key)); | ||
size_ += size; | ||
Evict(); | ||
} | ||
|
||
void SimpleLruCacheIndex::Delete(const std::string& key) { | ||
const auto it = entries_.find(key); | ||
if (it == entries_.end()) { | ||
return; | ||
} | ||
|
||
DCHECK_GE(size_, it->second.size); | ||
size_ -= it->second.size; | ||
access_list_.erase(it->second.age); | ||
entries_.erase(it); | ||
} | ||
|
||
uint64_t SimpleLruCacheIndex::GetSize() const { | ||
return size_; | ||
} | ||
|
||
void SimpleLruCacheIndex::Evict() { | ||
while (capacity_ < size_) { | ||
auto it = access_list_.begin(); | ||
DCHECK(it != access_list_.end()); | ||
DCHECK(entries_.find(it->second) != entries_.end()); | ||
|
||
Delete(it->second); | ||
} | ||
} | ||
|
||
} // namespace content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CONTENT_BROWSER_CODE_CACHE_SIMPLE_LRU_CACHE_INDEX_H_ | ||
#define CONTENT_BROWSER_CODE_CACHE_SIMPLE_LRU_CACHE_INDEX_H_ | ||
|
||
#include <stdint.h> | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include "base/types/strong_alias.h" | ||
#include "content/common/content_export.h" | ||
|
||
namespace content { | ||
|
||
// A simple LRU cache index, to measure the potential performance impact of | ||
// memory-backed code cache. | ||
class CONTENT_EXPORT SimpleLruCacheIndex { | ||
public: | ||
explicit SimpleLruCacheIndex(uint64_t capacity); | ||
~SimpleLruCacheIndex(); | ||
|
||
SimpleLruCacheIndex(const SimpleLruCacheIndex&) = delete; | ||
SimpleLruCacheIndex& operator=(const SimpleLruCacheIndex&) = delete; | ||
|
||
// Returns whether an entry for `key` exists in the cache. | ||
bool Get(const std::string& key); | ||
// Puts an entry whose payload size is `size` to the cache. | ||
void Put(const std::string& key, uint32_t size); | ||
// Deletes an entry for `key` in the cache. If there is no such an entry, this | ||
// does nothing. | ||
void Delete(const std::string& key); | ||
// Returns the total size of the cache. | ||
uint64_t GetSize() const; | ||
|
||
static constexpr uint32_t kEmptyEntrySize = 2048; | ||
|
||
private: | ||
using Age = base::StrongAlias<class AgeTag, uint32_t>; | ||
using Key = std::string; | ||
struct Value { | ||
Value(Age age, uint32_t size) : age(age), size(size) {} | ||
|
||
Age age; | ||
uint32_t size; | ||
}; | ||
|
||
Age GetNextAge() { return Age(age_source_++); } | ||
void Evict(); | ||
|
||
const uint64_t capacity_; | ||
std::map<Key, Value> entries_; | ||
std::map<Age, Key> access_list_; | ||
uint32_t age_source_ = 0; | ||
uint64_t size_ = 0; | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_BROWSER_CODE_CACHE_SIMPLE_LRU_CACHE_INDEX_H_ |
Oops, something went wrong.