Why do we put GMP allocations on GHC heaps?

Simon Marlow marlowsd at gmail.com
Mon Nov 4 10:18:43 UTC 2013


On 31/10/2013 07:49, Edward Kmett wrote:
> That actually would be a pretty reasonable way forward, that could
> maintain the performance of the current approach without crippling GMP
> for other consumers.
>
> It'd probably be a fair bit of work, as you wind up up to your eyeballs
> in the guts of the library, limbs everywhere, colorful body part
> metaphors flying every which way.
>
> It doesn't make accessing libraries like MPFR from Haskell in the most
> high performance way all that much easier, but it does mean that if you
> linked to a library that used them you wouldn't just start randomly
> segfaulting like you do today.
>
> If someone else doesn't get around to it first, I'll probably give it a
> shot eventually.

We did originally look at targetting the mpn API, but decided against it 
for the reasons alluded to above - the mpz layer has quite a lot of 
stuff in it that you would need to replicate.  For instance, when you 
start doing an operation some calculation has to be done to figure out 
how much memory to allocate for the result.  And be extra careful, 
because the GMP code is GPL, so our replacement for the mpz bits would 
probably end up being GPL too.

Historical tidbit: hbc targets the mpn layer, IIRC.

Cheers,
Simon


>
> -Edward
>
>
> On Thu, Oct 31, 2013 at 3:39 AM, Florian Weimer <fw at deneb.enyo.de
> <mailto:fw at deneb.enyo.de>> wrote:
>
>     * Edward Z. Yang:
>
>      > Actually, this is precisely the problem.  When is a GMP integer
>     freed?
>      > It can have pointers to it from objects on the heap, so this free
>     should
>      > only occur when the integer is dead, with no references from the
>     heap.
>      > How can that be arranged?  Well, the garbage collector is responsible
>      > for figuring this out.  So why shouldn't they just live on the
>     heap, and
>      > then smoothly integrate with the existing garbage collector.
>
>     The real problem here is that GHC uses the mpz_* functions and not the
>     lower-level mpn_* functions, where the caller is responsible for
>     memory management and which would play well with garbage collection
>     (or any other memory management scheme).
>     _______________________________________________
>     ghc-devs mailing list
>     ghc-devs at haskell.org <mailto:ghc-devs at haskell.org>
>     http://www.haskell.org/mailman/listinfo/ghc-devs
>
>
>
>
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://www.haskell.org/mailman/listinfo/ghc-devs
>


More information about the ghc-devs mailing list