From deb2f2d1792c86f041895f576d8410f9b5b093b2 Mon Sep 17 00:00:00 2001 From: K Henriksson Date: Thu, 21 May 2020 13:48:56 -0700 Subject: [PATCH] Simplify copy logic in stats cache sorted_entries fully owns its contents, and there are no reference types contained, so the existing logic wasn't needed. --- src/stats_cache.cc | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/stats_cache.cc b/src/stats_cache.cc index 24d86e22..d58f7164 100644 --- a/src/stats_cache.cc +++ b/src/stats_cache.cc @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -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 sorted_entries; /* Copy all the entries to a vector to be sorted. */ + std::vector sorted_entries; { std::lock_guard 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);