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 string conversion efficiency #125

Merged
merged 1 commit into from
Oct 17, 2022

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)

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.

Nice improvement!

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

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.

@@ -57,7 +57,8 @@ func (d *Decimal) Append(buf []byte, fmt byte) []byte {
return append(buf, "unknown"...)
}

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

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