GMP provides methods for initializing and assigning an mpz_t.
A call to mpz_init_set(a, b) will assign to a the content of b. However, I assume, this performs a deep copy on b.
On my project I need to work with arrays of mpz_t that are as long as 5,000,000 (we are talking about 640MB of memory) and the framework I'm using performs various assignment operations on such types (I didn't develop the framework and rewriting it in not an option). Recently, I realized that after most of the assignments the value of b is cleared so it seems unnatural to deep copy a value that can already be used like it is. However, the interface of the framework doesn't allow to do that (uses wrappers around mpz_t's) and it would take a lot of effort to change that (I can still change some basic things).
I have already tried a solution based on a pointer to mpz_class but surprisingly that doesn't give a performance boost at all. In fact it slows down the execution (didn't test on huge arrays though).
My question is: Can I shallow copy an mpz_t? Example given below
class somewrapper
{
mpz_t v;
somewrapper(mpz_t x) //constructor: probably performing deep copy here as well
{
// the following line performs a deep copy(?) on x
// but x is not used. why not shallow copy it?
mpz_init_set(v, x);
}
somefunction() { }
}