-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: eliminate more bounds checks #17370
Comments
/cc @dr2chase @randall77 |
/cc @cherrymui. This is relevant to protobuf optimizations. |
The current compiler seems good to eliminate bound checks for constant indices, but does not do much for non-constant not inside a loop.
At least it can get down to two bound checks. |
Hi, with this program I see what I think is unnecessary bound checking, func isPalindrome(s string) bool {
runes := []rune(s)
for len(runes) > 1 {
if runes[0] != runes[len(runes)-1] {
return false
}
runes = runes[1:len(runes)-1]
}
return true
} Relevant assembly loop is,
Version: go version go1.7.1 darwin/amd64 Thought maybe now with SSA Go will have range analysis and would know in this case that since |
Another testcase: https://play.golang.org/p/ZIvG8AUHJL go version devel +7dc97d9 windows/amd64
|
@rillig , it looks like your case isn't really a bug. That said, maybe it would be possible to decide what order to evaluate the RHS of a multiassignment in order to minimize the bounds checks needed. That sounds like a pretty low-priority optimization though, as there is an easy workaround. |
With Go 1.7.4 this code produces bounds checks on all 4 stores in the loop.
It seems like it should be possible to reorder the stores to eliminate 3 of the bounds checks:
However this results in 4 bounds checks as before. |
@willnewton the compiler isn't currently able to deduce that In fact, we currently handle only loops in which the counter bound is a constant or a special value like |
In the OP's code, if the variable |
It looks OP's argument is not valid. What about i == -3 or -2 or -1? |
What version of Go are you using (
go version
)?go version devel +fe77c5b Mon Oct 3 18:26:43 2016 +0000 linux/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/KvKjpisUOl
What did you expect to see?
bounds checks eliminated from assembly
What did you see instead?
I think the chain of panicindex calls should be removed, since both the:
and
_ = buf[i+3]
calls should make it clear that the subsequent accesses cannot possibly go out of bounds. Sorry if this is noise and it's WAI.The text was updated successfully, but these errors were encountered: