speed of mpz_odd_p and lazy allocation

Marc Glisse marc.glisse at inria.fr
Mon Aug 20 22:29:05 CEST 2012

On Mon, 20 Aug 2012, Gabriel Dos Reis wrote:

> On Mon, Aug 20, 2012 at 2:43 PM, Marc Glisse <marc.glisse at inria.fr> wrote:
>> On Mon, 20 Aug 2012, Gabriel Dos Reis wrote:
>>> What would be the basis for a vendor to assume that
>>> the elements from the old vector should be assigned
>>> as opposed to being copy constructed into the new vector?
>> Are you asking if it is legal to implement reserve() this way (after
>> detecting the right constructors/operators)?
> No.  Being "legal" does not necessarily mean that it will be done.
> For example, it is legal of reserve() to make as many useless
> computations as it wants (including useless copies);  that does not
> mean that it will be implemented that way.
>>  Or how we can guess if this
>> "optimization" is really an improvement?

Ok, I'll take that as option n°2: it may be worse than the usual 
implementation. In the case where the type is not copyable, the move 
constructor may throw but the move assignment is nothrow, this technique 
gives you exception-safety (not required by the standard in this specific 
case, but always nice). In any case, this implementation can only make 
sense when the move constructor may throw, in which case it is not absurd 
to guess that default-construction + move-assignment is likely no more 
expensive than copy-construction. I agree there are counter-examples, but 
I believe it would be a win on average.

Marc Glisse

More information about the gmp-devel mailing list