Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Module (re)construction cost
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <markus@m...>
Subject: Re: [Caml-list] Module (re)construction cost
On Fri, 27 Jul 2001, Xavier Leroy wrote:
> > i.e. does functor
> > application cost anything (time ? memory ?).
> 
> Evaluating the functor application itself is very cheap, it just
> builds a tuple of values from a tuple of values.  Moreover, this takes
> place at program start-up time, i.e. not often.  The main hidden cost
> is the lack of optimization in certain function calls as described
> above.

Unless this functor application is used to create a local module, of
course. Then each call to the enclosing function (and if control flow
actually reaches the functor application) will require the creation of
this tuple.

It should also be noted that this may mean evaluating possibly expensive
expressions whose values will be put into the tuple slots. In fact,
you could even have expressions there that do not terminate at all
or eat up all your memory depending on the parameters passed via the
functor parameter. E.g.:

  module type VICE = sig
    val sleep : bool
    val munch : bool
  end

  module Make (Vice : VICE) = struct
    let _ =
      if Vice.munch then
        let stomach = ref [] in
        for i = 1 to 10000000 do
          stomach := () :: !stomach
        done;
      if Vice.sleep then while true do () done
  end

  let sin sleep munch =
    let module Immoderateness = struct
      let munch = munch
      let sleep = sleep
    end in
    let module Cockaigne = Make (Immoderateness) in
    ()

  let _ = sin true true

First the functor application will eat a good share of your memory. Then
it will put your machine to sleep forever...

Regards,
Markus Mottl

-- 
Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr