Version franaise
Home About Download Resources Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
The Implicit Accumulator: a design pattern using optional arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-06-27 (13:58)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments
On Wednesday 27 June 2007 14:18:07 Quc Peyrot wrote:
> On Jun 27, 2007, at 2:14 PM, Jon Harrop wrote:
> > Provided performance is unimportant...
> Could you be more specifics about the performance hit?

Actually, no. I have no quantitative results but I remember that it is 

> I was quite happy with this solution, but maybe there is something
> more elegant to do?

Nice. :-)

> (I'm still in the process of learning good optimization patterns in
> ocaml which preserve readability)

I was going to mention symbol tables to Raj B for his Python JIT compiler but 
he hasn't gotten back to me yet.

Basically, you memoize strings:

# let symbol =
    let m = Hashtbl.create 1 in
    fun string ->
      try Hashtbl.find m string with Not_found ->
      Hashtbl.add m string string;
val symbol : '_a -> '_a = <fun>

This is another trick I learned whilst writing my Mathematica interpreter (so 
many tricks, so little time). This function looks totally pointless, like a 
no-op, but if you pipe your identifiers through it (e.g. when building the 
AST during parsing) then all structurally-equal strings are the same physical 
string. If you're careful, this lets you use physical equality for string 
comparison and that is a lot faster.

You might do something similar by memoizing temporary strings of different 

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal