English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Functors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-03 (18:42)
From: Jon Harrop <jdh30@c...>
Subject: Re: [Caml-list] Functors
On Monday 03 May 2004 17:02, Julien Signoles wrote:
> As far as I test ocamldefun, performance improvement is very dependent of
> the application. Mainly, the defunctorized code is generally much more
> efficient than the code with functors if
> (1) some functions are not inlined due to functors; and
> (2) these functions are called very often.
> Generally, in the others case, the defunctorization do not improve
> performance a lot.

Absolutely, but it can result in a huge performance increase in some cases. 
Although these cases may seem to be insignificant to most people round these 
parts, I believe they would be a make-or-break for someone considering ocaml 
for numerical work. The most obvious example would be to use functors to 
partially specialise code for a primitive vector or geometric type. When I 
did something equivalent in C++ using templates I saw a 2-3 times performance 
improvement. I think it would be a shame if people had to resort to using 
"cat" to build their ocaml source files before compiling...

> Some SML compilers include a defunctorizer... But, in a lot of cases,
> functors do not reduce performance a lot (as explained above). Morever,
> static analysis tools do not generally correctly work on functorized
> programs: so a source-to-source defunctorizer as ocamldefun can be used
> by these tools while a defunctorizer directly included in the compiler
> cannot. So i'm not sure including a defunctorizer in the compiler is a
> good thing...

I didn't mean including the defunctorizor in the compiler, just the 
functionality which it provides. Perhaps I am mistaken, but can these 
optimisations not be done after all of the analysis, as a relatively simple 
extension to the current inlining optimisations?

I was afraid that the ocamldefun example might be out of date so I wrote my 
own little version (see end). To my surprise, this example runs 15-18 times 
slower when generated via a functor! I'd be interested to hear if other 
people get similar results.


module type FUNC = sig val f : int -> int -> int end
module Func : FUNC = struct let f a b = a / b end
module FuncFunc = functor (F : FUNC) -> struct let f = F.f end
module MyFunc = FuncFunc (Func)

let f1 a b = a / b

let _ =
  let t = Unix.gettimeofday () in
  for i=0 to 1000000000 do
    ignore (f1 12345 8);
  print_string ("Same compilation unit took "^(string_of_float 
((Unix.gettimeofday ()) -. t)));
  print_newline ();
  let t = Unix.gettimeofday () in
  for i=0 to 1000000000 do
    ignore (MyFunc.f 12345 8);
  print_string ("Same compilation unit took "^(string_of_float 
((Unix.gettimeofday ()) -. t)));
  print_newline ();

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners