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

Conversation

miretskiy
Copy link
Contributor

Prefer to use append and allocate scratch space to improve allocation efficiency of SetFloat64.

name         old time/op    new time/op    delta
SetFloat-10     261ns ± 0%     243ns ± 1%   -6.66%  (p=0.000 n=9+9)

name         old alloc/op   new alloc/op   delta
SetFloat-10     91.0B ± 0%     67.0B ± 0%  -26.37%  (p=0.000 n=10+10)

name         old allocs/op  new allocs/op  delta
SetFloat-10      4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.000 n=10+10)

@@ -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.

@miretskiy
Copy link
Contributor Author

miretskiy commented Jul 14, 2023 via email

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

LGTM

bench_test.go Outdated
@@ -122,3 +122,23 @@ func BenchmarkDecimalString(b *testing.B) {
_ = corpus[rng.Intn(len(corpus))].String()
}
}

func BenchmarkSetFloat(b *testing.B) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: BenchmarkDecimalSetFloat because this is a method on Decimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Prefer to use append and allocate scratch space to improve
allocation efficiency of SetFloat64.

```
name         old time/op    new time/op    delta
SetFloat-10     261ns ± 0%     243ns ± 1%   -6.66%  (p=0.000 n=9+9)

name         old alloc/op   new alloc/op   delta
SetFloat-10     91.0B ± 0%     67.0B ± 0%  -26.37%  (p=0.000
n=10+10)

name         old allocs/op  new allocs/op  delta
SetFloat-10      4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.000
n=10+10)
```
@miretskiy miretskiy merged commit e746f59 into cockroachdb:master Jul 14, 2023
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants