mpz_t caching

Marc Glisse marc.glisse at
Tue Sep 8 11:32:59 UTC 2015

On Fri, 4 Sep 2015, Vincent Lefevre wrote:

> In 2014, Patrick Pelissier (in Cc) implemented a mpz_t allocation
> cache for MPFR, redefining mpz_init and mpz_clear, in order to
> avoid some deallocations/allocations (via the indirect call to
> the allocation functions) when mpz_t's cleared and init'ed again
> a bit after. I've attached the patch that was applied to MPFR.

One similar thing I have used in the past is a wrapper (like a shared 
pointer) to mpq_t where the destructor, instead of destructing the mpq_t, 
was putting it on a free list where constructors could pick it up. That 
is, I was recycling the whole object and not just its allocated limbs. 
This was efficient because I was never using many mpq_t at the same time, 
their size was never huge, and reallocs rapidly converged to a sufficient 
size. And it was convenient because I did not have to provide an upper 
bound on either the number or the size of the variables. It could also be 
done per-thread because those numbers had no way to migrate from one 
thread to another. But that's quite a specific use-case.

Marc Glisse

More information about the gmp-devel mailing list