Skip to content

cmd/compile: inline-able function with named results performs worse #48507

Open
@zigo101

Description

@zigo101

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

$ go version
go version go1.17.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package functions

import "testing"

const N = 1<<12
var buf = make([]byte, N)
var r [128][N]byte

func Benchmark_SliceToArray_Named(b *testing.B) {
	for i := 0; i < b.N; i++ {
		r[i&127] = SliceToArray_Named(buf)
	}
}

func Benchmark_SliceToArray_Unnamed(b *testing.B) {
	for i := 0; i < b.N; i++ {
		r[i&127] = SliceToArray_Unnamed(buf)
	}
}

func SliceToArray_Named(b []byte) (ret [N]byte) {
	ret = *(*[N]byte)(b)
	return
}

func SliceToArray_Unnamed(b []byte) [N]byte {
	return *(*[N]byte)(b)
}

What did you expect to see?

No performance difference

What did you see instead?

The named reult version is slower than the anonymous result version.

Benchmark_SliceToArray_Named-4     	 2983450	       397.7 ns/op
Benchmark_SliceToArray_Unnamed-4   	 4106510	       293.0 ns/op

It looks this is related to code inlining.

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