Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] lazy vs function for values that are used once at most
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Frederic van der Plancke <fvdp@d...>
Subject: Re: [Caml-list] lazy vs function for values that are used once at most
henri dubois-ferriere wrote:
> 
> ok.
> actually, i should refine my question a little further:
> in the vast majority of cases, the value is *not* used at all.
> so i suppose the memoization overhead you mention does not occur when
> a lazy value is not forced.
> 
> so, the question then becomes:
> 
> any difference in overhead between creating
> (lazy v)
> and
> (fun () -> v)
> 
> ?
> thanks for any insights
> henri

Sure. The value (lazy ...) is something significantly more complex than (fun () ->
...) and contains a (fun () -> ...) internally anyway. So it has overhead.

See the source (lazy.ml) if you want to be sure and precise. For instance (lazy 33)
has type (int Lazy.t) where Lazy.t is defined as such:

type 'a status =
  | Delayed of (unit -> 'a)
  | Value of 'a
  | Exception of exn
;;

type 'a t = 'a status ref;;

So before computation is forced, (lazy 33) = ref (Delayed (fun () -> 33)) which means
2 levels of indirection more -> somewhat slower access, 2 words of memory waste, more
strain on the GC.

Frédéric.

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