-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Replaced comparison with -1 w/ equivalent comparison to 0 #61428
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
base: main
Are you sure you want to change the base?
Conversation
This allows the cpu to fuse the compare and jump.
Thanks for your PR, @@gfoidl. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
I'm not a fan of this type of change; it makes the code less "idiomatic" and we don't have numbers to back that it produces a significant improvement. I understand the CPU will use a better instruction, but the impact is likely negligible in most cases and it definitely takes an extra split second to reason when you are looking at code in things like |
I'm going to be a little contrarian - on the "idiomatic" point of @javiercn ; it could just be the way my brain works, but in all of these methods I don't think of
vs
Which of those is faster? I don't know, but the difference is probably marginal and dwarfed by the thing that returns the value ( |
+1 on the "is negative" view - that's the correct semantic meaning. I'm sure if for some reason -2 was ever returned, a lot of code would be broken because the condition "went the wrong way". |
L0000: test ecx, ecx
L0002: jge short L001e is faster.
It won't change the needle much perf-wise, but a tad here and there can sum up.
That's actually the way to think here. |
which means "correctness" may be a better and easier hammer to justify this PR, rather than perf ;p Working on a BDN, though. |
All in all, the comment about the "negative" vs a concrete number (-1) is fair, but the reality is that the implementation chose to return -1 (and that can't change without breaking the world) and the docs and everything explicitly call out -1 as opposed to a negative number https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof?view=net-9.0. The other aspect of this is that I doubt any coding assistant will go for I'm just adding this for completeness, I want to be 100% clear that I don't care enough about this for this to be a problem, I'm just cautioning of going down the road of making these types of changes without backing numbers when they just make the code less legible for a percentage of people |
Results inconclusive - I think we're talking about the rounding noise:
Again, "correctness" may be the better hammer here. |
This allows the cpu to fuse the compare and jump, thus cpu's backend has a little bit less work to do.
It's a micro-optimization, but the code-change is quite trivial and easy to follow.