caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Nicolas Ojeda Bar <nicolas.ojeda.bar@lexifi.com>
To: Mark Hayden <markghayden@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] OCaml optimization
Date: Mon, 30 Nov 2015 00:28:08 +0100 (CET)	[thread overview]
Message-ID: <2032001085.281825.1448839688192.JavaMail.zimbra@lexifi.com> (raw)
In-Reply-To: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com>

Dear Mark,

I would like to suggest a third way to write array summation which
1) avoids the closure allocations, 2) is very natural, and 3) does not require much cleverness from the compiler.

Additionally, this form plays well with the float unboxing heuristics in the compiler (in the case when you are adding floats):

let sum2 a =
  let s = ref 0 in
  for i = 0 to Array.length a - 1 do
    s := !s + a.(i)
  done;
  !s

Best wishes,
Nicolas

----- Original Message -----
From: "Mark Hayden" <markghayden@yahoo.com>
To: caml-list@inria.fr
Sent: Sunday, 29 November, 2015 7:16:57 PM
Subject: [Caml-list] OCaml optimization

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 ;
;;







-- 
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

  reply	other threads:[~2015-11-29 23:28 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 [this message]
2015-11-30  9:53   ` Mark Shinwell

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=2032001085.281825.1448839688192.JavaMail.zimbra@lexifi.com \
    --to=nicolas.ojeda.bar@lexifi.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;
as well as URLs for NNTP newsgroup(s).