Commit bf3ceb19 authored by Weilun Shi's avatar Weilun Shi Committed by Commit Bot

Faster Bucket Lookup For "exact" Linear Histograms

For "exact" linear histograms, the bucket index for Sample |value| can
be easily computed and thus we don't need to do a costly binary search
for the bucket index.

This could have a notable performance improvement given that some
histograms (like TaskCount metrics, done after every queued task) can
see huge numbers of lookups.

the code is already covered by the HistogramFunctionsTest.ExactLinear
test. Without this change, running 1 million times the logging part of
the above test takes 640ms. With this change, it only takes 530ms which
improves ~20% of the bucket lookup performance.

Bug: 950536
Change-Id: I1fa9a1cfd0ccbf1967a678ba82b19f15f90c9848
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2328305Reviewed-by: default avatarBrian White <bcwhite@chromium.org>
Commit-Queue: Brian White <bcwhite@chromium.org>
Auto-Submit: Weilun Shi <sweilun@chromium.org>
Cr-Commit-Position: refs/heads/master@{#793788}
parent abe5e2a8
......@@ -213,14 +213,29 @@ bool SampleVectorBase::AddSubtractImpl(SampleCountIterator* iter,
}
}
// Use simple binary search. This is very general, but there are better
// approaches if we knew that the buckets were linearly distributed.
// Uses simple binary search or calculates the index directly if it's an "exact"
// linear histogram. This is very general, but there are better approaches if we
// knew that the buckets were linearly distributed.
size_t SampleVectorBase::GetBucketIndex(Sample value) const {
size_t bucket_count = bucket_ranges_->bucket_count();
CHECK_GE(bucket_count, 1u);
CHECK_GE(value, bucket_ranges_->range(0));
CHECK_LT(value, bucket_ranges_->range(bucket_count));
// For "exact" linear histograms, e.g. bucket_count = maximum + 1, their
// minimum is 1 and bucket sizes are 1. Thus, we don't need to binary search
// the bucket index. The bucket index for bucket |value| is just the |value|.
Sample maximum = bucket_ranges_->range(bucket_count - 1);
if (maximum == static_cast<Sample>(bucket_count - 1)) {
// |value| is in the underflow bucket.
if (value < 1)
return 0;
// |value| is in the overflow bucket.
if (value > maximum)
return bucket_count - 1;
return static_cast<size_t>(value);
}
size_t under = 0;
size_t over = bucket_count;
size_t mid;
......
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