[Haskell-cafe] Optimizing Eq instances with reallyUnsafePtrEquality#
Joachim Durchholz
jo at durchholz.org
Tue Jun 8 14:42:21 UTC 2021
Am 08.06.21 um 16:25 schrieb Simon Jakobi via Haskell-Cafe:
> Hi everyone!
>
> In https://github.com/haskell-unordered-containers/unordered-containers/issues/77
> we're wondering whether certain Eq instances, for example record types
> or strings, could be optimized by including a pointer equality check
> that detects when an object is compared with itself.
You have to be aware that the pointer comparison itself does not come
for free. Execution prediction means that the "happy path" may be so
rare it's not loaded into the CPU cache and you might end with a slower
system - the case is somewhat pathological but not so rare that you can
just assume that it will not happen to you.
A lot also depends on whether the data to be compared needs to be loaded
anyway. If yes, the pointer comparison won't give you any gains, it will
be just one instruction more to execute, creating more execution unit
contention inside the CPU. If no, then obviously the pointer comparison
will help.
In the end, you need to benchmark to see if it helps you.
And you cannot usefully benchmark unless you have also nailed down all
performance-relevant compiler and runtime options, which you do only if
you have exhausted all other optimization possibilities.
IOW if it complicates your code, don't do it - unless you are already
long past the point where code reusability has taken a back seat to raw
optimization.
Regards,
Jo
More information about the Haskell-Cafe
mailing list