Skip to content

Commit aaa3f32

Browse files
committed
add: review suggestions
Signed-off-by: Ivan Goncharov <[email protected]>
1 parent 9798775 commit aaa3f32

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

prometheus/histogram.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -858,14 +858,21 @@ func (h *histogram) Write(out *dto.Metric) error {
858858
// findBucket returns the index of the bucket for the provided value, or
859859
// len(h.upperBounds) for the +Inf bucket.
860860
func (h *histogram) findBucket(v float64) int {
861-
n := len(h.upperBounds)
862-
863861
// Early exit: if v is less than or equal to the first upper bound, return 0
864862
if v <= h.upperBounds[0] {
865863
return 0
866864
}
867865

866+
n := len(h.upperBounds)
867+
868+
// Early exit: if v is greater than the last upper bound, return len(h.upperBounds)
869+
if v > h.upperBounds[n-1] {
870+
return n
871+
}
872+
868873
// For small arrays, use simple linear search
874+
// "magic number" 35 is result of tests on couple different (AWS and baremetal) servers
875+
// see more details here: https://github.com/prometheus/client_golang/pull/1662
869876
if n < 35 {
870877
for i, bound := range h.upperBounds {
871878
if v <= bound {
@@ -876,12 +883,7 @@ func (h *histogram) findBucket(v float64) int {
876883
return n
877884
}
878885

879-
// For larger arrays, use binary search with early exit optimization
880-
// Early exit: if v is greater than the last upper bound, return len(h.upperBounds)
881-
if v > h.upperBounds[n-1] {
882-
return n
883-
}
884-
886+
// For larger arrays, use stdlib's binary search
885887
return sort.SearchFloat64s(h.upperBounds, v)
886888
}
887889

0 commit comments

Comments
 (0)