[MLton] Callback functions: how?

Vesa Karvonen vesa.karvonen at cs.helsinki.fi
Thu Feb 15 05:31:39 PST 2007

Quoting Vesa Karvonen <vesa.karvonen at cs.helsinki.fi>:

> Quoting "Wesley W. Terpstra" <wesley at terpstra.ca>:
> > Vesa Karvonen wrote:
> > > Basically, there is a single exported ML callback function and a
> > > callback cache for each imported C function that takes callbacks.
> > > The key generated by the cache is given to the C side as the
> > > context pointer (uarg in your snippet).
> > This sounds pretty much the same as my solution, except that my hash  
> > function is (fn x => x) and I don't need a collision policy.
> Yes.  I also considered the same free list scheme that you are using.
> It has one deficiency with respect to scaling.  Namely, if the array
> becomes large at some point (lots of callbacks registered), it may not
> be possible to shrink it (after most of those callbacks are
> unregistered).  The free list scheme is fine if there is a (small)
> upper bound on the number of active simultaneous callbacks for each
> imported C function.  Otherwise it may consume arbitrary amounts of
> memory without doing useful work.

I devised a caching scheme that should be faster than a hash table and
does not suffer from the problem described above.  The idea is simple:
* Allocate mutable C objects that hold an integer index into an array.
  The malloc cost can be amortized and shouldn't be huge problem.
* When removing a cached element, move the last cached element to the
  position of the removed element.  This makes it possible to shrink the
You can find the implementation (has a couple of rough edges ATM) source
code from mltonlib:


-Vesa Karvonen

More information about the MLton mailing list