Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: William <r.3@libertysurf.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] caml match optimization
Date: Wed, 17 Oct 2012 00:17:29 +0200	[thread overview]
Message-ID: <CAPFanBHyT5ZsPz=X5mYW9Znyz5zKwWKt6f44Y1oKgWDZbw5e1w@mail.gmail.com> (raw)
In-Reply-To: <507DD785.8030300@libertysurf.fr>

You can just test the current behavior of the compiler.

% cat test.ml
let apply2 apply_all_elements foo bar baz biz = function
| `Foo -> apply_all_elements foo foo
| `Bar -> apply_all_elements bar bar
| `Baz -> apply_all_elements baz baz
| `Biz -> apply_all_elements biz biz

% ocamlopt -dlambda -c test.ml
(seq
  (let
    (apply2/1030
       (function apply_all_elements/1031 foo/1032 bar/1033 baz/1034 biz/1035
         param/1037
         (if (>= param/1037 3305651)
           (if (>= param/1037 3505894)
             (apply apply_all_elements/1031 foo/1032 foo/1032)
             (apply apply_all_elements/1031 biz/1035 biz/1035))
           (if (>= param/1037 3303867)
             (apply apply_all_elements/1031 baz/1034 baz/1034)
             (apply apply_all_elements/1031 bar/1033 bar/1033)))))
    (setfield_imm 0 (global Test!) apply2/1030))
  0a)


Note that using regular variants instead of polymorphic variants gives
you a rather more efficient dispatch (because the variant tags are
represented as continguous small integers rather than integers
resulting from a hashing process):

% cat test2.ml
type tag = Foo | Bar | Baz | Biz

let apply2 apply_all_elements foo bar baz biz = function
| Foo -> apply_all_elements foo foo
| Bar -> apply_all_elements bar bar
| Baz -> apply_all_elements baz baz
| Biz -> apply_all_elements biz biz

% ocamlopt -dlambda -c test2.ml
(seq
  (let
    (apply2/1039
       (function apply_all_elements/1040 foo/1041 bar/1042 baz/1043 biz/1044
         param/1051
         (switch* param/1051
          case int 0: (apply apply_all_elements/1040 foo/1041 foo/1041)
          case int 1: (apply apply_all_elements/1040 bar/1042 bar/1042)
          case int 2: (apply apply_all_elements/1040 baz/1043 baz/1043)
          case int 3: (apply apply_all_elements/1040 biz/1044 biz/1044))))
    (setfield_imm 0 (global Test2!) apply2/1039))
  0a)


On Tue, Oct 16, 2012 at 11:54 PM, William <r.3@libertysurf.fr> wrote:
> Hello,
> I have this code sample :
>
> let apply_foo         = apply_all_elements foo foo_struct
> let apply_bar         = apply_all_elements bar bar_struct
> let apply_baz         = apply_all_elements baz baz_struct
> [...]
> let apply_biz         = apply_all_elements biz biz_struct
>
>
>
> If I make a function such as :
>
> let apply2 = function
> | `Foo -> apply_all_elements foo foo_struct
> | `Bar -> apply_all_elements bar bar_struct
> | `Baz -> apply_all_elements baz baz_struct
> [...]
> | `Biz -> apply_all_elements biz biz_struct
>
>
> How much is "apply2" inefficient ?
> does caml tests for 20 elements if `Biz is number 21 ?
> Or does ocaml try to convert the list of elements in a tree internally ? (to
> optimise access) or something else ?
>
> Regards,
> William
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

      parent reply	other threads:[~2012-10-16 22:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-16 21:54 William
2012-10-16 22:14 ` Gabriel Kerneis
2012-10-16 22:17 ` Gabriel Scherer [this message]

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='CAPFanBHyT5ZsPz=X5mYW9Znyz5zKwWKt6f44Y1oKgWDZbw5e1w@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=r.3@libertysurf.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