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
Efficency of varient types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-28 (00:17)
From: Christophe Raffalli <raffalli@u...>
Subject: Obj or not Obj

> As a general rule, unless you work at INRIA, you should never, ever 
> use the Obj module.  Not only will the code be signifigantly less 
> readable, it'll also be signifigantly more error prone, especially 
> obscure, hard to debug unless you are intimately familiar with the 
> inners of the garbage collector, segfault you program at random times, 
> boy doesn't this feel like C++ bugs.

As nicolas Canasse already said this is a too strong statement, that 
should/could be commented by somone else at INRIA
(otherwise, make install should not install obj at all ;-). Here are a 
few reason

- using Obj requires the same knowledge as writing C interface ... and C 
is more obscure that OCaml's code with Obj.
For instance, my bindlib library for bound variables would be much more 
error prone in C .. and by the way what do you think of
software distributed as patch for Ocaml like FreshOcaml ...

- then Obj is sometimes necessary, typically
  - when trying to have OCaml accept programs that it thinks wrongly to 
be not typable (like programs extracted from Coq proof)
  - when you have an "environment" (list, array, etc ...) that needs to 
contain variables of various AND unknown type (unkown because you are
    writing libraries or general tools, this is the case of bindlib, 
code genarated by ocamlyacc and  many others). Moreover, all these programs
    would be  typable using dependent types ...
  - for all the examples above using C would be stupid ! especially for 
ocamlyacc ;-)

Nevertheless, I almost agree, you should just make sure before using Obj 
  - you read and learn a lot about the runtime
  - you can not find another way round, even if it cost a little time

Just a small word about one of my bad experiment with Obj about tail rec 
map ... it is easy to get a tail
rec map using Obj to make ocaml's list mutable ... it is just slower 
that the original map and using roughly the same
amount of memory, because every cons cell  you gain ... is now in the 
list of grey val as soon as your list does not
fit in the minor heap anymore !! So even writing you own mutable_list 
type would be as bad !

Explanation: the list of grey val is a list of pointers from the major 
heap to the minor heap which
are created when using mutable data structure and which would break 
incremental GC if each minor GC did not scan
the list of grey val) ...

This also does mean that you should know a lot about the runtime when 
using mutable data structures and caring about performance !!
So Obj or not Obj (or mutable or not mutable) is always a hard question 
that needs time ...
but definitely, I think the above sentence is too strong.