Skip to content

cmd/compile: possible missed optimization in append benchmark #25916

Open
@perillo

Description

@perillo

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN="/home/manlio/.local/bin"
GOCACHE="/home/manlio/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/manlio/.local/lib/go:/home/manlio/code/src/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build931873105=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I wrote a simple benchmark to check the performance of append versus copy.
The benchmark is here:
https://play.golang.org/p/jA7Fb0oON6Z

The benchmark result is:

Benchmark_Append-4   	30000000	        43.1 ns/op	       0 B/op	       0 allocs/op
Benchmark_Copy-4     	30000000	        43.0 ns/op	       0 B/op	       0 allocs/op

The unexpected result is when bug is set to true. In this case the benchmarks results are:

Benchmark_Append-4   	30000000	        37.2 ns/op	       0 B/op	       0 allocs/op
Benchmark_Copy-4     	30000000	        43.2 ns/op	       0 B/op	       0 allocs/op

The same result is produced when I change bug from a const to a var, even if it is set to false.

When I set GOARCH=386 with bug=true, the benchmark result is:

Benchmark_Append-4   	20000000	        63.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Copy-4     	20000000	        59.6 ns/op	       0 B/op	       0 allocs/op

This seems to be an issue with the amd64 compiler.

This is the assembly listing when bug is false:
https://pastebin.com/kDVTypHF

and this is the assembly listing when bug is true
https://pastebin.com/VErtqZw6

This is the discussion of golang-nuts:
https://groups.google.com/forum/#!topic/golang-nuts/lJvBonZg62g

What did you expect to see?

The benchmark result should be the same, with bug set to false or bug set to true.

What did you see instead?

When bug is set to true, the Append benchmark is faster than the Copy benchmark.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions