-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Normalize Array.IndexOf result checks to use >= 0
#119936
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
Conversation
|
I this feels like a trivial transform that the JIT should be doing instead The typical pattern is -1 because that is the documented sentinel and I don’t think we want to start deviating from that in managed code as a micro-optimization Of course not all cases can be pattern matched, but simple comparison transforms or inversions that lead to improved codegen are well within the realm of what the JIT can and should be handling |
|
Just to clarify (based on a conversation on Discord) I'm not stating the JIT needs to do this optimization. Just rather, if it were beneficial for such an optimization to exist then it would be better for the JIT to provide it. In general we want to keep our managed code idiomatic and we want the JIT to optimize such idiomatic code so that every user benefits (if enough benefit exists to justify such a transform). We do not want to be just rewriting existing managed code, especially not to save 2 bytes of codegen for no measurable perf difference or where it deviates from the existing "standard" pattern that most users write. |
I am not sure what kind of bitwise tricks can the JIT do compared to the ones that it does today. I do not think that the JIT can do this well in all cases. Number of architectures have special instructions for branching when register value is negative, but there are no such instructions for branching when register value is -1. Personally, I like comparing with |
The JIT can already transform patterns like |
I have not done exact counts, but both comparing IndexOf result with -1 and comparing with 0 seems to be very popular. |
+1 |
|
I'm fine with changing the convention if we all agree that It might be good (as a bonus) to document that as part of our coding-style conventions then and potentially to have an analyzer for such cases. |
|
FWIW, comparing result of Array.IndexOf with 0 seems to be the more common pattern under libraries in this repo: 8x 9x |
|
/ba-g timeout, intermittent build crash without details |
Update checks against the result of
Array.IndexOfto consistently use>= 0instead of comparing directly to-1.Summary
Typical patterns seen in the codebase:
Normalized forms:
Rationale
This is a purely mechanical, semantic-preserving change that has some minor codegen benefits.
Diffs: MihuBot/runtime-utils#1526