Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
go101 opened this issue Sep 21, 2021 · 3 comments
Open

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

go101 opened this issue Sep 21, 2021 · 3 comments

Comments

@go101
Copy link

@go101 go101 commented Sep 21, 2021

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.

@randall77
Copy link
Contributor

@randall77 randall77 commented Sep 21, 2021

Related #20859

@go101
Copy link
Author

@go101 go101 commented Sep 21, 2021

Yes, some related, but not totally the same.
If we add the //go:noinline directive for the two functions,
then there is not performance difference.
That means the code generated for the two functions are the same
if they are not inlined, but not the same if they are inlined.

@randall77
Copy link
Contributor

@randall77 randall77 commented Sep 22, 2021

There is an extra zeroing of the named return value that doesn't exist in the unnamed one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants