This commit is contained in:
Jacob Dufault 2017-12-05 08:24:37 -08:00
parent 905a4ab2b1
commit dbeb4eb511
2 changed files with 12 additions and 11 deletions

View File

@ -18,7 +18,7 @@ std::string GetCachedBaseFileName(Config* config,
size_t len = config->projectRoot.size(); size_t len = config->projectRoot.size();
if (StartsWith(source_file, config->projectRoot)) { if (StartsWith(source_file, config->projectRoot)) {
cache_file = EscapeFileName(config->projectRoot) + '/' + cache_file = EscapeFileName(config->projectRoot) + '/' +
EscapeFileName(source_file.substr(len)); EscapeFileName(source_file.substr(len));
} else } else
cache_file = EscapeFileName(source_file); cache_file = EscapeFileName(source_file);
@ -32,8 +32,8 @@ std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
if (!config->enableCacheRead) if (!config->enableCacheRead)
return nullptr; return nullptr;
optional<std::string> file_content = ReadContent( optional<std::string> file_content =
GetCachedBaseFileName(config, filename) + ".json"); ReadContent(GetCachedBaseFileName(config, filename) + ".json");
if (!file_content) if (!file_content)
return nullptr; return nullptr;
@ -52,8 +52,7 @@ void WriteToCache(Config* config, IndexFile& file) {
if (!config->enableCacheWrite) if (!config->enableCacheWrite)
return; return;
std::string cache_basename = std::string cache_basename = GetCachedBaseFileName(config, file.path);
GetCachedBaseFileName(config, file.path);
if (file.file_contents_.empty()) { if (file.file_contents_.empty()) {
LOG_S(ERROR) << "No cached file contents; performing potentially stale " LOG_S(ERROR) << "No cached file contents; performing potentially stale "

View File

@ -9,7 +9,8 @@ template <typename TKey, typename TValue>
struct LruCache { struct LruCache {
explicit LruCache(int max_entries); explicit LruCache(int max_entries);
// Fetches an entry for |key|. If it does not exist, |allocator| will be invoked to create one. // Fetches an entry for |key|. If it does not exist, |allocator| will be
// invoked to create one.
template <typename TAllocator> template <typename TAllocator>
std::shared_ptr<TValue> Get(const TKey& key, TAllocator allocator); std::shared_ptr<TValue> Get(const TKey& key, TAllocator allocator);
// Fetches the entry for |filename| and updates it's usage so it is less // Fetches the entry for |filename| and updates it's usage so it is less
@ -47,7 +48,8 @@ LruCache<TKey, TValue>::LruCache(int max_entries) : max_entries_(max_entries) {
template <typename TKey, typename TValue> template <typename TKey, typename TValue>
template <typename TAllocator> template <typename TAllocator>
std::shared_ptr<TValue> LruCache<TKey, TValue>::Get(const TKey& key, TAllocator allocator) { std::shared_ptr<TValue> LruCache<TKey, TValue>::Get(const TKey& key,
TAllocator allocator) {
std::shared_ptr<TValue> result = TryGet(key); std::shared_ptr<TValue> result = TryGet(key);
if (!result) if (!result)
Insert(key, result = allocator()); Insert(key, result = allocator());
@ -82,7 +84,8 @@ std::shared_ptr<TValue> LruCache<TKey, TValue>::TryTake(const TKey& key) {
} }
template <typename TKey, typename TValue> template <typename TKey, typename TValue>
void LruCache<TKey, TValue>::Insert(const TKey& key, const std::shared_ptr<TValue>& value) { void LruCache<TKey, TValue>::Insert(const TKey& key,
const std::shared_ptr<TValue>& value) {
if (entries_.size() >= max_entries_) { if (entries_.size() >= max_entries_) {
// Find entry with the lowest score. // Find entry with the lowest score.
size_t lowest_idx = 0; size_t lowest_idx = 0;
@ -113,9 +116,8 @@ void LruCache<TKey, TValue>::IncrementScore() {
// Overflow. // Overflow.
if (next_score_ == 0) { if (next_score_ == 0) {
std::sort(entries_.begin(), entries_.end(), [](const Entry& a, const Entry& b) { std::sort(entries_.begin(), entries_.end(),
return a.score < b.score; [](const Entry& a, const Entry& b) { return a.score < b.score; });
});
for (Entry& entry : entries_) for (Entry& entry : entries_)
entry.score = next_score_++; entry.score = next_score_++;
} }