1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:24:39 -07:00
Commit Graph

80 Commits

Author SHA1 Message Date
Sam Arnold
3d48ae355b strconv: Speed improvement to number parsing
Run underscore validation only if we have seen underscores.

Some performance results on my laptop:
name                   old time/op  new time/op  delta
Atof64Decimal-12       30.5ns ± 0%  23.8ns ± 0%  -22.02%  (p=0.016 n=5+4)
Atof64Float-12         39.0ns ± 0%  28.7ns ± 0%  -26.39%  (p=0.002 n=6+6)
Atof64FloatExp-12      64.4ns ± 1%  54.4ns ± 1%  -15.65%  (p=0.002 n=6+6)
Atof64Big-12            115ns ± 1%    87ns ± 1%  -24.45%  (p=0.002 n=6+6)
Atof64RandomBits-12     187ns ±14%   156ns ±19%  -16.46%  (p=0.032 n=6+6)
Atof64RandomFloats-12   126ns ± 0%   105ns ± 1%  -16.65%  (p=0.000 n=6+5)
Atof32Decimal-12       32.0ns ± 1%  24.0ns ± 1%  -24.97%  (p=0.002 n=6+6)
Atof32Float-12         37.1ns ± 1%  27.0ns ± 1%  -27.42%  (p=0.002 n=6+6)
Atof32FloatExp-12      68.4ns ± 1%  54.2ns ± 1%  -20.77%  (p=0.002 n=6+6)
Atof32Random-12        92.0ns ± 1%  77.4ns ± 0%  -15.81%  (p=0.000 n=6+5)
ParseInt/Pos/7bit-12   19.4ns ± 1%  13.8ns ±10%  -28.94%  (p=0.002 n=6+6)
ParseInt/Pos/26bit-12  29.1ns ± 1%  19.8ns ± 2%  -31.92%  (p=0.002 n=6+6)
ParseInt/Pos/31bit-12  33.1ns ± 0%  22.3ns ± 3%  -32.62%  (p=0.004 n=5+6)
ParseInt/Pos/56bit-12  47.8ns ± 1%  30.7ns ± 1%  -35.78%  (p=0.004 n=6+5)
ParseInt/Pos/63bit-12  51.9ns ± 1%  33.4ns ± 2%  -35.49%  (p=0.002 n=6+6)
ParseInt/Neg/7bit-12   18.5ns ± 4%  13.4ns ± 3%  -27.88%  (p=0.002 n=6+6)
ParseInt/Neg/26bit-12  28.4ns ± 3%  19.7ns ± 3%  -30.38%  (p=0.002 n=6+6)
ParseInt/Neg/31bit-12  31.9ns ± 1%  21.8ns ± 2%  -31.56%  (p=0.002 n=6+6)
ParseInt/Neg/56bit-12  46.2ns ± 0%  30.6ns ± 1%  -33.73%  (p=0.004 n=5+6)
ParseInt/Neg/63bit-12  50.2ns ± 1%  33.2ns ± 1%  -33.96%  (p=0.002 n=6+6)

Fixes #33330

Change-Id: I119da66457c2fbaf6e88bb90cf56417a16df8f0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/187957
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-08-28 03:23:59 +00:00
Rob Pike
5498fa90e9 strconv: simplify the text for bases in ParseInt
Followon from a review comment in https://golang.org/cl/191078

Change-Id: If115b2ae0df5e5cb9babd60802947ddb687d56c2
Reviewed-on: https://go-review.googlesource.com/c/go/+/191219
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-08-23 00:46:55 +00:00
Emmanuel T Odeke
d9b1323337 strconv: update documentation
Fixes #33750.
Updates #31197.

Change-Id: I26f63cef57e5f0eec85b84554c82f6d47b4f41a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/191078
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-08-22 05:35:51 +00:00
Andrew Gerrand
2165452a37 strconv: document handling of NaN and ±Inf
In addition to the example that was added in 203b80ab, mention these
special cases in the doc comment. This change also adjusts the example
to include "+Inf", as it was not otherwise mentioned that the plus
symbol may be present.

Fix #30990

Change-Id: I97d66f4aff6a17a6ccc0ee2e7f32e39ae91ae454
Reviewed-on: https://go-review.googlesource.com/c/go/+/179738
Reviewed-by: Alex Miasoedov <msoedov@gmail.com>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Run-TryBot: Benny Siegert <bsiegert@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-31 11:26:23 +00:00
Caleb Spare
05092163bb strconv: fix rounding in FormatFloat fallback path
Float formatting uses a multiprecision fallback path where Grisu3
algorithm fails. This has a bug during the rounding phase: the
difference between the decimal value and the upper bound is examined
byte-by-byte and doesn't properly handle the case where the first
divergence has a difference of 1.

For instance (using an example from #29491), for the number
498484681984085570, roundShortest examines the three decimal values:

lower: 498484681984085536
d:     498484681984085568
upper: 498484681984085600

After examining the 16th digit, we know that rounding d up will fall
within the bounds unless all remaining digits of d are 9 and all
remaining digits of upper are 0:

d:     ...855xx
upper: ...856xx

However, the loop forgets that d and upper have already diverged and
then on the next iteration sees that the 17th digit of d is actually
lower than the 17th digit of upper and decides that we still can't round
up:

d:     ...8556x
upper: ...8560x

Thus the original value is incorrectly rounded down to
498484681984085560 instead of the closer (and equally short)
498484681984085570.

Thanks to Brian Kessler for diagnosing this bug.

Fix it by remembering when we've seen divergence in previous digits.

This CL also fixes another bug in the same loop: for some inputs, the
decimal value d or the lower bound may have fewer digits than the upper
bound, yet the iteration through the digits starts at i=0 for each of
them. For instance, given the float64 value 1e23, we have

d:      99999999999999991611392
upper: 100000000000000000000000

but the loop starts by comparing '9' to '1' rather than '0' to '1'.

I haven't found any cases where this second bug causes incorrect output
because when the digit comparison fails on the first loop iteration the
upper bound always has more nonzero digits (i.e., the expression
'i+1 < upper.nd' is always true).

Fixes #29491

Change-Id: I58856a7a2e47935ec2f233d9f717ef15c78bb2d0
Reviewed-on: https://go-review.googlesource.com/c/go/+/157697
Run-TryBot: Caleb Spare <cespare@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rémy Oudompheng <remyoudompheng@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-23 15:41:19 +00:00
Alex Myasoedov
203b80ab86 strconv: Document ParseFloat's special cases
Updates #30990

Change-Id: I968fb13251ab3796328089046a3f0fc5c7eb9df9
Reviewed-on: https://go-review.googlesource.com/c/go/+/174204
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Run-TryBot: Benny Siegert <bsiegert@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-29 11:07:31 +00:00
Marcel van Lohuizen
34fc6ced76 all: update to Unicode 11
This does *not* update the vendored tables.
A commit updating these tables should follow soon,

Mostly generated running UNICODE_VERSION=11.0.0 in x/text.

Manually updated next.txt file.

Updates golang/go#27945.

Change-Id: I939a01e235aeca898ee9afc99a531e7ad8444e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/154420
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-04-25 20:33:22 +00:00
Rob Pike
c226f6432d strconv: pre-allocate in appendQuotedWith
The byte-at-a-time allocation done quoting strings in appendQuotedWith
grows the output incrementally, which is poor behavior for very large
strings. An easy fix is to make sure the buffer has enough room at
least for an unquoted string.

Add a benchmark with a megabyte of non-ASCII data.
	Before: 39 allocations.
	After: 7 allocations.

We could do better by doing a lot more work but this seems like a big
result for little effort.

Fixes #31472.

Change-Id: I852139e0a2bd13722c4dd329ded8ae1759abad5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/172677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-18 04:30:46 +00:00
Rémy Oudompheng
a1f7dbf0aa strconv: simplify (*extFloat).Multiply using math/bits.Mul64
This method was using a handwritten long multiplication of uint64s.
Since implementation of #24813 we can remove it and replace it
by Mul64 from math/bits.

This brings a small speedup for 64-bit platforms.

Benchmarks on Haswell Celeron 2955U.

benchmark                                  old ns/op     new ns/op     delta
BenchmarkAppendFloat/Decimal-2             127           127           +0.00%
BenchmarkAppendFloat/Float-2               340           317           -6.76%
BenchmarkAppendFloat/Exp-2                 258           233           -9.69%
BenchmarkAppendFloat/NegExp-2              256           231           -9.77%
BenchmarkAppendFloat/Big-2                 402           375           -6.72%
BenchmarkAppendFloat/BinaryExp-2           113           114           +0.88%
BenchmarkAppendFloat/32Integer-2           125           125           +0.00%
BenchmarkAppendFloat/32ExactFraction-2     274           249           -9.12%
BenchmarkAppendFloat/32Point-2             339           317           -6.49%
BenchmarkAppendFloat/32Exp-2               255           229           -10.20%
BenchmarkAppendFloat/32NegExp-2            254           229           -9.84%
BenchmarkAppendFloat/64Fixed1-2            165           154           -6.67%
BenchmarkAppendFloat/64Fixed2-2            184           176           -4.35%
BenchmarkAppendFloat/64Fixed3-2            168           158           -5.95%
BenchmarkAppendFloat/64Fixed4-2            187           177           -5.35%
BenchmarkAppendFloat/Slowpath64-2          84977         84883         -0.11%

Change-Id: If05784e856289b3b7bf136567882e7ee10234756
Reviewed-on: https://go-review.googlesource.com/c/go/+/157717
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-03-04 22:25:21 +00:00
Marcel van Lohuizen
36b09f334f strconv: remove use of DeepEqual for testing errors
Comparing errors using DeepEqual breaks if frame information
is added as proposed in Issue #29934.

Updates #29934.

Change-Id: I0372883288f974998138f95f6c7c79a60f922a3e
Reviewed-on: https://go-review.googlesource.com/c/162177
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
2019-02-27 18:23:19 +00:00
Russ Cox
11af353531 strconv: add 0b, 0o integer prefixes in ParseInt, ParseUint
This CL modifies ParseInt and ParseUint to recognize
0b and 0o as binary and octal base prefixes when base == 0.

See golang.org/design/19308-number-literals for background.

For #19308.
For #12711.

Change-Id: I8efe067f415aa517bdefbff7e230d3fa1694d530
Reviewed-on: https://go-review.googlesource.com/c/160244
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2019-02-12 14:48:26 +00:00
Russ Cox
36a81d5ec3 strconv: accept underscores in ParseInt, ParseUint, ParseFloat
This CL modifies ParseInt, ParseUint, and ParseFloat
to accept digit-separating underscores in their arguments.
For ParseInt and ParseUint, the underscores are only
allowed when base == 0.

See golang.org/design/19308-number-literals for background.

For #28493.

Change-Id: I057ca2539d89314643f591ba8144c3ea7126651c
Reviewed-on: https://go-review.googlesource.com/c/160243
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-12 14:48:24 +00:00
Russ Cox
e2d87f2ca5 strconv: format hex floats
This CL updates FormatFloat to format
standard hexadecimal floating-point constants,
using the 'x' and 'X' verbs.

See golang.org/design/19308-number-literals for background.

For #29008.

Change-Id: I540b8f71d492cfdb7c58af533d357a564591f28b
Reviewed-on: https://go-review.googlesource.com/c/160242
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-12 14:48:22 +00:00
Russ Cox
07717247d8 strconv: parse hex floats
This CL updates ParseFloat to recognize
standard hexadecimal floating-point constants.
See golang.org/design/19308-number-literals for background.

For #29008.

Change-Id: I45f3b0c36b5d92c0e8a4b35c05443a83d7a6d4b3
Reviewed-on: https://go-review.googlesource.com/c/160241
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-12 14:48:20 +00:00
Josh Bleecher Snyder
ed78c90a78 strconv: make docs for Itoa and Atoi slightly higher level
Fixes #29461

Change-Id: I5db8bc80e5bd0778dced8471581c67e66853aada
Reviewed-on: https://go-review.googlesource.com/c/155924
Reviewed-by: Rob Pike <r@golang.org>
2018-12-31 22:48:43 +00:00
Hidetatsu Yaginuma
f4f1b14ab4 strconv: add missing package name into doc.go(godoc overview)
Change-Id: I336ad707a85bf0c81b6c2230c90452c0b3b92924
Reviewed-on: https://go-review.googlesource.com/c/155998
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-30 18:30:11 +00:00
Martin Möhrmann
f81d73e8d5 strconv: add comment explaining bounded shift in formatBits
The compiler can generate better code for shifts bounded to be less than 32
and thereby known to be less than any register width.
See https://golang.org/cl/109776.

Change-Id: I0c4c9f0faafa065fce3c10fd328830deb92f9e38
Reviewed-on: https://go-review.googlesource.com/c/111735
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-10-15 21:45:52 +00:00
Urvil Patel
2afdd17e3f strconv: add example for QuoteRuneToGraphic and QuoteToGraphic functions
Change-Id: Ie5b2ef0087dbc7b8191de8c8b4190396631e3c7f
Reviewed-on: https://go-review.googlesource.com/c/137215
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-10-08 19:13:38 +00:00
Tim Cooper
8aee193fb8 all: remove unneeded parentheses from package consts and vars
Change-Id: Ic7fce53c6264107c15b127d9c9ca0bec11a888ff
Reviewed-on: https://go-review.googlesource.com/c/138183
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-06 12:11:53 +00:00
Ankit Goyal
860484a15f strconv: add example for IsGraphic
Change-Id: I58ba1f5d5c942d6a345c19df1bca80b63fb5abf5
Reviewed-on: https://go-review.googlesource.com/132777
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-09-03 05:45:52 +00:00
Tim Cooper
5ddecd1508 strconv: use bytealg implementation of IndexByteString
benchmark                  old ns/op     new ns/op     delta
    BenchmarkUnquoteEasy-4     188           79.5          -57.71%
    BenchmarkUnquoteHard-4     653           622           -4.75%

Fixes #23821

Change-Id: I1ebfab1b7f0248fd313de21396e0f8612076aa6d
Reviewed-on: https://go-review.googlesource.com/116755
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-22 17:04:01 +00:00
Daniela Petruzalek
6b9c782f9f strconv: clarify "g" and "G" precision in the docs
Fix the wording in "strconv" and "fmt" to make explicit
that the "g" and "G" formats remove trailing zeroes.

Fixes #25082

Change-Id: I2e2ad0a98d2ea27a3a8a006a0563b366f7a3b71b
Reviewed-on: https://go-review.googlesource.com/127135
Reviewed-by: Rob Pike <r@golang.org>
2018-08-01 00:57:00 +00:00
Mark Rushakoff
1f137052e4 strconv: add missing period to godoc comment
Change-Id: I90ba0a6e0c6ccdce16938eed09424308a84fc6fb
GitHub-Last-Rev: 66b6db1a67
GitHub-Pull-Request: golang/go#25801
Reviewed-on: https://go-review.googlesource.com/117575
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-06-09 01:26:25 +00:00
Rebecca Stambler
b219a68ad9 strconv: check for empty string in UnquoteChar
The existing implementation panics on malformed input of an empty
string. strconv.Unquote validates the length of the inputs, but calling
UnquoteChar directly with an empty string leads to a panic, so add a
check for length. Also, add a test to go/constant to ensure that
MakeFromLiteral does not panic on malformed input such as
"const x = ''".

Change-Id: I4217e38db48a09a21ec414bbfb3087709da62904
Reviewed-on: https://go-review.googlesource.com/116215
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-06-04 22:06:42 +00:00
Tim Cooper
161874da2a all: update comment URLs from HTTP to HTTPS, where possible
Each URL was manually verified to ensure it did not serve up incorrect
content.

Change-Id: I4dc846227af95a73ee9a3074d0c379ff0fa955df
Reviewed-on: https://go-review.googlesource.com/115798
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
2018-06-01 21:52:00 +00:00
Ilya Tocar
323c85862a strconv: simplify (*extFloat).Normalize
Use math/bits.LeadingZeros64 instead of local implementation.
This simplifies code, makes Normalize inlinable and fixes performance regression.
Idea was suggested by Giovanni Bajo in #25298

Performance results below:
Atof64Decimal-6                          46.7ns ± 0%  46.7ns ± 0%     ~     (all equal)
Atof64Float-6                            57.9ns ± 1%  56.9ns ± 0%   -1.72%  (p=0.000 n=10+9)
Atof64FloatExp-6                          163ns ± 0%   123ns ± 0%  -24.54%  (p=0.002 n=8+10)
Atof64Big-6                               222ns ± 1%   185ns ± 1%  -16.65%  (p=0.000 n=9+10)
Atof64RandomBits-6                        155ns ± 2%   154ns ± 3%     ~     (p=0.225 n=10+10)
Atof64RandomFloats-6                      156ns ± 2%   154ns ± 2%     ~     (p=0.124 n=10+9)
Atof32Decimal-6                          47.3ns ± 0%  46.7ns ± 0%   -1.26%  (p=0.000 n=7+9)
Atof32Float-6                            51.5ns ± 1%  51.6ns ± 1%     ~     (p=0.455 n=10+9)
Atof32FloatExp-6                          163ns ± 1%   124ns ± 1%  -24.36%  (p=0.000 n=10+10)
Atof32Random-6                            199ns ± 1%   163ns ± 0%  -17.93%  (p=0.000 n=10+10)
FormatFloat/Decimal-6                     209ns ± 2%   211ns ± 2%     ~     (p=0.402 n=10+10)
FormatFloat/Float-6                       393ns ± 2%   379ns ± 1%   -3.57%  (p=0.000 n=10+10)
FormatFloat/Exp-6                         333ns ± 2%   321ns ± 1%   -3.56%  (p=0.000 n=10+9)
FormatFloat/NegExp-6                      338ns ± 3%   317ns ± 1%   -6.27%  (p=0.000 n=10+9)
FormatFloat/Big-6                         457ns ± 1%   443ns ± 2%   -2.99%  (p=0.000 n=9+10)
FormatFloat/BinaryExp-6                   230ns ± 2%   232ns ± 2%     ~     (p=0.070 n=10+10)
FormatFloat/32Integer-6                   209ns ± 2%   211ns ± 1%     ~     (p=0.203 n=10+8)
FormatFloat/32ExactFraction-6             330ns ± 2%   319ns ± 1%   -3.42%  (p=0.000 n=10+10)
FormatFloat/32Point-6                     393ns ± 2%   377ns ± 1%   -4.15%  (p=0.000 n=10+10)
FormatFloat/32Exp-6                       331ns ± 2%   318ns ± 2%   -4.02%  (p=0.000 n=10+10)
FormatFloat/32NegExp-6                    327ns ± 2%   315ns ± 2%   -3.70%  (p=0.000 n=10+10)
FormatFloat/64Fixed1-6                    265ns ± 2%   253ns ± 2%   -4.38%  (p=0.000 n=10+10)
FormatFloat/64Fixed2-6                    278ns ± 2%   262ns ± 3%   -5.71%  (p=0.000 n=10+10)
FormatFloat/64Fixed3-6                    271ns ± 2%   260ns ± 2%   -4.03%  (p=0.000 n=10+10)
FormatFloat/64Fixed4-6                    277ns ± 3%   267ns ± 1%   -3.55%  (p=0.000 n=10+9)
FormatFloat/Slowpath64-6                 71.0µs ± 0%  71.0µs ± 0%     ~     (p=0.744 n=10+8)
AppendFloat/Decimal-6                     100ns ± 1%   100ns ± 0%     ~     (p=0.294 n=10+8)
AppendFloat/Float-6                       273ns ± 0%   260ns ± 1%   -4.87%  (p=0.000 n=7+10)
AppendFloat/Exp-6                         213ns ± 0%   200ns ± 0%   -6.29%  (p=0.000 n=8+10)
AppendFloat/NegExp-6                      211ns ± 0%   198ns ± 0%   -6.16%  (p=0.000 n=8+8)
AppendFloat/Big-6                         319ns ± 0%   305ns ± 0%   -4.31%  (p=0.000 n=8+7)
AppendFloat/BinaryExp-6                  98.4ns ± 0%  92.9ns ± 0%   -5.63%  (p=0.000 n=9+8)
AppendFloat/32Integer-6                   101ns ± 1%   102ns ± 1%   +0.89%  (p=0.004 n=10+10)
AppendFloat/32ExactFraction-6             222ns ± 1%   210ns ± 0%   -5.28%  (p=0.000 n=10+9)
AppendFloat/32Point-6                     273ns ± 1%   261ns ± 1%   -4.62%  (p=0.000 n=10+9)
AppendFloat/32Exp-6                       209ns ± 1%   197ns ± 0%   -5.56%  (p=0.000 n=10+9)
AppendFloat/32NegExp-6                    207ns ± 1%   194ns ± 1%   -6.18%  (p=0.000 n=10+10)
AppendFloat/64Fixed1-6                    145ns ± 0%   131ns ± 1%   -9.93%  (p=0.000 n=9+10)
AppendFloat/64Fixed2-6                    160ns ± 0%   146ns ± 0%   -8.58%  (p=0.000 n=10+8)
AppendFloat/64Fixed3-6                    147ns ± 1%   132ns ± 1%  -10.25%  (p=0.000 n=10+10)
AppendFloat/64Fixed4-6                    161ns ± 1%   149ns ± 0%   -7.93%  (p=0.000 n=10+10)
AppendFloat/Slowpath64-6                 70.6µs ± 1%  70.9µs ± 0%   +0.37%  (p=0.000 n=10+8)

Change-Id: I63bbc40905abd795fbd24743604c790023d11a43
Reviewed-on: https://go-review.googlesource.com/113256
Run-TryBot: Ilya Tocar <ilya.tocar@intel.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-05-30 19:28:20 +00:00
Sabin Mihai Rapan
74879f0f01 strconv: update Unquote example to be more concise
Changed the example to convey the intent of the Unquote function
in a more succint way.

Fixes #23693

Change-Id: I49465641d730e70b5af0d47057335af39882bcec
Reviewed-on: https://go-review.googlesource.com/92015
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-05-07 20:39:37 +00:00
Marvin Stenger
3c0bf181b7 strconv: simplify and optimize Itoa(small)
Use substring of digits for values < 10.

name                 old time/op  new time/op  delta
FormatIntSmall/7-4   4.54ns ± 1%  3.70ns ± 1%  -18.41%  (p=0.000 n=18+17)
FormatIntSmall/42-4  4.54ns ± 1%  4.13ns ± 1%   -9.02%  (p=0.000 n=16+18)

Change-Id: I0b521b563c13ef88aa2701049fa4a43760e884af
Reviewed-on: https://go-review.googlesource.com/111285
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-05-03 18:07:02 +00:00
Martin Möhrmann
d29ec40e19 strconv: use bounded bits.TrailingZeros instead of shifts table
The strconv shifts table is 320 bytes (amd64) and is present in
many binaries since integer formatting is very common.

Instead of using a precalculated table with shift amounts
use a bounded bits.TrailingZeros to determine the shift amount
to format numbers in a base that is a power of 2.

amd64:
name        old time/op  new time/op  delta
AppendUint   379ns ± 1%   286ns ± 2%  -24.62%  (p=0.000 n=20+19)

Change-Id: Ib94d9b033321b41e975868943c7fcd9428c5111e
Reviewed-on: https://go-review.googlesource.com/110478
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-05-01 15:48:15 +00:00
quasilyte
b3be2f4da7 strconv: make code formatting more consistent in doc.go
Replaces " \t" code indentation with "\t".

Issues like this are easy to spot with editor that prints
whitespace charecters.

Change-Id: Ia82877e7c99121bf369fa76e46ba52dff84f36bf
Reviewed-on: https://go-review.googlesource.com/101355
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-03-19 12:53:16 +00:00
Mark Rushakoff
5e52471761 all: fix non-standard "DO NOT EDIT" comments for generated files
I found files to change with this command:

    git grep 'DO NOT EDIT' | grep -v 'Code generated .* DO NOT'

There are more files that match that grep, but I do not intend on fixing
them.

Change-Id: I4b474f1c29ca3135560d414785b0dbe0d1a4e52c
GitHub-Last-Rev: 65804b0263
GitHub-Pull-Request: golang/go#24334
Reviewed-on: https://go-review.googlesource.com/99955
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-03-10 17:50:11 +00:00
Brad Fitzpatrick
c10e61424f strconv: detect invalid UTF-8 in the Unquote fast path
Fixes #23685

Change-Id: I3625bd01f860077ee0976df9e3dfb66754804bcd
Reviewed-on: https://go-review.googlesource.com/92535
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-02-13 18:33:44 +00:00
Marcel van Lohuizen
2fd9549797 unicode: update to Unicode 10.0.0
Also includes all derived values as well as
vendored packages.

Generated by running
    UNICODE_VERSION=10.0.0 go generate
in golang.org/x/text

and modified by hand to add the tests and
entries in next.txt for new script and properties.

Closes Issue #21471

Change-Id: I1d10ee3887bd1fd3d5a756ee0d04bd6ec2814ba1
Reviewed-on: https://go-review.googlesource.com/63953
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
2017-10-24 12:42:35 +00:00
Kunpei Sakai
5a986eca86 all: fix article typos
a -> an

Change-Id: I7362bdc199e83073a712be657f5d9ba16df3077e
Reviewed-on: https://go-review.googlesource.com/63850
Reviewed-by: Rob Pike <r@golang.org>
2017-09-15 02:39:16 +00:00
Daniel Martí
99da8730b0 all: remove some double spaces from comments
Went mainly for the ones that make no sense, such as the ones
mid-sentence or after commas.

Change-Id: Ie245d2c19cc7428a06295635cf6a9482ade25ff0
Reviewed-on: https://go-review.googlesource.com/57293
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-08-26 15:09:09 +00:00
Aliaksandr Valialkin
46aa9f5437 strconv: optimize Atoi for common case
Benchmark results on GOOS=linux:

GOARCH=amd64

name              old time/op  new time/op  delta
Atoi/Pos/7bit-4   20.1ns ± 2%   8.6ns ± 1%  -57.34%  (p=0.000 n=10+10)
Atoi/Pos/26bit-4  25.8ns ± 7%  11.9ns ± 0%  -53.91%  (p=0.000 n=10+8)
Atoi/Pos/31bit-4  27.3ns ± 2%  13.2ns ± 1%  -51.56%  (p=0.000 n=10+10)
Atoi/Pos/56bit-4  37.2ns ± 5%  18.2ns ± 1%  -51.26%  (p=0.000 n=10+10)
Atoi/Pos/63bit-4  38.7ns ± 1%  38.6ns ± 1%     ~     (p=0.297 n=9+10)
Atoi/Neg/7bit-4   17.6ns ± 1%   7.2ns ± 0%  -59.22%  (p=0.000 n=10+10)
Atoi/Neg/26bit-4  24.4ns ± 1%  12.4ns ± 1%  -49.28%  (p=0.000 n=10+10)
Atoi/Neg/31bit-4  26.9ns ± 0%  14.0ns ± 1%  -47.88%  (p=0.000 n=7+10)
Atoi/Neg/56bit-4  36.2ns ± 1%  19.5ns ± 0%  -46.24%  (p=0.000 n=10+9)
Atoi/Neg/63bit-4  38.9ns ± 1%  38.8ns ± 1%     ~     (p=0.385 n=9+10)

GOARCH=386

name              old time/op  new time/op  delta
Atoi/Pos/7bit-4   89.6ns ± 1%   8.2ns ± 1%  -90.84%  (p=0.000 n=9+10)
Atoi/Pos/26bit-4   187ns ± 2%    12ns ± 1%  -93.71%  (p=0.000 n=10+9)
Atoi/Pos/31bit-4   225ns ± 1%   225ns ± 1%     ~     (p=0.995 n=10+10)
Atoi/Neg/7bit-4   86.2ns ± 1%   8.5ns ± 1%  -90.14%  (p=0.000 n=10+10)
Atoi/Neg/26bit-4   183ns ± 1%    13ns ± 1%  -92.77%  (p=0.000 n=9+10)
Atoi/Neg/31bit-4   223ns ± 0%   223ns ± 0%     ~     (p=0.247 n=8+9)

Fixes #20557

Change-Id: Ib6245d88cffd4b037419e2bf8e4a71b86c6d773f
Reviewed-on: https://go-review.googlesource.com/44692
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-08-25 10:17:39 +00:00
Martin Möhrmann
63c4284346 strconv: check bitsize range in ParseInt and ParseUint
Return an error when a bitSize below 0 or above 64 is specified.

Move bitSize 0 handling in ParseInt after the call to ParseUint
to avoid a spill.

AMD64:
name       old time/op  new time/op  delta
Atoi       28.9ns ± 6%  27.4ns ± 6%  -5.21%  (p=0.002 n=20+20)
AtoiNeg    24.6ns ± 2%  23.1ns ± 1%  -6.04%  (p=0.000 n=19+18)
Atoi64     38.8ns ± 1%  38.0ns ± 1%  -2.03%  (p=0.000 n=17+20)
Atoi64Neg  35.5ns ± 1%  34.3ns ± 1%  -3.42%  (p=0.000 n=19+20)

Updates #21275

Change-Id: I70f0e4a16fa003f7ea929ca4ef56bd1a4181660b
Reviewed-on: https://go-review.googlesource.com/55139
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
2017-08-22 13:37:40 +00:00
Martin Möhrmann
de7e5d45df strconv: cleanup variable declarations in ParseUint
Move variable declarations closer to their first uses.

Use an additional string variable s0 for error reporting that references
the original input string. This allows the variable s to be modified.

Change-Id: I4725152490ca1dc10c1161ad8ad2f4ae8933493f
Reviewed-on: https://go-review.googlesource.com/55138
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Marvin Stenger <marvin.stenger94@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-08-15 04:24:52 +00:00
Martin Möhrmann
dd6880d69b strconv: unify error creation in ParseUint with ParseInt
Remove goto and use helper functions in ParseUint to create errors.

Change-Id: I1c4677ae1b9980db79065a9f8ca1f2c470249505
Reviewed-on: https://go-review.googlesource.com/55135
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2017-08-14 21:02:21 +00:00
Martin Möhrmann
566f390cfa strconv: adjust test output and names for parseUint and parseInt
Instead of printing Atoi as function name for test failures
print the actual function name and arguments tested.

Add a base field to the parseUint64BaseTests for consistency with
the parseInt64BaseTests tests.

Change-Id: Ib9891bdb87b62672b4216625212acfe6474c70fc
Reviewed-on: https://go-review.googlesource.com/55136
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2017-08-14 20:51:59 +00:00
Martin Möhrmann
09ed0f6805 strconv: avoid truncation of output in parse int tests
If needed cast the test table values to a higher bit size
integer type instead of casting the result values of the
tested function to a lower bit size integer type.

Change-Id: Iaa79742b2b1d90c7c7eac324f54032ebea0b1b41
Reviewed-on: https://go-review.googlesource.com/55137
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2017-08-13 10:08:42 +00:00
Martin Möhrmann
fc6b74ce39 strconv: fix ParseUint return value on range overflow
If the value corresponding to the input string cannot be
represented by an unsigned integer of the given size,
err.Err = ErrRange and the returned value is the maximum
magnitude unsigned integer of the appropriate bitSize.
This is consistent with ParseInt's behavior and the documentation.

Expand tests to test 32 bit test value tables with bitsize 32 set.
These tests fail without the fix in this CL.

Fixes #21278

Change-Id: I8aab39279ec3e31905fcbf582a916cbf6d9b95da
Reviewed-on: https://go-review.googlesource.com/55134
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2017-08-13 09:11:32 +00:00
Russ Cox
8bb88fdcc2 strconv: apply Brad's review comment
Missed in CL 49253 because I submitted from the Gerrit UI and
had not mailed the latest copy. Sigh.

Change-Id: I540f960278df43e2eaf1aac188eb124a1ff240dd
Reviewed-on: https://go-review.googlesource.com/49256
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2017-07-17 16:32:05 +00:00
Russ Cox
5ac16c6c76 strconv: fix initialization of atofRandomTests
The init func was using testing.Short, but that's not available
until after flag parsing. Found by CL 49251.

Change-Id: Ia7b871043375260873fa2c7e81e1d43c1c83d33f
Reviewed-on: https://go-review.googlesource.com/49253
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-07-17 15:46:53 +00:00
Adam Kisala
8cb40fa421 strconv: adds missing comment to neg flag, formats comment on trunc flag
Change-Id: Ibdd57489543d57a24d1e3c41210abd9fbc930b8f
Reviewed-on: https://go-review.googlesource.com/48867
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-15 19:41:25 +00:00
Aliaksandr Valialkin
4c8023bfed strconv: optimize decimal ints formatting with smallsString
Benchmark results for GOARCH=amd64:

name                                     old time/op  new time/op  delta
FormatInt-4                              2.51µs ± 2%  2.40µs ± 2%   -4.51%  (p=0.000 n=9+10)
AppendInt-4                              1.67µs ± 2%  1.61µs ± 3%   -3.74%  (p=0.000 n=9+9)
FormatUint-4                              698ns ± 2%   643ns ± 3%   -7.95%  (p=0.000 n=10+8)
AppendUint-4                              478ns ± 1%   418ns ± 2%  -12.61%  (p=0.000 n=8+10)
AppendUintVarlen/1-4                     9.30ns ± 6%  9.15ns ± 1%     ~     (p=0.199 n=9+10)
AppendUintVarlen/12-4                    9.12ns ± 0%  9.16ns ± 2%     ~     (p=0.307 n=9+9)
AppendUintVarlen/123-4                   18.6ns ± 2%  18.7ns ± 0%     ~     (p=0.091 n=10+6)
AppendUintVarlen/1234-4                  19.1ns ± 4%  17.7ns ± 1%   -7.35%  (p=0.000 n=10+9)
AppendUintVarlen/12345-4                 21.5ns ± 3%  20.7ns ± 3%   -3.78%  (p=0.002 n=9+10)
AppendUintVarlen/123456-4                23.5ns ± 3%  20.9ns ± 1%  -11.14%  (p=0.000 n=10+9)
AppendUintVarlen/1234567-4               25.0ns ± 2%  23.6ns ± 7%   -5.48%  (p=0.004 n=9+10)
AppendUintVarlen/12345678-4              26.8ns ± 2%  23.4ns ± 2%  -12.79%  (p=0.000 n=9+10)
AppendUintVarlen/123456789-4             29.8ns ± 3%  26.5ns ± 5%  -11.03%  (p=0.000 n=10+10)
AppendUintVarlen/1234567890-4            31.6ns ± 3%  26.9ns ± 3%  -14.95%  (p=0.000 n=10+9)
AppendUintVarlen/12345678901-4           33.8ns ± 3%  29.3ns ± 5%  -13.21%  (p=0.000 n=10+10)
AppendUintVarlen/123456789012-4          35.5ns ± 4%  29.2ns ± 4%  -17.82%  (p=0.000 n=10+10)
AppendUintVarlen/1234567890123-4         37.6ns ± 4%  31.4ns ± 3%  -16.48%  (p=0.000 n=10+10)
AppendUintVarlen/12345678901234-4        39.8ns ± 6%  32.0ns ± 7%  -19.60%  (p=0.000 n=10+10)
AppendUintVarlen/123456789012345-4       40.7ns ± 0%  34.4ns ± 4%  -15.55%  (p=0.000 n=6+10)
AppendUintVarlen/1234567890123456-4      45.4ns ± 6%  35.1ns ± 4%  -22.66%  (p=0.000 n=10+10)
AppendUintVarlen/12345678901234567-4     45.1ns ± 1%  36.7ns ± 4%  -18.77%  (p=0.000 n=9+10)
AppendUintVarlen/123456789012345678-4    46.9ns ± 0%  36.4ns ± 3%  -22.49%  (p=0.000 n=9+10)
AppendUintVarlen/1234567890123456789-4   50.6ns ± 6%  38.8ns ± 3%  -23.28%  (p=0.000 n=10+10)
AppendUintVarlen/12345678901234567890-4  51.3ns ± 2%  38.4ns ± 0%  -25.00%  (p=0.000 n=9+8)

Benchmark results for GOARCH=386:

name                                     old time/op  new time/op  delta
FormatInt-4                              6.21µs ± 0%  6.14µs ± 0%  -1.11%  (p=0.008 n=5+5)
AppendInt-4                              4.95µs ± 0%  4.85µs ± 0%  -1.99%  (p=0.016 n=5+4)
FormatUint-4                             1.89µs ± 1%  1.83µs ± 1%  -2.94%  (p=0.008 n=5+5)
AppendUint-4                             1.59µs ± 0%  1.57µs ± 2%  -1.72%  (p=0.040 n=5+5)
FormatIntSmall-4                         8.48ns ± 0%  8.48ns ± 0%    ~     (p=0.905 n=5+5)
AppendIntSmall-4                         12.2ns ± 0%  12.2ns ± 0%    ~     (all equal)
AppendUintVarlen/1-4                     10.6ns ± 1%  10.7ns ± 0%    ~     (p=0.238 n=5+4)
AppendUintVarlen/12-4                    10.7ns ± 0%  10.7ns ± 1%    ~     (p=0.333 n=4+5)
AppendUintVarlen/123-4                   29.9ns ± 1%  30.2ns ± 0%  +1.07%  (p=0.016 n=5+4)
AppendUintVarlen/1234-4                  32.4ns ± 1%  30.4ns ± 0%  -6.30%  (p=0.008 n=5+5)
AppendUintVarlen/12345-4                 35.1ns ± 2%  34.9ns ± 0%    ~     (p=0.238 n=5+5)
AppendUintVarlen/123456-4                36.6ns ± 0%  35.3ns ± 0%  -3.55%  (p=0.029 n=4+4)
AppendUintVarlen/1234567-4               38.9ns ± 0%  39.6ns ± 0%  +1.80%  (p=0.029 n=4+4)
AppendUintVarlen/12345678-4              41.3ns ± 0%  40.1ns ± 0%  -2.91%  (p=0.000 n=5+4)
AppendUintVarlen/123456789-4             44.9ns ± 1%  44.8ns ± 0%    ~     (p=0.667 n=5+5)
AppendUintVarlen/1234567890-4            65.6ns ± 0%  66.2ns ± 1%  +0.88%  (p=0.016 n=4+5)
AppendUintVarlen/12345678901-4           77.9ns ± 0%  76.3ns ± 0%  -2.00%  (p=0.000 n=4+5)
AppendUintVarlen/123456789012-4          80.7ns ± 0%  79.1ns ± 1%  -2.01%  (p=0.008 n=5+5)
AppendUintVarlen/1234567890123-4         83.6ns ± 0%  80.2ns ± 1%  -4.07%  (p=0.008 n=5+5)
AppendUintVarlen/12345678901234-4        86.2ns ± 1%  83.3ns ± 0%  -3.39%  (p=0.008 n=5+5)
AppendUintVarlen/123456789012345-4       88.5ns ± 0%  83.7ns ± 0%  -5.42%  (p=0.008 n=5+5)
AppendUintVarlen/1234567890123456-4      90.6ns ± 0%  88.3ns ± 0%  -2.54%  (p=0.008 n=5+5)
AppendUintVarlen/12345678901234567-4     92.7ns ± 0%  89.0ns ± 1%  -4.01%  (p=0.008 n=5+5)
AppendUintVarlen/123456789012345678-4    95.6ns ± 1%  92.6ns ± 0%  -3.18%  (p=0.016 n=5+4)
AppendUintVarlen/1234567890123456789-4    118ns ± 0%   114ns ± 0%    ~     (p=0.079 n=4+5)
AppendUintVarlen/12345678901234567890-4   138ns ± 0%   136ns ± 0%  -1.45%  (p=0.008 n=5+5)

Updates #19445

Change-Id: Iafbe5c074898187c150dc3854e5b9fc19c10be05
Reviewed-on: https://go-review.googlesource.com/38255
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2017-03-22 16:03:28 +00:00
Robert Griesemer
aea44109cf strconv: replace small int string table with constant string
This reduces memory use yet still provides the significant
performance gain seen when using a fast path for small integers.

Improvement of this CL comparing to code without fast path:

name              old time/op  new time/op  delta
FormatIntSmall-8  35.6ns ± 1%   4.5ns ± 1%  -87.30%  (p=0.008 n=5+5)
AppendIntSmall-8  17.4ns ± 1%   9.4ns ± 3%  -45.70%  (p=0.008 n=5+5)

For comparison, here's the improvement before this CL to code without
fast path (1% better for FormatIntSmall):

name              old time/op  new time/op  delta
FormatIntSmall-8  35.6ns ± 1%   4.0ns ± 3%  -88.64%  (p=0.008 n=5+5)
AppendIntSmall-8  17.4ns ± 1%   8.2ns ± 1%  -52.80%  (p=0.008 n=5+5)

Thus, the code in this CL performs slower for small integers using fast
path then the prior version, but this is relative to an already very fast
version:

name              old time/op  new time/op  delta
FormatIntSmall-8  4.05ns ± 3%  4.52ns ± 1%  +11.81%  (p=0.008 n=5+5)
AppendIntSmall-8  8.21ns ± 1%  9.45ns ± 3%  +15.05%  (p=0.008 n=5+5)

Measured on 2.3 GHz Intel Core i7 running macOS Sierra 10.12.3.

Overall, it's still ~88% faster than without fast path for small integers,
so probably worth it as it removes 100 global string slices in favor of
a single string.

Credits: This is based on the original (but cleaned up) version of the
code by Aliaksandr Valialkin (https://go-review.googlesource.com/c/37963/).

Change-Id: Icda78679c8c14666d46257894e9fa3d7f35e58b8
Reviewed-on: https://go-review.googlesource.com/38319
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2017-03-17 20:33:46 +00:00
Aliaksandr Valialkin
bc8b9b23ca strconv: optimize formatting for small decimal ints
Avoid memory allocations by returning pre-calculated strings
for decimal ints in the range 0..99.

Benchmark results:

name              old time/op    new time/op    delta
FormatInt-4         2.45µs ± 1%    2.40µs ± 1%    -1.86%  (p=0.000 n=8+9)
AppendInt-4         1.67µs ± 1%    1.65µs ± 0%    -0.92%  (p=0.000 n=10+10)
FormatUint-4         676ns ± 3%     669ns ± 1%      ~     (p=0.146 n=10+10)
AppendUint-4         467ns ± 2%     474ns ± 0%    +1.58%  (p=0.000 n=10+10)
FormatIntSmall-4    29.6ns ± 2%     3.3ns ± 0%   -88.98%  (p=0.000 n=10+9)
AppendIntSmall-4    16.0ns ± 1%     8.5ns ± 0%   -46.98%  (p=0.000 n=10+9)

name              old alloc/op   new alloc/op   delta
FormatInt-4           576B ± 0%      576B ± 0%      ~     (all equal)
AppendInt-4          0.00B          0.00B           ~     (all equal)
FormatUint-4          224B ± 0%      224B ± 0%      ~     (all equal)
AppendUint-4         0.00B          0.00B           ~     (all equal)
FormatIntSmall-4     2.00B ± 0%     0.00B       -100.00%  (p=0.000 n=10+10)
AppendIntSmall-4     0.00B          0.00B           ~     (all equal)

name              old allocs/op  new allocs/op  delta
FormatInt-4           37.0 ± 0%      35.0 ± 0%    -5.41%  (p=0.000 n=10+10)
AppendInt-4           0.00           0.00           ~     (all equal)
FormatUint-4          6.00 ± 0%      6.00 ± 0%      ~     (all equal)
AppendUint-4          0.00           0.00           ~     (all equal)
FormatIntSmall-4      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
AppendIntSmall-4      0.00           0.00           ~     (all equal)

Fixes #19445

Change-Id: Ib1f8922f2e0b13743c847ee9e703d1dab77f705c
Reviewed-on: https://go-review.googlesource.com/37963
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-16 19:58:51 +00:00
Martin Möhrmann
b71ed4edc6 strconv: fix performance regression in integer formatting on 32bit platforms
Some of the changes in CL golang.org/cl/38071/ assumed that / and %
could always be combined to use only one DIV instruction. However,
this is not the case for 64bit operands on a 32bit platform which use
seperate runtime functions to calculate division and modulo.

This CL restores the original optimizations that help on 32bit platforms
with negligible impact on 64bit platforms.

386:
name          old time/op  new time/op  delta
FormatInt-2   6.06µs ± 0%  6.02µs ± 0%  -0.70%  (p=0.000 n=20+20)
AppendInt-2   4.98µs ± 0%  4.98µs ± 0%    ~     (p=0.747 n=18+18)
FormatUint-2  1.93µs ± 0%  1.85µs ± 0%  -4.19%  (p=0.000 n=20+20)
AppendUint-2  1.71µs ± 0%  1.64µs ± 0%  -3.68%  (p=0.000 n=20+20)

amd64:
name          old time/op  new time/op  delta
FormatInt-2   2.41µs ± 0%  2.41µs ± 0%  -0.09%  (p=0.010 n=18+18)
AppendInt-2   1.77µs ± 0%  1.77µs ± 0%  +0.08%  (p=0.000 n=18+18)
FormatUint-2   653ns ± 1%   653ns ± 0%    ~     (p=0.178 n=20+20)
AppendUint-2   514ns ± 0%   513ns ± 0%  -0.13%  (p=0.000 n=20+17)

Change-Id: I574a18e54fb41b25fbe51ce696e7a8765abc79a6
Reviewed-on: https://go-review.googlesource.com/38051
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2017-03-12 21:37:32 +00:00
Robert Griesemer
95c5227c15 strconv: use % instead of computing the remainder from the quotient
The compiler recognizes that in a sequence q = x/y; r = x%y only
one division is required. Remove prior work-arounds and write
more readable straight-line code (this also results in fewer
instructions, though it doesn't appear to affect the benchmarks
significantly).

name          old time/op  new time/op  delta
FormatInt-8   2.95µs ± 1%  2.92µs ± 5%   ~     (p=0.952 n=5+5)
AppendInt-8   1.91µs ± 1%  1.89µs ± 2%   ~     (p=0.421 n=5+5)
FormatUint-8   795ns ± 2%   782ns ± 4%   ~     (p=0.444 n=5+5)
AppendUint-8   557ns ± 1%   557ns ± 2%   ~     (p=0.548 n=5+5)

https://perf.golang.org/search?q=upload:20170310.1

Also:
- use uint instead of uintptr where we want to guarantee single-
  register operations
- remove some unnecessary conversions (before indexing)
- add more comments and fix some comments

Change-Id: I04858dc2d798a6495879d9c7cfec2fdc2957b704
Reviewed-on: https://go-review.googlesource.com/38071
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-11 00:14:24 +00:00