From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA12884 for caml-redistribution; Fri, 29 Oct 1999 19:21:28 +0200 (MET DST) 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 LAA22415 for ; Fri, 29 Oct 1999 11:39:22 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id LAA10647; Fri, 29 Oct 1999 11:39:19 +0200 (MET DST) Received: (from fpottier@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA19767; Fri, 29 Oct 1999 11:39:18 +0200 (MET DST) Message-ID: <19991029113918.23521@pauillac.inria.fr> Date: Fri, 29 Oct 1999 11:39:18 +0200 From: Francois Pottier To: Christopher Jeris Cc: caml-list@inria.fr Subject: Re: [caml] Closures and efficiency References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Mailer: Mutt 0.89.1 In-Reply-To: ; from Christopher Jeris on Tue, Oct 26, 1999 at 06:06:28PM -0400 Sender: weis Hello, > Here's the nub of my question. In a typical synth architecture there are > very many parameters which take values 0..127. If I had a function > > int_subrange_encoder: int -> int -> param_value -> bytestring > > which took the bounds of a subrange and generated an encoder function for > values of that subrange, and then in a voice-architecture description I > had a list of very many records each of which contained an entry > > int_subrange_encoder 0 127 > > would I suddenly have six million little closures, or would the compiler > do common-subexpression elimination on them ? I don't know if the compiler performs CSE for expressions which involve function applications (it would have to prove that the function int_subrange_encoder does not perform side effects, which I think it doesn't -- but I could be wrong). However, if you're worried by memory consumption, you can easily create a memoizing version of int_subrange_encoder. Here is a generic "memoize" function which accepts any 1-parameter function f and returns a memoizing version of it. (You can adapt it for 2 arguments if you wish, or uncurry the function int_subrange_encoder, as I have done below.) let memoize f = let table = Hashtbl.create 1023 in fun argument -> try Hashtbl.find table argument with Not_found -> let result = f argument in Hashtbl.add table argument result; result Of course, memoizing a function f only makes sense if it is applicative, i.e. it returns the same result when applied twice, and it performs no observable side effects. You can write: let int_subrange_encoder (x, y) = ... let memoized_int_subrange_encoder = memoize int_subrange_encoder Hope this helps, -- François Pottier Francois.Pottier@inria.fr http://pauillac.inria.fr/~fpottier/