Lazy mpz allocation

Marc Glisse marc.glisse at
Fri Apr 20 07:42:30 UTC 2018

On Fri, 20 Apr 2018, Marco Bodrato wrote:

> Ciao,
> Il Gio, 19 Aprile 2018 4:37 pm, Marc Glisse ha scritto:
>> I finally pushed it. It seemed unsafe to keep mpq unaware of lazy
>> allocation, in case people start swapping the numerator of a rational with
>> a lazy 0 integer or something like that.
> If we fear swaps, then ... what about swapping the deoniminator?

Only 0 can have lazy allocation, and I think we document that it isn't 
legal to put 0 on the denominator.

> Should we support a code like:
>  mpz_init (saved_denominator);
>  mpz_swap (saved_denominator, mpq_denref(rational));
>  /* Code overwriting rational*/
> ?

Hmmm... I don't know. It does seem vaguely possible that someone would do:

mpz_class den;
mpq_class q = some_computation();
den = std::move(q.get_den()); // won't need this value of q anymore
q = other_computation();

(the third line does a swap)

But that seems unlikely and contrived. If testing for this case is costly 
(it is likely cheap enough that I shouldn't worry), I'd be sad to pay for 
it :-( I'd be less negative if we were reaping some benefit from those new 
tests, for instance allowing a lazy denominator of 1.

But safety is a strong argument indeed.

> If yes, a larger patch is needed, I attach a proposal.

If yes, then something like that looks right, thanks.

Marc Glisse

More information about the gmp-devel mailing list