-
Notifications
You must be signed in to change notification settings - Fork 37
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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)
}
}
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) ```
Prefer to use Append and allocate scratch space
to improve speed and efficiency of string conversion: