Replaced comparison with -1 w/ equivalent comparison to 0#61428
Replaced comparison with -1 w/ equivalent comparison to 0#61428gfoidl wants to merge 1 commit intodotnet:mainfrom
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 L001eis 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. |
|
The benchmark above is clearly dominated by the cost of |
Which was sort of intentional.
I think we should try to find some of those examples - there's probably a lot of prior discussion we can lean on. |
|
I don't think we should make code less clear for such a minor perf improvement. |
I started in runtime with this several years ago, now it's used widespread over there and almost all new code uses a comparison w/
Well, that's IMO in the eye of the beholder. The code isn't less clear with int idx = IndexOf(...);
-if (idx == -1)
+if (idx >= 0)
|
|
I see this as a change to improve readability rather than a change to performance. If the change has actually improved the performance(assuming it to be micro-optimization), it would be good to see any evidence suggesting the improvement. |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
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.