From 16e4558792b533504860932f6efa63afe80eef83 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 28 Feb 2021 17:39:11 +0200 Subject: [PATCH] Move bucket index calculations to the place where they are used This can help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1096 --- histogram.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/histogram.go b/histogram.go index a424c5d..ebdd00d 100644 --- a/histogram.go +++ b/histogram.go @@ -86,14 +86,6 @@ func (h *Histogram) Update(v float64) { return } bucketIdx := (math.Log10(v) - e10Min) * bucketsPerDecimal - idx := uint(bucketIdx) - if bucketIdx == float64(idx) { - // Edge case for 10^n values, which must go to the lower bucket - // according to Prometheus logic for `le`-based histograms. - idx-- - } - decimalBucketIdx := idx / bucketsPerDecimal - offset := idx % bucketsPerDecimal h.mu.Lock() h.sum += v if bucketIdx < 0 { @@ -101,6 +93,14 @@ func (h *Histogram) Update(v float64) { } else if bucketIdx >= bucketsCount { h.upper++ } else { + idx := uint(bucketIdx) + if bucketIdx == float64(idx) { + // Edge case for 10^n values, which must go to the lower bucket + // according to Prometheus logic for `le`-based histograms. + idx-- + } + decimalBucketIdx := idx / bucketsPerDecimal + offset := idx % bucketsPerDecimal db := h.decimalBuckets[decimalBucketIdx] if db == nil { var b [bucketsPerDecimal]uint64