Zarith icon indicating copy to clipboard operation
Zarith copied to clipboard

GMP `mpq_cmp` function features a lot of fast cases compared to `Q.compare`

Open bobot opened this issue 9 months ago • 2 comments

In GMP mpq_cmp avoid the actual comparison of the rational at most as possible:

  • comparing the number of limbs
  • comparing the number of bits

Does some benchmarks have been attempted to see if those optimisations are not useful? Would it be useful to use directly mpq_cmp ?

bobot avatar Mar 03 '25 08:03 bobot

Sorry for the delay. The implementation of rationals in Zarith does not seem to be very used; hence, there has not been much attempt to optimize it. Furthermore, as it is a pure OCaml implementation on top of Zarith's integers, it cannot benefit immediately from the optimizations in GMP's mpq: we would have to either reimplement them in OCaml, or completely change the implementation to call MPQ. I don't know if a hybrid approach (keep the OCaml implementation for the most part, but convert to MPQ and call MPQ functions in specific cases) is feasible and interesting performance-wise.

If there is a plan for such optimisations, I'll be OK to reviews the patch, but I don't plan to attempt such a path myself at the moment.

antoinemine avatar Mar 17 '25 08:03 antoinemine

I mainly wanted to know if something similar was attempted before, looking at doing it myself. Thank you a lot for the summary and your work.

bobot avatar Mar 17 '25 08:03 bobot