From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 00312BB81 for ; Sun, 27 Nov 2005 10:53:29 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAR9rTTn013856 for ; Sun, 27 Nov 2005 10:53:29 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA17537 for ; Sun, 27 Nov 2005 10:53:28 +0100 (MET) Received: from irma.motion-twin.com (irma.motiontwin.com [213.186.50.39]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAR9rSP4013853 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sun, 27 Nov 2005 10:53:28 +0100 Received: from [82.225.176.25] (helo=[192.168.0.1]) by irma.motion-twin.com with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.54) id 1EgJDo-0001LY-Eu; Sun, 27 Nov 2005 10:53:24 +0100 Message-ID: <4389844B.90008@motion-twin.com> Date: Sun, 27 Nov 2005 11:02:51 +0100 From: Nicolas Cannasse User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Brian Hurt Cc: "Michael D. Adams" , caml-list@inria.fr Subject: Re: [Caml-list] Efficency of varient types References: <4387ACC9.2040107@motion-twin.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 43898219.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 43898218.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; cannasse:01 ncannasse:01 motion-twin:01 caml-list:01 ocaml:01 bool:01 compiler:01 unboxed:01 compiler:01 variants:01 bool:01 casted:01 runtime:01 variants:01 unboxed:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 Brian Hurt wrote: > > > 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. Exactly. So actually the custom bool can be "safely" casted to the corresponding real bool since they have same runtime representation. >> 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. It's a bit more complex. A variant have a header storing : - it's tag - some GC bits - it's size (number of fields) Then the data is followed by N fields which are the variant parameters. That's why there is a semantical difference between Tag of int * int and Tag of (int * int) In the first case it's a 2 fields block, in the second it's a on-field block storing a 2 fields tuple. I think that the compiler is pretty good at unboxing floats and maybe int32 when doing some numerical calculus. Other optimizations are more tricky. Nicolas