Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Efficency of varient types


On Sun, 27 Nov 2005, Michael D. Adams wrote:

> I agree, which is why it was my hope that OCaml might do some of that
> for me.  Consider a home brew bool type, "type mybool = Mytrue |
> Myfalse".  If the compiler were smart enough, it could represent that
> as an unboxed type.

The compiler is smart enough to do that already- variants without 
associated data are represented as ints.

> From there it might be a small step to
> semi-unboxed types such as the one I started this discussion with,
> "type value = Int of int | Bool of bool | String of string".

The problem here is that the variants take two words- one word which says 
which variant it is, and the second word which is the unboxed int, unboxed 
bool, or pointer to the boxed string.

The problem with unboxing multi-word structures is that this would break 
other things.  For example, consider List.rev:

let rev lst =
     let rec loop accum = function
         | h :: t -> loop (h :: accum) t
         | [] -> accum
     in
     loop [] lst
;;

This function has a type of 'a list -> 'a list.  Unlike C++ templates, 
Ocaml only generates one copy of this function that works on all types. 
This is because all the list elements fit into a single word- either their 
unboxed types that fit into a single word (int, boolean, char), or the 
members of the list are just pointers to the real (boxed) data.

Brian