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, runtime: optimize comparison of large values with zero #23929

Open
josharian opened this issue Feb 19, 2018 · 7 comments
Open

cmd/compile, runtime: optimize comparison of large values with zero #23929

josharian opened this issue Feb 19, 2018 · 7 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Milestone

Comments

@josharian
Copy link
Contributor

josharian commented Feb 19, 2018

package p

type T [20]byte

func f(x *T) bool {
	return *x == T{}
}

Currently this sets up and zeros twenty bytes and then calls memequal. Comparing a large type to zero should be easy; we can then dispatch instead to an optimized runtime routine that doesn't need to actually construct a zero value. (Should handle != as well.)

cc @randall77 @martisch

@josharian josharian added this to the Unplanned milestone Feb 19, 2018
@martisch
Copy link
Contributor

I would suggest to do small compares inline (up to x bytes, like compares, not sure where the cutoff is currently) and when bigger we can call a special runtime routine isZero(*p, len) or alike as suggested.

@cznic
Copy link
Contributor

cznic commented Feb 20, 2018

Why aren't non-scalar required zero values ready made in the text segment?

@josharian
Copy link
Contributor Author

And for that matter, why not use runtime.zeroVal?

isZero will still be more efficient, though.

@martisch
Copy link
Contributor

martisch commented Feb 20, 2018

zeroVal can be used up to 1024 bytes.
I think after that if used in maps we allocate bigger zero values.

However flushing multiple cache lines and loading them with zeros for a zero compare seems wasteful and slow if the zero value is not already loaded.

@josharian
Copy link
Contributor Author

Filed #23948 for using runtime.zeroVal.

@randall77
Copy link
Contributor

See #24416 for large struct optimization.

@gopherbot
Copy link

Change https://golang.org/cl/186477 mentions this issue: cmd/compile: optimize comparisons with zero values

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 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

5 participants