perf(PromQL): make floatHistogram.KahanAdd inlineable on Go 1.26 (#18568)

* perf(PromQL): make kahansum.Inc inlineable on Go 1.26

Signed-off-by: linasm <linas.medziunas@gmail.com>
Signed-off-by: Linas Medžiūnas <linasm@users.noreply.github.com>
Co-authored-by: George Krajcsovits <krajorama@users.noreply.github.com>
This commit is contained in:
Linas Medžiūnas 2026-04-27 09:43:13 +03:00 committed by GitHub
parent 277d3c9009
commit 45afb1d06e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 1 deletions

View File

@ -15,6 +15,12 @@ package kahansum
import "math"
// isInf reports whether f is positive or negative infinity.
// It avoids math.IsInf to prevent inflating Inc's inlining cost.
func isInf(f float64) bool {
return f > math.MaxFloat64 || f < -math.MaxFloat64
}
// Inc performs addition of two floating-point numbers using the Kahan summation algorithm.
func Inc(inc, sum, c float64) (newSum, newC float64) {
// We've seen Kahan summation return less accurate results when Inc function is
@ -33,7 +39,7 @@ func Inc(inc, sum, c float64) (newSum, newC float64) {
t := sum + inc
switch {
case math.IsInf(t, 0):
case isInf(t):
c = 0
// Using Neumaier improvement, swap if next term larger than sum.

View File

@ -0,0 +1,36 @@
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kahansum
import (
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestIsInf(t *testing.T) {
for _, f := range []float64{
math.Inf(1),
math.Inf(-1),
math.MaxFloat64,
-math.MaxFloat64,
1.0,
-1.0,
0,
math.NaN(),
} {
require.Equal(t, math.IsInf(f, 0), isInf(f), "%e", f)
}
}