From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 75CD0BC69 for ; Mon, 25 Jun 2007 18:13:18 +0200 (CEST) Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.212]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5PGDH1T029797 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 25 Jun 2007 18:13:18 +0200 Received: from [80.229.56.224] (helo=beast.local) by ptb-relay01.plus.net with esmtp (Exim) id 1I2rBk-0006p5-OA for caml-list@yquem.inria.fr; Mon, 25 Jun 2007 17:13:17 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Function inlining and functor Date: Mon, 25 Jun 2007 17:07:25 +0100 User-Agent: KMail/1.9.7 References: <200706251622.21625.jon@ffconsultancy.com> <3C66712F-1267-42A5-9D08-BBB9D5D579B1@gmail.com> In-Reply-To: <3C66712F-1267-42A5-9D08-BBB9D5D579B1@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200706251707.25356.jon@ffconsultancy.com> X-Miltered: at discorde with ID 467FE99D.003 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; inlining:01 functor:01 metaocaml:01 higher-order:01 higher-order:01 inlining:01 run-time:01 compilation:01 metaocaml:01 ocaml:01 ocaml:01 frog:98 closures:01 wrote:01 partial:01 On Monday 25 June 2007 16:36:53 you wrote: > How would MetaOCaml help here? You rewrite higher-order functions that accept closures (e.g. Array.fold_left) as higher-order functions that accept code. Then the inlining is up to you. In the case of Array.fold_left, you make partial application generate and compile specialized implementations, giving you a type-specialized closure: let fold_left f = .< fun accu a -> let accu = ref accu in for i=0 to Array.length a - 1 do accu := .~f !accu a.(i) done; !accu >.;; Partial application of "f" now gives you a specialized function: # fold_left .<( +. )>.;; - : ('a, float -> float array -> float) code = . fun a_2 -> let accu_3 = (ref accu_1) in for i_4 = 0 to ((Array.length a_2) - 1) do (accu_3 := ((! accu_3) +. a_2.(i_4))) done; (! accu_3)>. Run-time compilation of this gives you the efficiency of inlining and, therefore, type specialization: # .!(fold_left .<( +. )>.);; - : float -> float array -> float = Instead of: Array.fold_left ( +. ) 0. You write: .!(fold_left .<( +. )>.) 0. a which is twice as fast on my machine. MetaOCaml rocks. I do hope it becomes mainstream... :-) -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. The OCaml Journal http://www.ffconsultancy.com/products/ocaml_journal/?e