Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Which function is consing?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-07-04 (14:35)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Which function is consing?
On Wednesday 04 July 2007 15:12:38 Christopher L Conway wrote:
> I'm not sure I understand you correctly, but it's highly doubtful that
> consing is the hotspot in your program.

The garbage collector often takes 30% of the CPU but when it is taking 90% of 
the CPU time you want to know where all of the garbage is coming from in 
order to optimize away its allocation. This is typically by avoiding 
overly-eager recomputation or by memoizing results. I believe Joel 
means "allocation" when he says "consing", so he isn't just referring to list 

To solve this problem you need to know which functions in the call tree are 
allocating heavily. The GC module provides the necessary statistics but 
(AFAIK) there are no tools to automate this for OCaml as there are for F# and 
other languages.

So the simple solution is to wrap your functions in checks by hand and log the 
results. This is very tedious. Perhaps someone would like to write a camlp4 
macro that wraps all top-level function definitions with memory profiling 


  let f x = ...

is supplemented with:

  let f x = ...

  let f x =
    let __ = (Gc.stat()).Gc.major_words in
    let f_x = f x in
    gc_log "f" ((Gc.stat()).Gc.major_words - __);

How does that sound?

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal