# Hashtbl find\_opt function

**URL:** <https://discuss.ocaml.org/t/hashtbl-find-opt-function/3615>\
**Category:** Learning\
**Tags:** hashtbl\
**Created:** [April 2, 2019, 6:41pm UTC](https://discuss.ocaml.org/t/hashtbl-find-opt-function/3615 "2019-04-02T18:41:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloudyhug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cloudyhug/32/2399_2.png) [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Post date:** [April 2, 2019, 6:41pm UTC](https://discuss.ocaml.org/t/hashtbl-find-opt-function/3615/1 "2019-04-02T18:41:29Z")

</div>

I found this code for the `Hashtbl.find_opt` function while looking at the implementation of Hashtbl in the standard library.

```ocaml
let rec find_rec_opt key = function
  | Empty ->
      None
  | Cons{key=k; data; next} ->
      if compare key k = 0 then Some data else find_rec_opt key next

let find_opt h key =
  match h.data.(key_index h key) with
  | Empty -> None
  | Cons{key=k1; data=d1; next=next1} ->
      if compare key k1 = 0 then Some d1 else
      match next1 with
      | Empty -> None
      | Cons{key=k2; data=d2; next=next2} ->
          if compare key k2 = 0 then Some d2 else
          match next2 with
          | Empty -> None
          | Cons{key=k3; data=d3; next=next3} ->
              if compare key k3 = 0 then Some d3 else find_rec_opt key next3

```

Does anybody know why they chose to manually perform a pattern matching on the 3 first buckets before calling `find_rec_opt` ? I suppose it is for performance reasons, but isn’t tail recursion optimised by the compiler ? How does a manual match impact performance on a positive way ?

Thanks for your answers.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 2, 2019, 7:04pm UTC](https://discuss.ocaml.org/t/hashtbl-find-opt-function/3615/2 "2019-04-02T19:04:52Z")

</div>

This looks like [loop unrolling](https://en.wikipedia.org/wiki/Loop_unrolling).
