caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Francois Pottier <Francois.Pottier@inria.fr>
To: Christopher Jeris <cjeris@math.mit.edu>
Cc: caml-list@inria.fr
Subject: Re: [caml] Closures and efficiency
Date: Fri, 29 Oct 1999 11:39:18 +0200	[thread overview]
Message-ID: <19991029113918.23521@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.SUN.4.20.9910261748340.6977-100000@schauder.mit.edu>; from Christopher Jeris on Tue, Oct 26, 1999 at 06:06:28PM -0400


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/




      parent reply	other threads:[~1999-10-29 17:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-10-26 22:06 Christopher Jeris
1999-10-29  8:56 ` Xavier Leroy
1999-10-29  9:39 ` Francois Pottier [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19991029113918.23521@pauillac.inria.fr \
    --to=francois.pottier@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=cjeris@math.mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).