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

**URL:** <https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719>\
**Category:** Learning\
**Created:** [April 22, 2022, 10:04pm UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719 "2022-04-22T22:04:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giltho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/giltho/32/3521_2.png) [@giltho](https://discuss.ocaml.org/u/giltho)\
**Post date:** [April 22, 2022, 10:04pm UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719/1 "2022-04-22T22:04:24Z")

</div>

Hi!

[The documentation of ppx\_deriving](https://github.com/ocaml-ppx/ppx_deriving#plugins-eq-and-ord) explains that the `equal` and `compare` functions derived are faster than the usage of `=`. Is it really the case? And if so, why? I haven’t been able to find out.

Thanks!

---

<div class="post-metadata">

**Author:** ![OCamlUser](https://avatars.discourse-cdn.com/v4/letter/o/3ec8ea/32.png) [@OCamlUser](https://discuss.ocaml.org/u/OCamlUser)\
**Post date:** [April 23, 2022, 2:10am UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719/2 "2022-04-23T02:10:13Z")

</div>

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:

> <https://github.com/ocaml/ocaml/blob/001997e81342fd0d321fd877b73608150601e7d9/runtime/compare.c>

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

> **[The perils of polymorphic compare](https://blog.janestreet.com/the-perils-of-polymorphic-compare/)**
>
> I have a love-hate relationship with OCaml’s polymorphic comparison functions,which I think I share with a lot of people who use the language. For those whod...

---

<div class="post-metadata">

**Author:** ![giltho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/giltho/32/3521_2.png) [@giltho](https://discuss.ocaml.org/u/giltho)\
**Post date:** [April 23, 2022, 1:51pm UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719/3 "2022-04-23T13:51:38Z")

</div>

Hi!

Thank you for your answer.  
I had read that article, but it is still not exactly clear _why_ this would be slower.

I understand that `equal` functions can be manually optimised of course, but I am talking about the functions derived by ppx.  
Polymorphic compare will compare tags, and if tags are equal, recursively compare the fields. But so will the equal functions derived by ppx won’t it?

But I think I get my answer from the code you linked however: polymorphic compare also has to decide on the type of what it’s comparing before being able to perform comparison, which obviously is slower. I could have guessed it probably hahaha

Thank you!

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [April 23, 2022, 4:49pm UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719/4 "2022-04-23T16:49:10Z")

</div>

There are two main reasons why the polymorphic comparison is slower. The first one is that it’s implemented in C, not in OCaml. In bytecode it’s not really worse (in fact, it’s likely better in many cases) but for native code switching from OCaml code to C code is relatively slow, so if your comparison function isn’t too complicated it’s usually faster to use a pure OCaml version. Plus, a pure OCaml function can be considered for inlining, which can again improve performance

The second reason is specialisation. The polymorphic version has to consider all possible cases that are valid for any given type, while a hand-written one (or ppx-generated one) only has to deal with the cases allowed for the particular type it’s written for.  
It’s not as much a problem as the other point though, in particular since for a number of base types the specialisation is performed automatically by the compiler (i.e. `Stdlib.compare (x : int) y` is reasonably fast). Even when that doesn’t work, it’s usually only a few comparisons that you can skip by knowing the type.

---

<div class="post-metadata">

**Author:** ![cdaringe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cdaringe/32/2419_2.png) [@cdaringe](https://discuss.ocaml.org/u/cdaringe)\
**Post date:** [April 23, 2022, 5:41pm UTC](https://discuss.ocaml.org/t/why-is-a-hand-written-equal-function-faster-than/9719/5 "2022-04-23T17:41:13Z")

</div>

Thx for the ref. It’s just what I had always imagined it to be in my head… and more!
