Skip to content

cmd/compile: does not inline slices.Backwards's yield #79819

Closed as not planned
Closed as not planned

Description

@karelbilek

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?

Activity

  1. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    goplsIssues related to the Go language server, gopls.
    on Jun 4, 2026
  2. added this to the Unreleased milestone on Jun 4, 2026
  3. changed the title [-]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[/+] on Jun 4, 2026
  4. changed the title [-]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[/+] on Jun 4, 2026
  5. madelinekalil commented on Jun 4, 2026

    @madelinekalil
    Contributor

    Thanks for the bug report! There is extra copying to the v variable, 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

  6. Jorropo commented on Jun 4, 2026

    @Jorropo
    Member

    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 yield closure.

    However when I try with go version go1.27-devel_d1d68c2dd0 Wed Jun 3 14:52:16 2026 -0700 linux/amd64 it doesn't repro.
    slices.Backward and yield are inlined.

  7. changed the title [-]x/tools/go/analysis/passes/modernize: slices.Backwards can introduce extra copy[/-] [+]cmd/compile: does not inline slices.Backwards's yield[/+] on Jun 4, 2026
  8. Jorropo commented on Jun 4, 2026

    @Jorropo
    Member

    Thus closing as it looks solved in gotip, if I've got something wrong ask and I'll reopen. 🙂

  9. karelbilek commented on Jun 5, 2026

    @karelbilek
    Author

    @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.

  10. randall77 commented on Jun 8, 2026

    @randall77
    Contributor

    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]byte as 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. if FirstIsOne was 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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions