Commit 5d615d61 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Simplify some code and rely on TimeDelta::operator/() less.

This code can use TimeDelta::operator%() to express the same thing in a
shorter way.  Add a comment on one less-obvious block.

Bug: none
Change-Id: I2d639ee85b4427a78a902cf8b8c1930828132edc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2318967Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Commit-Queue: Andrew Moylan <amoylan@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791688}
parent b2375105
...@@ -44,11 +44,10 @@ void RecentEventsCounter::Log(base::TimeDelta timestamp) { ...@@ -44,11 +44,10 @@ void RecentEventsCounter::Log(base::TimeDelta timestamp) {
} }
first_bucket_index_ = (bucket_index + 1) % num_buckets_; first_bucket_index_ = (bucket_index + 1) % num_buckets_;
int num_cycles = floor(timestamp / duration_); // Move the first bucket time such that |bucket_index| is the last bucket in
base::TimeDelta cycle_start = num_cycles * duration_; // the period [first_bucket_time_, first_bucket_time_ + duration_).
int extra_buckets = floor((timestamp - cycle_start) / bucket_duration_); first_bucket_time_ =
first_bucket_time_ = cycle_start + extra_buckets * bucket_duration_ + timestamp - (timestamp % bucket_duration_) + bucket_duration_ - duration_;
bucket_duration_ - duration_;
} }
int RecentEventsCounter::GetTotal(base::TimeDelta now) { int RecentEventsCounter::GetTotal(base::TimeDelta now) {
...@@ -73,9 +72,7 @@ int RecentEventsCounter::GetTotal(base::TimeDelta now) { ...@@ -73,9 +72,7 @@ int RecentEventsCounter::GetTotal(base::TimeDelta now) {
int RecentEventsCounter::GetBucketIndex(base::TimeDelta timestamp) const { int RecentEventsCounter::GetBucketIndex(base::TimeDelta timestamp) const {
DCHECK_GE(timestamp, base::TimeDelta()); DCHECK_GE(timestamp, base::TimeDelta());
int num_cycles = floor(timestamp / duration_); int index = (timestamp % duration_) / bucket_duration_;
base::TimeDelta cycle_start = num_cycles * duration_;
int index = floor((timestamp - cycle_start) / bucket_duration_);
if (index >= num_buckets_) { if (index >= num_buckets_) {
return num_buckets_ - 1; return num_buckets_ - 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment