-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: BCE is better with reslicing than index variables #28942
Comments
Possibly related: #24876 |
I believe this also affects the
As before, I tried combining unsigned integers and BCE hints, but no results. I also tried juggling with the for loop conditions, but that didn't make a difference either. |
I just realised that removing bounds checks from a loop that jumps more than one position per iteration might not be as easy as it sounds, because of overflows. Imagine:
If I presume this trouble disappears if we make the index variable unsigned, since an overflow will just get the index back to 0, which will definitely be within bounds. |
/cc @zdjones |
https://golang.org/cl/170125 might be interesting to the folks following this thread. |
Take this piece of code:
It's easy to see why the first variant has zero bounds checks. However, reslicing can be expensive in a hot loop, so sometimes the code is rewritten to use indexes.
This is what the second variant does. I do realise that the two loops aren't equivalent - for example, if
len(p) == 5
, the second loop will panic since the length is not a multiple of 4. So I understand why the compiler needs to insert one bounds check.Still, it seems to me like it should insert one, not four, since I've added the BCE hint. My first thought was that it couldn't prove that
i >= 0
, but changing the index to be unsigned still doesn't remove all the bounds checks that I'd expect.I encountered this in the base64 encoder:
Rewriting the loop to use reslicing like
for len(src) >= 3 { ...; src = src[3:] }
does remove the bounds checks, but slows down the encoder noticeably. Just like in my simple example above, BCE hints like_ = src[si+2]
and unsigned indexes didn't help either.I think there's probably a way to rewrite the loop index logic to trick BCE, but I think the compiler could be made smarter here. I'm not sure whether that should be an enhancement in the prove pass, or in the bounds check elimination pass.
/cc @aclements @rasky @josharian
The text was updated successfully, but these errors were encountered: