Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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)

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
Contributor

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
Contributor

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 miretskiy requested a review from nvb July 14, 2023 17:14
@miretskiy
Copy link
Contributor Author

miretskiy commented Jul 14, 2023 via email

Copy link
Contributor

@nvb nvb 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
}
}

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

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