1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright 2022 The Chromium Authors
// 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.h"
#include <limits>
#include "base/feature_list.h"
#include "base/numerics/clamped_math.h"
#include "content/common/features.h"
#include "net/base/url_util.h"
namespace content {
using GetResult = SimpleLruCache::GetResult;
GetResult::GetResult(base::Time response_time, mojo_base::BigBuffer data)
: response_time(response_time), data(std::move(data)) {}
GetResult::~GetResult() = default;
GetResult::GetResult(GetResult&&) = default;
GetResult& GetResult::operator=(GetResult&&) = default;
SimpleLruCache::Value::Value(Age age, base::Time response_time, uint32_t size)
: age(age), response_time(response_time), size(size) {
DCHECK(!base::FeatureList::IsEnabled(features::kInMemoryCodeCache));
}
SimpleLruCache::Value::Value(Age age,
base::Time response_time,
uint32_t size,
base::span<const uint8_t> data)
: age(age),
response_time(response_time),
size(size),
data(data.begin(), data.end()) {
DCHECK(base::FeatureList::IsEnabled(features::kInMemoryCodeCache));
}
SimpleLruCache::Value::~Value() = default;
SimpleLruCache::Value::Value(Value&&) = default;
SimpleLruCache::Value& SimpleLruCache::Value::operator=(Value&&) = default;
SimpleLruCache::SimpleLruCache(uint64_t capacity) : capacity_(capacity) {}
SimpleLruCache::~SimpleLruCache() = default;
std::optional<GetResult> SimpleLruCache::Get(const std::string& key) {
base::Time response_time;
mojo_base::BigBuffer data;
if (!GetInternal(key, &response_time, &data)) {
return std::nullopt;
}
return std::make_optional<GetResult>(response_time, std::move(data));
}
bool SimpleLruCache::Has(const std::string& key) {
return GetInternal(key, /*response_time=*/nullptr, /*data=*/nullptr);
}
void SimpleLruCache::Put(const std::string& key,
base::Time response_time,
base::span<const uint8_t> payload) {
Delete(key);
const uint64_t size = base::ClampedNumeric<uint64_t>(key.size()) +
payload.size() + kEmptyEntrySize;
if (size > capacity_) {
// Ignore a too big entry.
return;
}
const Age age = GetNextAge();
if (base::FeatureList::IsEnabled(features::kInMemoryCodeCache)) {
entries_.emplace(key, Value(age, response_time, size, payload));
} else {
entries_.emplace(key, Value(age, response_time, size));
}
access_list_.emplace(age, std::move(key));
size_ += size;
Evict();
}
void SimpleLruCache::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 SimpleLruCache::GetSize() const {
return size_;
}
void SimpleLruCache::Clear() {
entries_.clear();
access_list_.clear();
size_ = 0;
}
bool SimpleLruCache::GetInternal(const std::string& key,
base::Time* response_time,
mojo_base::BigBuffer* data) {
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);
if (response_time) {
*response_time = it->second.response_time;
}
if (data && base::FeatureList::IsEnabled(features::kInMemoryCodeCache)) {
*data = mojo_base::BigBuffer(it->second.data);
}
return true;
}
void SimpleLruCache::Evict() {
while (capacity_ < size_) {
auto it = access_list_.begin();
CHECK(it != access_list_.end());
DCHECK(entries_.find(it->second) != entries_.end());
Delete(it->second);
}
}
} // namespace content
|