From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id BF76DBC57 for ; Tue, 8 Jun 2010 12:22:00 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoMCAGu2DUxRZ90wkWdsb2JhbACSL4wSFQEBAQEJCwoHEQMfvRmFFgQ X-IronPort-AV: E=Sophos;i="4.53,383,1272837600"; d="scan'208";a="60864255" Received: from mtaout02-winn.ispmail.ntl.com ([81.103.221.48]) by mail1-smtp-roc.national.inria.fr with ESMTP; 08 Jun 2010 12:21:56 +0200 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20100608102152.NUGL3192.mtaout02-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Tue, 8 Jun 2010 11:21:52 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20100608102152.OMWC1593.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Tue, 8 Jun 2010 11:21:52 +0100 Received: from Tenor ([172.16.0.8]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id o58ALmkc012947 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Tue, 8 Jun 2010 11:21:49 +0100 From: "David Allsopp" To: Cc: 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> <20100608095152.GA11227@yquem.inria.fr> In-Reply-To: <20100608095152.GA11227@yquem.inria.fr> Subject: RE: [Caml-list] Converting variants with only constant constructors to integers Date: Tue, 8 Jun 2010 11:21:48 +0100 Message-ID: <016001cb06f4$67b4cc40$371e64c0$@romulus.metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQK3OCAoaAveGworypJxQY6jcfYL4QG5R1OKAl4LCCQB8HFXWwG5f4A0AZ1ERFMCOcHmgwHorL8MAgkoyREB2kERuAJU9l/KAgcQfpM= Content-Language: en-gb Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=1ggfb5FlKZQUfF3vzm9UBYZ2uTfLsbs/8dSljwg5+mE= c=1 sm=0 a=dfm47NNIjPMA:10 a=VZGMvYHv9TIA:10 a=8sW17jYxVu0A:10 a=kj9zAlcOel0A:10 a=vlD4ua7Rn1kEC0pVq5kA:9 a=E-59dMHxGvrJR6rG9jQA:7 a=WHYYyK8qjlPEwVuBU5vLYN0D8JcA:4 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 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 constructors:01 dreamt:98 wrote:01 wrote:01 luc:01 Luc Maranget wrote: > > 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. I think that's probably what I was getting muddled with - I didn't realise that the compiler doesn't do that all the time for constant constructors, though. David