From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by yquem.inria.fr (Postfix, from userid 18041) id 5DE99BC57; Tue, 8 Jun 2010 11:51:52 +0200 (CEST) Date: Tue, 8 Jun 2010 11:51:52 +0200 To: David Allsopp Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Converting variants with only constant constructors to integers Message-ID: <20100608095152.GA11227@yquem.inria.fr> References: <4C0D3557.1020200@gmail.com> <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> <4C0D4C88.4070102@gmail.com> <877hma78xf.fsf@gmail.com> <013a01cb06de$32c6d930$98548b90$@romulus.metastack.com> <015201cb06ea$f06adc00$d1409400$@romulus.metastack.com> <20100608093651.GA8296@yquem.inria.fr> <015f01cb06ef$500b4f60$f021ee20$@romulus.metastack.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <015f01cb06ef$500b4f60$f021ee20$@romulus.metastack.com> User-Agent: Mutt/1.5.9i From: maranget@yquem.inria.fr (Luc Maranget) X-Spam: no; 0.00; variants:01 constructors:01 integers:01 maranget:01 maranget:01 compiler:01 hashtable:01 compiler:01 lookup':01 ocamlopt:01 dreamt:98 wrote:01 luc:01 luc:01 caml-list:01 > Luc Maranget wrote: > > Of course, if you have the following: > > > > > > type t = A | B | C > > > let int_of_t = function > > > A -> 0 > > > | B -> 1 > > > | C -> 2 > > > > > > Then in fact I believe that the compiler already converts that to a > > > hashtable lookup instead of a sequence of jumps.. > > > > The compiler does not convert the above code to 'hashtable lookup'. > > Is there a point where the compiler does do a table lookup for matches > rather than jumps or have I clearly just dreamt that? :o) > > > David As far as I know the compiler always output jumps for matches. Those jumps can be conditional jumps, or indirect jumps. For instance in the case of your code, ther will be two condional jumps (ocamlopt) With a bigger example, say | A -> 0 ... | Z -> 25 It is likely that you get a table of addresses indexed by constructor numbers. -- Luc