caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Function inlining and functor
Date: Mon, 25 Jun 2007 17:07:25 +0100	[thread overview]
Message-ID: <200706251707.25356.jon@ffconsultancy.com> (raw)
In-Reply-To: <3C66712F-1267-42A5-9D08-BBB9D5D579B1@gmail.com>

On Monday 25 June 2007 16:36:53 you wrote:
> How would MetaOCaml help here?

You rewrite higher-order functions that accept closures (e.g. Array.fold_left) 
as higher-order functions that accept code. Then the inlining is up to you.

In the case of Array.fold_left, you make partial application generate and 
compile specialized implementations, giving you a type-specialized closure:

let fold_left f =
  .<
    fun accu a ->
      let accu = ref accu in
      for i=0 to Array.length a - 1 do
        accu := .~f !accu a.(i)
      done;
      !accu
  >.;;

Partial application of "f" now gives you a specialized function:

# fold_left .<( +. )>.;;
- : ('a, float -> float array -> float) code =
.<fun accu_1 ->
   fun a_2 ->
    let accu_3 = (ref accu_1) in
    for i_4 = 0 to ((Array.length a_2) - 1) do
     (accu_3 := ((! accu_3) +. a_2.(i_4)))
    done;
    (! accu_3)>.

Run-time compilation of this gives you the efficiency of inlining and, 
therefore, type specialization:

# .!(fold_left .<( +. )>.);;
- : float -> float array -> float = <fun>

Instead of:

  Array.fold_left ( +. ) 0.

You write:

  .!(fold_left .<( +. )>.) 0. a

which is twice as fast on my machine. MetaOCaml rocks. I do hope it becomes 
mainstream... :-)

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e


  parent reply	other threads:[~2007-06-25 16:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-25 14:39 Quôc Peyrot
2007-06-25 15:22 ` [Caml-list] " Jon Harrop
2007-06-25 15:36   ` Joel Reymont
2007-06-25 15:41     ` Basile STARYNKEVITCH
2007-06-25 15:50       ` Quôc Peyrot
2007-06-25 15:58         ` Jon Harrop
2007-06-25 17:04           ` Quôc Peyrot
2007-06-26  3:22             ` Jon Harrop
2007-06-25 16:07     ` Jon Harrop [this message]
2007-06-25 20:11       ` Joel Reymont
2007-06-26  3:22         ` Jon Harrop

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=200706251707.25356.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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).