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 conditional pointer assignment #34020

Open
mariecurried opened this issue Sep 2, 2019 · 2 comments
Open

cmd/compile: optimize conditional pointer assignment #34020

mariecurried opened this issue Sep 2, 2019 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Performance
Milestone

Comments

@mariecurried
Copy link

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

$ go version
go version devel +d15dfdc Sun Sep 1 02:31:50 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

I compiled the following function (https://godbolt.org/z/rRkMVJ):

func fn(cond bool) int {
	a := new(int)
	if cond {
		*a = 1
	} else {
		*a = 2
	}
    return *a
}

What did you expect to see?

I expected that the generated instructions would be similar to https://godbolt.org/z/q0k3NT

What did you see instead?

Instead, there are MOV instructions to temporary stack variables and there is no use of the CMOV instruction.

@ALTree
Copy link
Member

ALTree commented Sep 2, 2019

Why would you write code like that? I mean, yeah, it could be compiled more efficiently... but optimizations generally 1) increase the complexity of the compiler's code 2) have the potential to slow down the compiler; so in general it's only worth to optimize reasonable code patterns that could appear in real-world code. Is this the case?

@mariecurried
Copy link
Author

mariecurried commented Sep 2, 2019

You are probably right, but I found this while looking at an example published in Slack (https://godbolt.org/z/kofqr2). After playing around with the code, I ended up with the above function and opened this issue.
Please close it if you don't think this kind of code appears in production 😉

@ALTree ALTree added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Performance labels Sep 2, 2019
@ALTree ALTree added this to the Unplanned milestone Sep 2, 2019
@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. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Performance
Projects
None yet
Development

No branches or pull requests

3 participants