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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « cc/base/rolling_time_delta_history.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm>
7 #include <cmath> 8 #include <cmath>
8 9
9 #include "cc/base/rolling_time_delta_history.h" 10 #include "cc/base/rolling_time_delta_history.h"
10 11
11 namespace cc { 12 namespace cc {
12 13
13 RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size) 14 RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size)
14 : max_size_(max_size) {} 15 : next_index_(0), max_size_(max_size) {
16 sample_vector_.reserve(max_size_);
17 }
15 18
16 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {} 19 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {}
17 20
18 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) { 21 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) {
19 if (max_size_ == 0) 22 if (max_size_ == 0)
20 return; 23 return;
21 24
22 if (sample_set_.size() == max_size_) { 25 if (sample_vector_.size() == max_size_) {
23 sample_set_.erase(chronological_sample_deque_.front()); 26 sample_vector_[next_index_++] = time;
24 chronological_sample_deque_.pop_front(); 27 if (next_index_ == max_size_)
28 next_index_ = 0;
29 } else {
30 sample_vector_.push_back(time);
25 } 31 }
26
27 TimeDeltaMultiset::iterator it = sample_set_.insert(time);
28 chronological_sample_deque_.push_back(it);
29 } 32 }
30 33
31 void RollingTimeDeltaHistory::Clear() { 34 void RollingTimeDeltaHistory::Clear() {
32 chronological_sample_deque_.clear(); 35 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/
33 sample_set_.clear(); 36 next_index_ = 0;
34 } 37 }
35 38
36 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const { 39 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
37 if (sample_set_.size() == 0) 40 if (sample_vector_.size() == 0)
38 return base::TimeDelta(); 41 return base::TimeDelta();
39 42
40 double fraction = percent / 100.0; 43 double fraction = percent / 100.0;
41
42 if (fraction <= 0.0) 44 if (fraction <= 0.0)
43 return *(sample_set_.begin()); 45 return *std::min_element(sample_vector_.begin(), sample_vector_.end());
44 46
45 if (fraction >= 1.0) 47 if (fraction >= 1.0)
46 return *(sample_set_.rbegin()); 48 return *std::max_element(sample_vector_.begin(), sample_vector_.end());
47 49
48 size_t num_smaller_samples = 50 size_t num_smaller_samples =
49 static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1; 51 static_cast<size_t>(std::ceil(fraction * sample_vector_.size())) - 1;
50 52
51 if (num_smaller_samples > sample_set_.size() / 2) { 53 std::vector<base::TimeDelta> v(sample_vector_.begin(), sample_vector_.end());
52 size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1; 54 std::nth_element(v.begin(), v.begin() + num_smaller_samples, v.end());
53 TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin(); 55 return v[num_smaller_samples];
54 for (size_t i = 0; i < num_larger_samples; i++)
55 it++;
56 return *it;
57 }
58
59 TimeDeltaMultiset::const_iterator it = sample_set_.begin();
60 for (size_t i = 0; i < num_smaller_samples; i++)
61 it++;
62 return *it;
63 } 56 }
64 57
65 } // namespace cc 58 } // namespace cc
OLDNEW
« 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