@@ -858,14 +858,21 @@ func (h *histogram) Write(out *dto.Metric) error {
858
858
// findBucket returns the index of the bucket for the provided value, or
859
859
// len(h.upperBounds) for the +Inf bucket.
860
860
func (h * histogram ) findBucket (v float64 ) int {
861
- n := len (h .upperBounds )
862
-
863
861
// Early exit: if v is less than or equal to the first upper bound, return 0
864
862
if v <= h .upperBounds [0 ] {
865
863
return 0
866
864
}
867
865
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
+
868
873
// 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
869
876
if n < 35 {
870
877
for i , bound := range h .upperBounds {
871
878
if v <= bound {
@@ -876,12 +883,7 @@ func (h *histogram) findBucket(v float64) int {
876
883
return n
877
884
}
878
885
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
885
887
return sort .SearchFloat64s (h .upperBounds , v )
886
888
}
887
889
0 commit comments