Skip to content

cmd/compile: copy causes escape #15730

@aclements

Description

@aclements
  1. What version of Go are you using (go version)?
    go version devel +0a9595f Tue May 17 18:46:03 2016 -0400 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/austin/r/go"
    GORACE=""
    GOROOT="/home/austin/go.dev"
    GOTOOLDIR="/home/austin/go.dev/pkg/tool/linux_amd64"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build366275436=/tmp/go-build -gno-record-gcc-switches"
    CXX="g++"
    CGO_ENABLED="1"
  3. What did you do?
package main

var sink [100]byte

func test(args ...interface{}) {
    for _, arg := range args {
        switch arg := arg.(type) {
        case string:
            // copy causes args to escape and causes the
            // convT2E in main to allocate.
            copy(sink[:], arg)

            // Manually copying means args does not escape
            // and the convT2E in main does not allocate.
            //for i := 0; i < len(arg); i++ {
            //  sink[i] = arg[i]
            //}
        }
    }
}

func main() {
    test("abc")
}
  1. What did you expect to see?
    I expected "abc" in main to not escape and the conversion to interface{} to not allocate.
  2. What did you see instead?
    If I use copy in test, escape analysis determines that arg's content escapes, which causes the args slice to escape, which causes the convT2E in main to allocate. However, if I do the copy "manually" by looping over the string, args does not escape, and the convT2E happens on the stack.

/cc @dr2chase @randall77

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.Performanceearly-in-cycleA change that should be done early in the 3 month dev cycle.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions