-
Notifications
You must be signed in to change notification settings - Fork 783
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
Perf improvement for primitive comparison #13098
Comments
It is worth mentioning that comparing any struct type outside of predefined set of intrinsic types will result in boxing too. Like equality operator. You shoud probably add a benchmark showing IComparable comparsion result e.g. cmp<'a when 'a :> IComparable> |
Could you add a PR for this please? |
I believe it's important to check first what C# does - because JIT team is much more likely to optimize C# stuff. It's not reasonable to make custom il generation just to rewrite it again when jit team does new optimization for C# il patterns. Also, such test is quite cpu-specific and needs to have broader benchmark cpu coverage. Because amd/intel/arm cpus are quite different at handling branches. This is once again why I think its important to check what C# does. |
@En3Tho Actually the implementation in the BCL is not better: |
The last thing that annoys me is that the Jit is producing a movzx eax,al and a movezy edx,dl to extend the result after the setg, setl, but it could factorize it by doing the substraction first, the sign extend... |
@thinkbeforecoding maybe you can first post issue in dotnet/runtime? I'm curious what jit team has to say about this |
Yes for that part, I'll make a suggestion there. |
Cgt and clt are pushing an int on the stack, but the setx instruction works only on 8bits registers (al, dl etc) so you need zero extension to get an int. |
I also didn't touch the implementation for float as it seems to behave differently due to NaN. |
@thinkbeforecoding please use x64 mode in sharplab. It will use latest stable corclr in amd64 mode. Default is x86. |
Yes the sample above is using x64. |
Is your feature request related to a problem? Please describe.
The compiled code is never fast enough. The code for GenericComparisonWithComparerFast generates specific IL code for primitive data types. This code use an if then else branching to return -1, 0 or 1. Branches are costly.
https://github.com/dotnet/fsharp/blob/main/src/fsharp/FSharp.Core/prim-types.fs#L1061
Comparison is used a lot in Set and Map as well as all sorting algorithms.
Describe the solution you'd like
It is possible to get the same result without branching. In the following image, there is the code as currently generated for its, and as it could be implemented.
it uses the fact that clt and cgt actually push 0 or 1 on the stack that can be used directly as an int instead as a bool.
The logic behind this is simple:
The assembly code generated by the JIT have the same size, but the second one contains no branching:
Benchmarking such code cannot be done on a single sample.. it can be subject to constant propagation (this is true in both cases if you call the functions with constant parameters), and branch prediction that can hide the cost of branching. It is better to test it on a large array of random values:
In the benchmark, we sum the results. If not, result is discarded and actually not computed at all.
The result shows the version without branches takes 1/3 of the time compared to the version with branches:
Of course, when the branch is systematically not taken, we do more work while the branch prediction is systematically correct. Here is a benchmark with random x, but y is always x + 2:
When the branch is systematically taken, both version do globally the same work since the branch is not costly. The branch seems even to be free, while the subtraction takes an instruction.
However, these situations where the result are always the same and same branch is always take is less probable than have variable inputs, and the gain when there is misprediction is higher than the loss when prediction is always correct.
Describe alternatives you've considered
Not do it.
The text was updated successfully, but these errors were encountered: