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 E04A8BB9C for ; Sat, 26 Nov 2005 01:21:44 +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 jAQ0Li3E027617 for ; Sat, 26 Nov 2005 01:21:44 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA21602 for ; Sat, 26 Nov 2005 01:21:43 +0100 (MET) Received: from irma.motion-twin.com (irma.motiontwin.com [213.186.50.39]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jAQ0Lh4x030033 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sat, 26 Nov 2005 01:21:43 +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 1Efnoz-00011A-14; Sat, 26 Nov 2005 01:21:41 +0100 Message-ID: <4387ACC9.2040107@motion-twin.com> Date: Sat, 26 Nov 2005 01:31:05 +0100 From: Nicolas Cannasse User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: "Michael D. Adams" Cc: caml-list@inria.fr Subject: Re: [Caml-list] Efficency of varient types References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4387AA98.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 4387AA97.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 ackermann:01 ackermann:01 ocamlopt:01 ocaml:01 model:01 runtime:01 unboxed:01 pointers:01 variants:01 booleans: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 Michael D. Adams wrote: > I have recently learned about OCaml and have been impressed by how > fast it is in the benchmarks. However I have discovered that variant > types can slow down a program quite a bit. For example, consider the > Ackermann function implemented for int (see program 1) and the same > Ackermann function implemented for a "type value = Int of int | String > of int" (program 2). The second one is ten times slower! (Using > ocamlopt.) In order to understand what there is such difference, it's useful to learn the ocaml memory model at runtime : - int are 31 bits unboxed value with last bit set to 1 in order to differenciate them with GC allocated pointers. - tagged variants are GC allocated blocks with a discriminating "tag" in the header. - chars and booleans are integers at runtime The second bit is used to mark an exception but it's only internal and temporary when dealing with callbacks. If you have a tagged variant where all constructors have a parameter, you can use Obj module to unbox the Int variant but the code is a lot less readable. Nicolas