StaticArrays.jl
StaticArrays.jl copied to clipboard
Improve BitonicSort performance for sorting floats
Also: add docstring, extend tests, and add benchmarks.
The speedup (for sorting isbits
floats under isless
) is based on @stev47's improvement to isless
in Julia 1.7 (stev47 also authored the BitonicSort implementation!), but instead of NaN-checking we subtract an offset to wrap the NaNs with negative sign to the greatest integers (see code for details), giving branchless code. We apply this transformation only once at the start of each sort to sort them as integers, then apply the inverse transformation at the end to get back the floats. (This technique cannot be used for isless
in Julia Base because isless(NaN, NaN)
returns false regardless of payload and sign.)
Comparative benchmark results on Julia 1.7.0-beta3:
- x86-64 (Intel Haswell, mobile)
- x86-64 (Intel Silvermont, microserver)
- AArch64 (Arm Neoverse N1, Ampere A1 instance on Oracle Cloud)
(The couple of timing fluctuations that show up for integer sorting are just noise.)
I have a vectorized implementation that I hope to contribute later after cleaning it up. (SIMD performance is of course heavily dependent on CPU capabilities and compiler support, which is why I'm benchmarking on three machines. Didn't matter for this PR, though.)
@stev47 Thank you for the review. I've made some changes based on your feedback.
It is a bit regrettable that we now have a different _sort
api than Base, but I have no quick idea around it.
It looks good to me.
Could a maintainer approve the pending workflows? This is ready for merging.