mirror of
https://github.com/prometheus/prometheus.git
synced 2026-05-04 20:06:12 +02:00
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:
parent
277d3c9009
commit
45afb1d06e
@ -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.
|
||||
|
||||
36
util/kahansum/kahansum_test.go
Normal file
36
util/kahansum/kahansum_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user