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 F0AC4BB9C for ; Sat, 26 Nov 2005 02:23:42 +0100 (CET) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAQ1NgL0031708 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sat, 26 Nov 2005 02:23:42 +0100 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1Efomz-0006uv-Vc for caml-list@yquem.inria.fr; Sat, 26 Nov 2005 01:23:42 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Efficency of varient types Date: Sat, 26 Nov 2005 01:18:43 +0000 User-Agent: KMail/1.8.2 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200511260118.44430.jon@ffconsultancy.com> X-Miltered: at nez-perce with ID 4387B91E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 ocaml:01 char:01 char:01 statically:01 compiler:01 vastly:01 optimising:01 compiler:01 transforming:01 unboxing:01 compilers:01 pointer:01 pointer: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.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Friday 25 November 2005 23:53, Michael D. Adams wrote: > I am working on a program that translates code from scheme into ocaml. > (Later I will try python into ocaml.) Because it is a dynamicly > typed language, the most natural translation would make everything a > function of a large variant type like: > > type value = Int of int > > | Char of char > | String of string Yes, exactly. > However the point of my project is to translate into *efficient* ocaml > code so I would like to avoid the ten times slow down that I've seen > with variant types. This is the main reason why dynamically typed languages are typically much slower than statically typed languages. Look at my ray tracer comparison: http://www.ffconsultancy.com/free/ray_tracer/languages.html Adding type declarations makes the Lisp implementation much faster because, without them and without static typing, the Lisp compiler (SBCL) resorts to boxing almost everything as you have done. The Stalin-compiled Scheme implementation is vastly faster than the SBCL-compiled Lisp implementation for many reasons. One of the most important reasons is that Stalin is a whole-program optimising compiler and spends a significant part of its 10 minute compile time removing as much boxing as possible. > Is there is a way to make program 2 as fast as program 1? (Still > keeping the variant type of course.) No. Transforming from version 2 to version 1 (unboxing) is exactly what the optimisers in the compilers of dynamically typed languages are doing. > Why is program 2 so slow? Is it the cost of heap allocation? The GC? > Having to access memory instead of registers? Constructor matching? All of the above. > One trick that I've thought of would be to push some of the tag > information into the pointer. For example an Int would be represented > exactly like an int (i.e. an odd address), but a String would be > represented as a pointer to a block (i.e. an even address). I could > hack around with the Obj module (indeed some experiments with that > show very good performance), but it would be nice if there were a > cleaner way such as a way to annotate constructors so OCaml tries to > avoid using blocks for them. (Stuffing tags into pointers is very > common in the Scheme world.) You would probably be better off trying to apply the unboxing optimisations (e.g. that Stalin does). -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists