caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Arlen Cuss <celtic@sairyx.org>
To: caml-list@inria.fr
Subject: [Caml-list] Hashtbl: find and find_rec
Date: Mon, 07 Mar 2011 17:39:50 +1100	[thread overview]
Message-ID: <1299479990.22876.15.camel@azayaka> (raw)

Hi all,

I'm puzzling over the OCaml source, and am trying to work out the reason
for the following definition:

----8<----
let rec find_rec key = function
    Empty ->
      raise Not_found
  | Cons(k, d, rest) ->
      if compare key k = 0 then d else find_rec key rest

let find h key =
  match h.data.((hash key) mod (Array.length h.data)) with
    Empty -> raise Not_found
  | Cons(k1, d1, rest1) ->
      if compare key k1 = 0 then d1 else
      match rest1 with
        Empty -> raise Not_found
      | Cons(k2, d2, rest2) ->
          if compare key k2 = 0 then d2 else
          match rest2 with
            Empty -> raise Not_found
          | Cons(k3, d3, rest3) ->
              if compare key k3 = 0 then d3 else find_rec key rest3
----8<----

My only guess thus far is that it's cheaper if we don't recurse (stack
doesn't grow as much, fewer branches), and thus a few levels of testing
are put in for good measure to speed up the case where it's one of the
first three items.

Is this it? Or am I missing something else?

Cheers,
Arlen


             reply	other threads:[~2011-03-07  6:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-07  6:39 Arlen Cuss [this message]
2011-03-07 12:00 ` Alexandre Pilkiewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1299479990.22876.15.camel@azayaka \
    --to=celtic@sairyx.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).