Why is a hand-written equal function faster than ( = )?

I believe this is because the default implementations are calling the polymorphic variants of those functions and those are very heavy. A specialized implementation can be much smaller and potentially inlined or have other optimisations.

For example here’s the polymorphic versions implementations:

Here’s a small blog post by Jane Street on the topic:

3 Likes