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 speed and efficiency of string conversion:

name              old time/op    new time/op    delta
DecimalString-10     147ns ± 1%      56ns ± 4%  -62.14%  (p=0.000
n=10+10)

name              old alloc/op   new alloc/op   delta
DecimalString-10     84.0B ± 0%     39.0B ± 0%  -53.57%  (p=0.000
n=10+10)

name              old allocs/op  new allocs/op  delta
DecimalString-10      5.00 ± 0%      1.00 ± 0%  -80.00%  (p=0.000
n=10+10)

@miretskiy miretskiy requested a review from nvb October 15, 2022 20:05
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.

Nice improvement!

bench_test.go Outdated
if err != nil {
b.Fatal(err)
}
res[i] = d
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, but can we simplify this to:

for i := range res {
    _, err := res[i].SetFloat64(..)
    if err != nil {
        b.Fatal(err)
    }
}

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.

}

digits := d.Coeff.String()
var scratch [16]byte
Copy link
Contributor

Choose a reason for hiding this comment

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

Does scratch escape to the heap? If it does, could we Append directly to buf? Or temporarily use the spare capacity at the end of buf? fmtE and fmtF look like they might make that tricky.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't look like it;
I couldn't find scratch itself in the output, but

./format.go:107:35: digits does not escape

refers to digits argument to fmtE/f functions -- so, no I don't think scratch escapes.

Prefer to use Append and allocate scratch space
to improve speed and efficiency of string conversion:

```
name              old time/op    new time/op    delta
DecimalString-10     147ns ± 1%      56ns ± 4%  -62.14%  (p=0.000
n=10+10)

name              old alloc/op   new alloc/op   delta
DecimalString-10     84.0B ± 0%     39.0B ± 0%  -53.57%  (p=0.000
n=10+10)

name              old allocs/op  new allocs/op  delta
DecimalString-10      5.00 ± 0%      1.00 ± 0%  -80.00%  (p=0.000
n=10+10)
```
@miretskiy miretskiy merged commit e2bc4ac into cockroachdb:master Oct 17, 2022
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