Repository navigation
Closed as not planned
Labels
ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.Issues related to the Go language server, gopls.
Milestone
Description
Go version
go version go1.26.3 darwin/arm64
golang.org/x/tools/go/analysis/passes/modernize v0.45.0
Output of go env in your module/workspace:
AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/karelbilek/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/karelbilek/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/nk/7smw_dlx6kb5073pkfvkfmm00000gn/T/go-build2966487101=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/karelbilek/slicesbackwards/go.mod'
GOMODCACHE='/Users/karelbilek/go/pkg/mod'
GONOPROXY='gitlab.cdn77.eu'
GONOSUMDB='gitlab.cdn77.eu'
GOOS='darwin'
GOPATH='/Users/karelbilek/go'
GOPRIVATE='gitlab.cdn77.eu'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/karelbilek/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.26.3'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
Prior code:
package main
import (
"fmt"
)
type MyStruct struct {
Array [5]byte
}
func (m MyStruct) LastIsOne() bool {
return m.Array[4] == 1
}
func main() {
s := []MyStruct{
{
Array: [5]byte{3, 3, 3, 3, 3},
},
{
Array: [5]byte{1, 1, 1, 1, 1},
},
{
Array: [5]byte{2, 2, 2, 2, 2},
},
}
for i := len(s) - 1; i >= 0; i-- {
fmt.Println(s[i].LastIsOne())
}
}
go run golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest -fix ./...
What did you see happen?
the loop changes to
for _, v := range slices.Backward(s) {
fmt.Println(v.LastIsOne())
}
What did you expect to see?
The code still works the same way - however, it introduces extra copy of the struct to v variable. The struct is copied to the receiver in both loops, however, here it's also copied to v.
Of course this is a toy example and there is probably Println dominating; however, in big structs, this introduces needless copying.
Iterating like for i := range slices.Backward(s) { is somehow better, because the copy is optimized away. I... think?
Metadata
Metadata
Assignees
Labels
ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.Issues related to the Go language server, gopls.
Activity
[-]x/tools/gopls/internal/analysis/modernize: slices.Backwards can introduce extra copy[/-][+]golang.org/x/tools/go/analysis/passes/modernize: slices.Backwards can introduce extra copy[/+]gabyhelp commentedon Jun 4, 2026
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
[-]golang.org/x/tools/go/analysis/passes/modernize: slices.Backwards can introduce extra copy[/-][+]x/tools/go/analysis/passes/modernize: slices.Backwards can introduce extra copy[/+]madelinekalil commentedon Jun 4, 2026
Thanks for the bug report! There is extra copying to the
vvariable, but I'm wondering if you've seen this as a performance issue in practice?A potential fix could be to avoid suggesting a fix with slices.Backward if we observe that SizeOf(s) is larger than some threshold
Jorropo commentedon Jun 4, 2026
I first thought this was a duplicate of #65495 but after looking at the assembly I don't think so.
I can't find an extra copy however I can find spilling when calling the
yieldclosure.However when I try with
go version go1.27-devel_d1d68c2dd0 Wed Jun 3 14:52:16 2026 -0700 linux/amd64it doesn't repro.slices.Backwardandyieldare inlined.[-]x/tools/go/analysis/passes/modernize: slices.Backwards can introduce extra copy[/-][+]cmd/compile: does not inline slices.Backwards's yield[/+]Jorropo commentedon Jun 4, 2026
Thus closing as it looks solved in gotip, if I've got something wrong ask and I'll reopen. 🙂
karelbilek commentedon Jun 5, 2026
@Jorropo hm, this seems to be a side-effect of this commit (trying with bisect)
38ac6577cb
however, a small change still triggers copies, looking at the compiler output
before modernize - https://godbolt.org/z/T1Y6MWxrc
after modernize - https://godbolt.org/z/1G9Kzj6Mo - all the MOVUPS are copies that just don't exist before modernize.
@madelinekalil we did not do any kind of performance test, no. We caught this in PR review (on code where efficiency is critical) where we just "blindly" put
modernize -fix. I don't think modernize should potentially cause a performance regression? I thought of modernizer as "the same thing, just more modern way". Which is not what's happening here.randall77 commentedon Jun 8, 2026
Sure, the compiler could do a better job here. It seems like a pretty low priority case though. I'm inclined to leave this closed.
More generally, using
[1024]byteas a value type, instead of referencing it by pointer, is not a great idea. The compiler is not great at large by-value arrays, and even if it was you're probably going to have a lot more copies at scale than you want. (e.g. ifFirstIsOnewas complicated enough to not inline, but maybe simple enough that it didn't read most of its input array, then you'd be forced to copy the whole thing instead of just reading the elements you need.)