Optimize the performance of data merge: decimal.CalibrateScale() (#5672)

* Optimize the performance of data merge: decimal.CalibrateScale() from 49633 ns/op to 9146 ns/op

* Optimize the performance of data merge: decimal.CalibrateScale()
This commit is contained in:
Fuchun Zhang 2024-01-27 07:05:04 +08:00 committed by Aliaksandr Valialkin
parent 1a6c3370bf
commit 64780f4f02
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -7,6 +7,20 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fastnum"
)
const tableLen = 32767
var table = func() []int64 {
out := make([]int64, 0, tableLen)
var cur int64 = 10
for i := 1; i <= tableLen; i++ {
out = append(out, cur)
if cur*10 > 0 {
cur *= 10
}
}
return out
}()
// CalibrateScale calibrates a and b with the corresponding exponents ae, be
// and returns the resulting exponent e.
func CalibrateScale(a []int64, ae int16, b []int64, be int16) (e int16) {
@ -35,30 +49,24 @@ func CalibrateScale(a []int64, ae int16, b []int64, be int16) (e int16) {
}
}
upExp -= downExp
for i, v := range a {
if isSpecialValue(v) {
// Do not take into account special values.
continue
if upExp > 0 {
times := table[upExp-1]
for i, v := range a {
if isSpecialValue(v) {
// Do not take into account special values.
continue
}
a[i] = v * times
}
adjExp := upExp
for adjExp > 0 {
v *= 10
adjExp--
}
a[i] = v
}
if downExp > 0 {
times := table[downExp-1]
for i, v := range b {
if isSpecialValue(v) {
// Do not take into account special values.
continue
}
adjExp := downExp
for adjExp > 0 {
v /= 10
adjExp--
}
b[i] = v
b[i] = v / times
}
}
return be + downExp