What version of Go are you using (go version)?
$ go version
go version go1.14.7 linux/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/prashant/.cache/go-build"
GOENV="/home/prashant/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/prashant/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/prashant/.gimme/versions/go1.14.7.linux.amd64"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/prashant/.gimme/versions/go1.14.7.linux.amd64/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build010078908=/tmp/go-build -gno-record-gcc-switches"
What did you do?
Return a medium sized struct (E.g., 4 byte slices) in similar, but slightly different ways (e.g., using named returns, as part of return), and saw unexpected slow performance for the return "immediate"
E.g., this approach was slower:
func (s largeStruct) ReturnDirectly() (largeStruct, error) {
return largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}, nil
}
as compared to using named returns:
//go:noinline
func (s largeStruct) NamedReturnExplicit() (ls largeStruct, _ error) {
ls = largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}
return ls, nil
}
Full repro:
Details
package main
import "testing"
type largeStruct struct {
bs1 []byte
bs2 []byte
bs3, bs4 int
}
func BenchmarkFoo(b *testing.B) {
tests := []struct {
msg string
f func(s largeStruct) (largeStruct, error)
}{
{"ReturnDirectly", largeStruct.ReturnDirectly},
{"ReturnImmediate", largeStruct.ReturnIntermediate},
{"NamedReturnExplicit", largeStruct.NamedReturnExplicit},
{"NamedReturnImplicit", largeStruct.NamedReturnImplicit},
}
for _, tt := range tests {
b.Run(tt.msg, func(b *testing.B) {
var s largeStruct
var err error
for i := 0; i < b.N; i++ {
s, err = tt.f(s)
}
_ = err
})
}
}
//go:noinline
func (s largeStruct) ReturnDirectly() (largeStruct, error) {
return largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}, nil
}
//go:noinline
func (s largeStruct) ReturnIntermediate() (largeStruct, error) {
ls := largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}
return ls, nil
}
//go:noinline
func (s largeStruct) NamedReturnExplicit() (ls largeStruct, _ error) {
ls = largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}
return ls, nil
}
//go:noinline
func (s largeStruct) NamedReturnImplicit() (ls largeStruct, err error) {
ls = largeStruct{
bs1: s.bs2,
bs2: s.bs1,
bs3: s.bs4,
bs4: s.bs3,
}
return
}
What did you expect to see?
Expected the performance to be the same for the different approaches.
What did you see instead?
Named returns has a performance advantage over returning a value as part of a return statement.
What version of Go are you using (
go version)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env)?go envOutputWhat did you do?
Return a medium sized struct (E.g., 4 byte slices) in similar, but slightly different ways (e.g., using named returns, as part of
return), and saw unexpected slow performance for the return "immediate"E.g., this approach was slower:
as compared to using named returns:
Full repro:
Details
What did you expect to see?
Expected the performance to be the same for the different approaches.
What did you see instead?
Named returns has a performance advantage over returning a value as part of a
returnstatement.