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

Unified Diff: cc/base/rolling_time_delta_history.cc

Issue 2127003002: Switch cc::RollingTimeDeltaHistory to use a vector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: replace modulo with a branch Created 4 years, 5 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..a80ca21fc2e1c20f42e9d171da9c30e8e5e351d8 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"
@@ -11,7 +12,9 @@
namespace cc {
RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size)
- : max_size_(max_size) {}
+ : next_index_(0), max_size_(max_size) {
+ sample_vector_.reserve(max_size_);
+}
RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {}
@@ -19,47 +22,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());
- chronological_sample_deque_.pop_front();
+ if (sample_vector_.size() == max_size_) {
+ sample_vector_[next_index_++] = time;
+ if (next_index_ == max_size_)
+ next_index_ = 0;
+ } else {
+ sample_vector_.push_back(time);
}
-
- TimeDeltaMultiset::iterator it = sample_set_.insert(time);
- chronological_sample_deque_.push_back(it);
}
void RollingTimeDeltaHistory::Clear() {
- chronological_sample_deque_.clear();
- sample_set_.clear();
+ sample_vector_.clear();
danakj 2016/07/12 20:08:43 should we reserve() after this again? it's not cle
dhsharp 2016/07/12 20:25:01 http://en.cppreference.com/w/cpp/container/vector/
+ next_index_ = 0;
}
base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
- if (sample_set_.size() == 0)
+ if (sample_vector_.size() == 0)
return base::TimeDelta();
double fraction = percent / 100.0;
-
if (fraction <= 0.0)
- return *(sample_set_.begin());
+ return *std::min_element(sample_vector_.begin(), sample_vector_.end());
if (fraction >= 1.0)
- return *(sample_set_.rbegin());
+ return *std::max_element(sample_vector_.begin(), sample_vector_.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;
- }
+ static_cast<size_t>(std::ceil(fraction * sample_vector_.size())) - 1;
- TimeDeltaMultiset::const_iterator it = sample_set_.begin();
- for (size_t i = 0; i < num_smaller_samples; i++)
- it++;
- return *it;
+ std::vector<base::TimeDelta> v(sample_vector_.begin(), sample_vector_.end());
+ std::nth_element(v.begin(), v.begin() + num_smaller_samples, v.end());
+ return v[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