Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Mark Shinwell <mshinwell@janestreet.com>
To: Mark Hayden <markghayden@yahoo.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] OCaml optimization
Date: Mon, 30 Nov 2015 09:53:13 +0000	[thread overview]
Message-ID: <CAM3Ki75y5R_7oz7ejDt1OckdhC0wOkZY3FiD_6MWb7HEH=e4Qg@mail.gmail.com> (raw)
In-Reply-To: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com>

The forthcoming version of OCaml (4.03) with the flambda optimization
passes can compile the example "sum0" to code that does not allocate.
(The particular optimization that enables that probably won't be
enabled by default in 4.03, though.)

Mark

On 29 November 2015 at 18:16, Mark Hayden <markghayden@yahoo.com> wrote:
> Below are two ways to write array summation in OCaml, sum0 and sum1.  Both
> of them use recursive functions.  ’sum0' allocates a closure on each call
> but I think is more natural.  ‘sum1’ does do any allocation, but is awkward
> to write.  Is there a compiler flag or other way to get the OCaml native
> compiler to avoid closure allocation in functions in the form of ‘sum0’?
>
> best, Mark
>
>
> Running each function 1000 times show sum0 is allocating 6 words on each
> call.
>
> % ocamlopt -o loop0 -S -inline 10 -unsafe loop0.ml
> ./loop0
> sum0:6029
> sum1:23
>
>
>
> open Printf ;;
>
> (* Allocates
>  *)
> let sum0 a =
>   let len = Array.length a in
>   let rec loop ofs sum =
>     if ofs < len then (
>       loop (succ ofs) (sum + a.(ofs))
>     ) else (
>       sum
>     )
>   in
>   loop 0 0
> ;;
>
>
> let rec sum1_help a len ofs sum =
>   if ofs < len then (
>     sum1_help a len (succ ofs) (sum + a.(ofs))
>   ) else (
>     sum
>   )
> ;;
>
> (* Does not allocate
>  *)
> let sum1 a =
>   let len = Array.length a in
>   sum1_help a len 0 0
> ;;
>
>
> let test which sum =
>   let a = Array.make 123 123 in
>
>   let minor0 = (Gc.stat ()).Gc.minor_words in
>
>   for ofs = 0 to 1000 do
>     ignore (sum a)
>   done ;
>
>   let minor1 = (Gc.stat ()).Gc.minor_words in
>
>   printf "%s:%.0f\n" which (minor1 -. minor0) ;
> ;;
>
> let _ =
>   test "sum0" sum0 ;
>   test "sum1" sum1 ;
> ;;
>
>
>
>
>
>

      parent reply	other threads:[~2015-11-30  9:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20151129110020.D92E17FD40@sympa.inria.fr>
2015-11-29 18:16 ` Mark Hayden
2015-11-29 23:28   ` Nicolas Ojeda Bar
2015-11-30  9:53   ` Mark Shinwell [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='CAM3Ki75y5R_7oz7ejDt1OckdhC0wOkZY3FiD_6MWb7HEH=e4Qg@mail.gmail.com' \
    --to=mshinwell@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=markghayden@yahoo.com \
    /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