Skip to content

strconv: ParseInt, ParseFloat use two loops instead of one #33330

@sarnold64

Description

@sarnold64

What version of Go are you using (go version)?

$ go version
go version devel +919594830f Tue Jul 23 23:42:43 2019 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes, see version

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/sarnold64/Library/Caches/go-build"
GOENV="/Users/sarnold64/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/sarnold64/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/sarnold64/golang/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/sarnold64/golang/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/sarnold64/golang/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5s/zkkc95rs08731f9t6df3fvsc0000gr/T/go-build178433439=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I noticed that strconv takes more time than it needs to for ParseInt and ParseFloat because it loops over the input twice, once in underscoreOK and once to do the parsing.

What did you expect to see?

One loop

What did you see instead?

Two loops

I was playing around with pprof after the gophercon talks and saw this looking suspicious.

Some performance results on my laptop after fixing:

name                   old time/op  new time/op  delta
ParseInt/Pos/7bit-12   19.0ns ± 3%  12.1ns ± 0%  -36.58%  (p=0.000 n=6+5)
ParseInt/Pos/26bit-12  28.5ns ± 0%  18.4ns ± 2%  -35.58%  (p=0.002 n=6+6)
ParseInt/Pos/31bit-12  32.4ns ± 1%  20.9ns ± 1%  -35.71%  (p=0.002 n=6+6)
ParseInt/Pos/56bit-12  47.0ns ± 2%  29.3ns ± 1%  -37.70%  (p=0.002 n=6+6)
ParseInt/Pos/63bit-12  50.9ns ± 2%  32.0ns ± 1%  -37.24%  (p=0.002 n=6+6)
ParseInt/Neg/7bit-12   18.3ns ± 6%  12.2ns ± 1%  -33.18%  (p=0.002 n=6+6)
ParseInt/Neg/26bit-12  28.2ns ± 3%  18.2ns ± 2%  -35.38%  (p=0.002 n=6+6)
ParseInt/Neg/31bit-12  31.4ns ± 1%  20.7ns ± 1%  -34.04%  (p=0.004 n=6+5)
ParseInt/Neg/56bit-12  45.4ns ± 0%  29.4ns ± 1%  -35.17%  (p=0.004 n=5+6)
ParseInt/Neg/63bit-12  49.4ns ± 1%  32.0ns ± 1%  -35.30%  (p=0.002 n=6+6)

name                   old time/op  new time/op  delta
Atof64Decimal-12       30.1ns ± 1%  25.4ns ± 1%  -15.75%  (p=0.002 n=6+6)
Atof64Float-12         38.3ns ± 1%  31.5ns ± 0%  -17.84%  (p=0.000 n=6+5)
Atof64FloatExp-12      63.1ns ± 0%  53.9ns ± 0%  -14.53%  (p=0.002 n=6+6)
Atof64Big-12            113ns ± 0%    96ns ± 2%  -14.85%  (p=0.010 n=4+6)
Atof64RandomBits-12     180ns ±10%   160ns ±16%  -11.36%  (p=0.028 n=6+6)
Atof64RandomFloats-12   123ns ± 2%   102ns ± 1%  -17.03%  (p=0.002 n=6+6)
Atof32Decimal-12       31.4ns ± 0%  25.4ns ± 1%  -18.86%  (p=0.002 n=6+6)
Atof32Float-12         36.5ns ± 0%  29.4ns ± 0%  -19.40%  (p=0.008 n=5+5)
Atof32FloatExp-12      67.0ns ± 0%  56.6ns ± 0%  -15.58%  (p=0.008 n=5+5)
Atof32Random-12        90.6ns ± 1%  75.1ns ± 0%  -17.15%  (p=0.004 n=6+5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions