diff options
| author | Alberto Donizetti <alb.donizetti@gmail.com> | 2016-10-01 14:01:09 +0200 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2016-10-17 18:23:32 +0000 |
| commit | cbf28ff87c0aab519cd87a27c168d433f2404764 (patch) | |
| tree | 62613aa9f7f69ff69d896d67df9d7295fe2893e6 /src/strconv | |
| parent | 77b6a08e0d3061cbf505394f1d0c5bc9d16fa6cb (diff) | |
| download | go-cbf28ff87c0aab519cd87a27c168d433f2404764.tar.xz | |
strconv: make FormatFloat slowpath a little faster
The relevant benchmark (on an Intel i7-4510U machine):
name old time/op new time/op delta
FormatFloat/Slowpath64-4 68.6µs ± 0% 44.1µs ± 2% -35.71% (p=0.000 n=13+15)
Change-Id: I67eb0e81ce74ed57752d0280059f91419f09e93b
Reviewed-on: https://go-review.googlesource.com/30099
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/strconv')
| -rw-r--r-- | src/strconv/decimal.go | 6 | ||||
| -rw-r--r-- | src/strconv/ftoa_test.go | 3 |
2 files changed, 7 insertions, 2 deletions
diff --git a/src/strconv/decimal.go b/src/strconv/decimal.go index 5252d6e86e..957acd9891 100644 --- a/src/strconv/decimal.go +++ b/src/strconv/decimal.go @@ -131,11 +131,13 @@ func rightShift(a *decimal, k uint) { } a.dp -= r - 1 + var mask uint = (1 << k) - 1 + // Pick up a digit, put down a digit. for ; r < a.nd; r++ { c := uint(a.d[r]) dig := n >> k - n -= dig << k + n &= mask a.d[w] = byte(dig + '0') w++ n = n*10 + c - '0' @@ -144,7 +146,7 @@ func rightShift(a *decimal, k uint) { // Put down extra digits. for n > 0 { dig := n >> k - n -= dig << k + n &= mask if w < len(a.d) { a.d[w] = byte(dig + '0') w++ diff --git a/src/strconv/ftoa_test.go b/src/strconv/ftoa_test.go index 1d25242ff3..976bd2c9b8 100644 --- a/src/strconv/ftoa_test.go +++ b/src/strconv/ftoa_test.go @@ -208,6 +208,9 @@ var ftoaBenches = []struct { {"64Fixed2", 123.456, 'e', 3, 64}, {"64Fixed3", 1.23456e+78, 'e', 3, 64}, {"64Fixed4", 1.23456e-78, 'e', 3, 64}, + + // Trigger slow path (see issue #15672). + {"Slowpath64", 622666234635.3213e-320, 'e', -1, 64}, } func BenchmarkFormatFloat(b *testing.B) { |
