Replacement for GMP

Simon Peyton-Jones simonpj at
Wed Aug 2 08:05:51 EDT 2006

Esa, Peter

Thank you for diving into the GMP stuff.  Simon and I discussed it a
bit; here's a summary:

(1) We'd be delighted to use a BSD-licensed alternative to GMP in GHC.
It's been a long-standing issue, just never quite important enough to
get done.  If either or both of you are willing to put in the legwork,
and emerge with an implementation that we understand and can maintain,
we'd be happy to use it.  We'll certainly help in any way we can.

(2) We're concerned about performance.  Replacing GMP, but losing
substantial performance on bignum-intensive programs would be

(3) It's unlikely (albeit not impossible) that we'll get GMP-level
performance out of a Haskell-only bignum library.  Nevertheless,
providing such a library would be a particularly easy way to provide a
drop-in alternative to GMP.   It's just that it probably wouldn't let us
*replace* GMP altogether, for performance reasons, so we'd still need
the GMP route; from a maintenance point of view, this would be a step

(4) The tricky spot for any library is memory allocation.  Our GMP-based
implementation works by getting GMP to use GHC's allocator to allocate
memory.  This means that every bignum is allocated in the Haskell heap,
is automatically managed by GHC's garbage collector, which is Very Good.

But because the allocator is statically linked to GMP, you can only have
one allocator, and that leads to difficulties if you have another bit of
the same binary that also wants to use GMP.  (Of course, we could *copy*
GMP, changing all the function names.  That would eliminate the

I suppose that one alternative is to let the library use 'malloc', but
make a foreign-pointer proxy for every bignum, which calls 'free' when
the GHC garbage collector frees it.  Not as efficient, though.

Anyway, this problem will arise for *any* bignum library, and is one of
the first things to figure out.  

(5) If you do go ahead, could you pls start a Wiki page on the GHC
development Wiki (, where you
document your thoughts, the evolving design etc?  You might want to
extract the core of this email thread to initialise the Wiki page.

Thanks for your help!


More information about the Glasgow-haskell-users mailing list