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

Side by Side 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, 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 : max_size_(max_size) {}
15 16
16 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {} 17 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {}
17 18
18 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) { 19 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) {
19 if (max_size_ == 0) 20 if (max_size_ == 0)
20 return; 21 return;
21 22
22 if (sample_set_.size() == max_size_) { 23 if (chronological_sample_deque_.size() == max_size_) {
23 sample_set_.erase(chronological_sample_deque_.front());
24 chronological_sample_deque_.pop_front(); 24 chronological_sample_deque_.pop_front();
25 } 25 }
26 26 chronological_sample_deque_.push_back(time);
27 TimeDeltaMultiset::iterator it = sample_set_.insert(time);
28 chronological_sample_deque_.push_back(it);
29 } 27 }
30 28
31 void RollingTimeDeltaHistory::Clear() { 29 void RollingTimeDeltaHistory::Clear() {
32 chronological_sample_deque_.clear(); 30 chronological_sample_deque_.clear();
33 sample_set_.clear();
34 } 31 }
35 32
36 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const { 33 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
37 if (sample_set_.size() == 0) 34 if (chronological_sample_deque_.size() == 0)
38 return base::TimeDelta(); 35 return base::TimeDelta();
39 36
40 double fraction = percent / 100.0; 37 double fraction = percent / 100.0;
41
42 if (fraction <= 0.0) 38 if (fraction <= 0.0)
43 return *(sample_set_.begin()); 39 return *std::min_element(chronological_sample_deque_.begin(),
40 chronological_sample_deque_.end());
44 41
45 if (fraction >= 1.0) 42 if (fraction >= 1.0)
46 return *(sample_set_.rbegin()); 43 return *std::max_element(chronological_sample_deque_.begin(),
44 chronological_sample_deque_.end());
47 45
48 size_t num_smaller_samples = 46 size_t num_smaller_samples = static_cast<size_t>(
49 static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1; 47 std::ceil(fraction * chronological_sample_deque_.size() - 1));
48 std::vector<base::TimeDelta> sample_vec(chronological_sample_deque_.begin(),
49 chronological_sample_deque_.end());
50 50
51 if (num_smaller_samples > sample_set_.size() / 2) { 51 std::nth_element(sample_vec.begin(), sample_vec.begin() + num_smaller_samples,
52 size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1; 52 sample_vec.end());
53 TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin(); 53 return sample_vec[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 } 54 }
64 55
65 } // namespace cc 56 } // 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