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

Unified Diff: cc/base/rolling_time_delta_history.cc

Issue 2161113004: Revert of Switch cc::RollingTimeDeltaHistory to use a vector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 a80ca21fc2e1c20f42e9d171da9c30e8e5e351d8..e296c4b1d138086b979187fc5c9330fd969c230f 100644
--- a/cc/base/rolling_time_delta_history.cc
+++ b/cc/base/rolling_time_delta_history.cc
@@ -4,7 +4,6 @@
#include <stddef.h>
-#include <algorithm>
#include <cmath>
#include "cc/base/rolling_time_delta_history.h"
@@ -12,9 +11,7 @@
namespace cc {
RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size)
- : next_index_(0), max_size_(max_size) {
- sample_vector_.reserve(max_size_);
-}
+ : max_size_(max_size) {}
RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {}
@@ -22,37 +19,47 @@
if (max_size_ == 0)
return;
- 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);
+ if (sample_set_.size() == max_size_) {
+ sample_set_.erase(chronological_sample_deque_.front());
+ chronological_sample_deque_.pop_front();
}
+
+ TimeDeltaMultiset::iterator it = sample_set_.insert(time);
+ chronological_sample_deque_.push_back(it);
}
void RollingTimeDeltaHistory::Clear() {
- sample_vector_.clear();
- next_index_ = 0;
+ chronological_sample_deque_.clear();
+ sample_set_.clear();
}
base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
- if (sample_vector_.size() == 0)
+ if (sample_set_.size() == 0)
return base::TimeDelta();
double fraction = percent / 100.0;
+
if (fraction <= 0.0)
- return *std::min_element(sample_vector_.begin(), sample_vector_.end());
+ return *(sample_set_.begin());
if (fraction >= 1.0)
- return *std::max_element(sample_vector_.begin(), sample_vector_.end());
+ return *(sample_set_.rbegin());
size_t num_smaller_samples =
- static_cast<size_t>(std::ceil(fraction * sample_vector_.size())) - 1;
+ static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1;
- 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];
+ 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;
+ }
+
+ TimeDeltaMultiset::const_iterator it = sample_set_.begin();
+ for (size_t i = 0; i < num_smaller_samples; i++)
+ it++;
+ return *it;
}
} // 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