Skip to content

Commit

Permalink
Simplify copy logic in stats cache
Browse files Browse the repository at this point in the history
sorted_entries fully owns its contents, and there are no reference types
contained, so the existing logic wasn't needed.
  • Loading branch information
khenriks committed May 23, 2020
1 parent 70802fd commit deb2f2d
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/stats_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <algorithm>
#include <cerrno>
#include <iterator>
#include <ostream>
#include <vector>

Expand Down Expand Up @@ -93,18 +94,13 @@ bool StatsCache::cmp_by_atime(const StatsCache::cache_entry_t& a1,
void StatsCache::prune() {
Log(DEBUG) << "Pruning stats cache";
const size_t target_size = 9 * params.statcachesize / 10; // 90% NOLINT
std::vector<cache_entry_t> sorted_entries;

/* Copy all the entries to a vector to be sorted. */
std::vector<cache_entry_t> sorted_entries;
{
std::lock_guard<std::mutex> l(mutex_);
sorted_entries.reserve(cache_.size());
for (const auto& entry : cache_) {
/* Force a true copy of the string to prevent multithreading issues.
*/
std::string file(entry.first);
sorted_entries.emplace_back(std::make_pair(file, entry.second));
}
std::copy(cache_.begin(), cache_.end(),
std::back_inserter(sorted_entries));
}
/* Sort the entries by access time, with the oldest first */
std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_by_atime);
Expand Down

0 comments on commit deb2f2d

Please sign in to comment.