Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apd: Improve SetFloat64 efficiency #129

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ func BenchmarkDecimalString(b *testing.B) {
_ = corpus[rng.Intn(len(corpus))].String()
}
}

func BenchmarkDecimalSetFloat(b *testing.B) {
rng := rand.New(rand.NewSource(461210934723948))
corpus := func() []float64 {
res := make([]float64, 8192)
for i := range res {
res[i] = rng.ExpFloat64()
}
return res
}()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var d Decimal
_, err := d.SetFloat64(corpus[rng.Intn(len(corpus))])
if err != nil {
b.Fatal(err)
}
}
}
70 changes: 34 additions & 36 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Decimal is an arbitrary-precision decimal. Its value is:
//
// Negative × Coeff × 10**Exponent
// Negative × Coeff × 10**Exponent
//
// Coeff must be positive. If it is negative results may be incorrect and
// apd may panic.
Expand Down Expand Up @@ -198,6 +198,7 @@ func (c *Context) SetString(d *Decimal, s string) (*Decimal, Condition, error) {
}

// Set sets d's fields to the values of x and returns d.
//
//gcassert:inline
func (d *Decimal) Set(x *Decimal) *Decimal {
if d == x {
Expand Down Expand Up @@ -241,7 +242,8 @@ func (d *Decimal) setCoefficient(x int64) {
// SetFloat64 sets d's Coefficient and Exponent to x and returns d. d will
// hold the exact value of f.
func (d *Decimal) SetFloat64(f float64) (*Decimal, error) {
_, _, err := d.SetString(strconv.FormatFloat(f, 'E', -1, 64))
var buf [32]byte // Avoid most of the allocations in strconv.
_, _, err := d.SetString(string(strconv.AppendFloat(buf[:0], f, 'E', -1, 64)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The []byte to string allocation is unfortunate. Do you want to throw the following into an unsafe.go file and use it here?

// Copyright 2023 The Cockroach 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 apd

import "unsafe"

func unsafeConvertBytesToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you looked at it before I backed out of this approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by that? You had the unsafe cast and then removed it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kinda strange... I think if you look at the string converstion that's done inside bytes.HasPrefix(...) the comment there says that string([]byte) conversion does not incur copy when using various compilers. Perhaps that's what's at play here and might be the reason why the unsafeGetString approach is both slower and allocates more.

return d, err
}

Expand Down Expand Up @@ -425,22 +427,21 @@ func (d *Decimal) setBig(b *BigInt) *BigInt {
// For example, the following values are ordered from lowest to highest. Note
// the difference in ordering between 1.2300 and 1.23.
//
// -NaN
// -NaNSignaling
// -Infinity
// -127
// -1.00
// -1
// -0.000
// -0
// 0
// 1.2300
// 1.23
// 1E+9
// Infinity
// NaNSignaling
// NaN
//
// -NaN
// -NaNSignaling
// -Infinity
// -127
// -1.00
// -1
// -0.000
// -0
// 0
// 1.2300
// 1.23
// 1E+9
// Infinity
// NaNSignaling
// NaN
func (d *Decimal) CmpTotal(x *Decimal) int {
do := d.cmpOrder()
xo := x.cmpOrder()
Expand Down Expand Up @@ -493,9 +494,9 @@ func (d *Decimal) cmpOrder() int {

// Cmp compares x and y and sets d to:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
// -1 if x < y
// 0 if x == y
// +1 if x > y
//
// This comparison respects the normal rules of special values (like NaN),
// and does not compare them.
Expand All @@ -510,11 +511,10 @@ func (c *Context) Cmp(d, x, y *Decimal) (Condition, error) {

// Cmp compares d and x and returns:
//
// -1 if d < x
// 0 if d == x
// +1 if d > x
// undefined if d or x are NaN
//
// -1 if d < x
// 0 if d == x
// +1 if d > x
// undefined if d or x are NaN
func (d *Decimal) Cmp(x *Decimal) int {
ds := d.Sign()
xs := x.Sign()
Expand Down Expand Up @@ -600,7 +600,6 @@ func (d *Decimal) Cmp(x *Decimal) int {
//
// -1 if d.Negative == true
// +1 if d.Negative == false
//
func (d *Decimal) Sign() int {
if d.Form == Finite && d.Coeff.Sign() == 0 {
return 0
Expand Down Expand Up @@ -803,15 +802,14 @@ func (d *Decimal) MarshalText() ([]byte, error) {
// NullDecimal represents a string that may be null. NullDecimal implements
// the database/sql.Scanner interface so it can be used as a scan destination:
//
// var d NullDecimal
// err := db.QueryRow("SELECT num FROM foo WHERE id=?", id).Scan(&d)
// ...
// if d.Valid {
// // use d.Decimal
// } else {
// // NULL value
// }
//
// var d NullDecimal
// err := db.QueryRow("SELECT num FROM foo WHERE id=?", id).Scan(&d)
// ...
// if d.Valid {
// // use d.Decimal
// } else {
// // NULL value
// }
type NullDecimal struct {
Decimal Decimal
Valid bool // Valid is true if Decimal is not NULL
Expand Down
Loading