Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Unified Diff: cc/base/rolling_time_delta_history.cc

Issue 2114203002: Switch cc::RollingTimeDeltaHistory to use a single deque (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/base/rolling_time_delta_history.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/base/rolling_time_delta_history.cc
diff --git a/cc/base/rolling_time_delta_history.cc b/cc/base/rolling_time_delta_history.cc
index e296c4b1d138086b979187fc5c9330fd969c230f..b3cecae1775b7e26b2146989c8b16343910f55e3 100644
--- a/cc/base/rolling_time_delta_history.cc
+++ b/cc/base/rolling_time_delta_history.cc
@@ -4,6 +4,7 @@
#include <stddef.h>
+#include <algorithm>
#include <cmath>
#include "cc/base/rolling_time_delta_history.h"
@@ -19,47 +20,37 @@ void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) {
if (max_size_ == 0)
return;
- if (sample_set_.size() == max_size_) {
- sample_set_.erase(chronological_sample_deque_.front());
+ if (chronological_sample_deque_.size() == max_size_) {
chronological_sample_deque_.pop_front();
}
-
- TimeDeltaMultiset::iterator it = sample_set_.insert(time);
- chronological_sample_deque_.push_back(it);
+ chronological_sample_deque_.push_back(time);
}
void RollingTimeDeltaHistory::Clear() {
chronological_sample_deque_.clear();
- sample_set_.clear();
}
base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
- if (sample_set_.size() == 0)
+ if (chronological_sample_deque_.size() == 0)
return base::TimeDelta();
double fraction = percent / 100.0;
-
if (fraction <= 0.0)
- return *(sample_set_.begin());
+ return *std::min_element(chronological_sample_deque_.begin(),
+ chronological_sample_deque_.end());
if (fraction >= 1.0)
- return *(sample_set_.rbegin());
+ return *std::max_element(chronological_sample_deque_.begin(),
+ chronological_sample_deque_.end());
- size_t num_smaller_samples =
- static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1;
-
- if (num_smaller_samples > sample_set_.size() / 2) {
- size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1;
- TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin();
- for (size_t i = 0; i < num_larger_samples; i++)
- it++;
- return *it;
- }
+ size_t num_smaller_samples = static_cast<size_t>(
+ std::ceil(fraction * chronological_sample_deque_.size() - 1));
+ std::vector<base::TimeDelta> sample_vec(chronological_sample_deque_.begin(),
+ chronological_sample_deque_.end());
- TimeDeltaMultiset::const_iterator it = sample_set_.begin();
- for (size_t i = 0; i < num_smaller_samples; i++)
- it++;
- return *it;
+ std::nth_element(sample_vec.begin(), sample_vec.begin() + num_smaller_samples,
+ sample_vec.end());
+ return sample_vec[num_smaller_samples];
}
} // namespace cc
« no previous file with comments | « cc/base/rolling_time_delta_history.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698