Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Julien Signoles <Julien.Signoles@l...>
Subject: Re: [Caml-list] Functors
> > 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.

It is what I mean too ;-). Sorry, my English is really perfectible.

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'm not sure because functors add closures which are difficult to inline.

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

I test your example. Here are my results on a PIII 700 MHz, 64Mo, using
ocamlopt.opt v3.06:

==========
without using ocamldefun:

Same compilation unit took 5.50491905212
Same compilation unit took 49.5608668327
55.000u 0.010s 0:55.07 99.8%    0+0k 0+0io 141pf+0w
==========

As you said, this example is very slow when generated via a functor.

==========
using ocamldefun:

Same compilation unit took 5.50432610512
Same compilation unit took 5.50287103653
11.010u 0.000s 0:11.01 100.0%   0+0k 0+0io 140pf+0w
==========

As you can see: no more loss of performance :-).

Thank's a lot, I keep your example: it is really interesting for me.

Cheers,
Julien Signoles

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