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: optimize field access after type assertion #28045

Open
rillig opened this issue Oct 5, 2018 · 0 comments
Open

cmd/compile: optimize field access after type assertion #28045

rillig opened this issue Oct 5, 2018 · 0 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Milestone

Comments

@rillig
Copy link
Contributor

rillig commented Oct 5, 2018

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

go version go1.11.1 windows/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

package performance

type Large struct {
	padding1 [1024]string
	interesting string
	padding2 [1024]string
}

func ExtractInteresting(x interface{}) string {
	return x.(Large).interesting
}

What did you expect to see?

The generated code of ExtractInteresting only copies the interesting string, not the padding around it.

What did you see instead?

The generated code first initializes a variable of type Large on the stack (repz stosq). Then it copies the whole struct to the stack (repz movsq). Then it extracts the interesting field.

A real-life example of this pattern can be found in the pkglint project, where an interface is used to emulate a union type. That interface is accessed in many of the accessor methods, such as Varname. In that code I used a struct instead of a pointer to struct, since I wanted to avoid the extra dereference. I had expected that avoiding the pointer would result in faster code.

@randall77 randall77 added this to the Unplanned milestone Oct 5, 2018
rillig added a commit to rillig/pkglint that referenced this issue Oct 6, 2018
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Projects
None yet
Development

No branches or pull requests

3 participants