From cbf28ff87c0aab519cd87a27c168d433f2404764 Mon Sep 17 00:00:00 2001 From: Alberto Donizetti Date: Sat, 1 Oct 2016 14:01:09 +0200 Subject: [PATCH] strconv: make FormatFloat slowpath a little faster MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/strconv/decimal.go | 6 ++++-- 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) { -- 2.48.1