From 97b30dee5bbcdbaaaa9227b0efe6407d32adac2b Mon Sep 17 00:00:00 2001 From: leipeng Date: Thu, 14 Oct 2021 14:44:15 -0700 Subject: [PATCH] improve-histogram-performance: remove valueIndexMap_ (#8625) Summary: `valueIndexMap_` in histogram is redundant and search in `valueIndexMap_` is slower than search in `bucketValues_`. this PR delete `valueIndexMap_` and search in `bucketValues_` by `std::lower_bound` Pull Request resolved: https://github.com/facebook/rocksdb/pull/8625 Reviewed By: zhichao-cao Differential Revision: D31613386 Pulled By: ajkr fbshipit-source-id: d7415d724f5c8f41f80cbe82afd7467cfad6f009 --- monitoring/histogram.cc | 23 ++++++++--------------- monitoring/histogram.h | 1 - 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/monitoring/histogram.cc b/monitoring/histogram.cc index 03268b4a4..c6ef856b0 100644 --- a/monitoring/histogram.cc +++ b/monitoring/histogram.cc @@ -10,6 +10,8 @@ #include "monitoring/histogram.h" #include + +#include #include #include #include @@ -23,7 +25,6 @@ HistogramBucketMapper::HistogramBucketMapper() { // If you change this, you also need to change // size of array buckets_ in HistogramImpl bucketValues_ = {1, 2}; - valueIndexMap_ = {{1, 0}, {2, 1}}; double bucket_val = static_cast(bucketValues_.back()); while ((bucket_val = 1.5 * bucket_val) <= static_cast(port::kMaxUint64)) { bucketValues_.push_back(static_cast(bucket_val)); @@ -35,26 +36,18 @@ HistogramBucketMapper::HistogramBucketMapper() { pow_of_ten *= 10; } bucketValues_.back() *= pow_of_ten; - valueIndexMap_[bucketValues_.back()] = bucketValues_.size() - 1; } maxBucketValue_ = bucketValues_.back(); minBucketValue_ = bucketValues_.front(); } size_t HistogramBucketMapper::IndexForValue(const uint64_t value) const { - if (value >= maxBucketValue_) { - return bucketValues_.size() - 1; - } else if ( value >= minBucketValue_ ) { - std::map::const_iterator lowerBound = - valueIndexMap_.lower_bound(value); - if (lowerBound != valueIndexMap_.end()) { - return static_cast(lowerBound->second); - } else { - return 0; - } - } else { - return 0; - } + auto beg = bucketValues_.begin(); + auto end = bucketValues_.end(); + if (value >= maxBucketValue_) + return end - beg - 1; // bucketValues_.size() - 1 + else + return std::lower_bound(beg, end, value) - beg; } namespace { diff --git a/monitoring/histogram.h b/monitoring/histogram.h index a6b93e8fd..6d72b0651 100644 --- a/monitoring/histogram.h +++ b/monitoring/histogram.h @@ -48,7 +48,6 @@ class HistogramBucketMapper { std::vector bucketValues_; uint64_t maxBucketValue_; uint64_t minBucketValue_; - std::map valueIndexMap_; }; struct HistogramStat {