[Python-Dev] [Python-checkins] cpython: Issue #14716: Change integer overflow check in unicode_writer_prepare()

Antoine Pitrou solipsis at pitrou.net
Mon May 7 18:38:58 CEST 2012


On Mon, 7 May 2012 12:35:27 +0100
Mark Dickinson <dickinsm at gmail.com> wrote:
> 
> Hmm.  Very clever, but it's not obvious that that overflow check is
> mathematically sound.  As it turns out, the maths works provided that
> PY_SSIZE_T_MAX isn't congruent to 4 modulo 5;  since PY_SSIZE_T_MAX
> will almost always be one less than a power of 2 and powers of 2 are
> always congruent to 1, 2 or 4 modulo 5, we're safe.
> 
> Is the gain from this kind of micro-optimization really worth the cost
> of replacing obviously correct code with code whose correctness needs
> several minutes of thought?

Agreed that the original code is good enough. Dividing by 4 is fast,
and this particular line of code is followed by a memory reallocation.

In general, "clever" micro-optimizations that don't produce significant
performance improvements should be avoided, IMHO :-)

Regards

Antoine.




More information about the Python-Dev mailing list