[Python-ideas] Expansion of the range of small integers

Steven D'Aprano steve at pearwood.info
Mon Sep 17 15:09:58 CEST 2012


On 17/09/12 22:41, Serhiy Storchaka wrote:

> Now in the CPython small integer numbers from -5 up to 256 inclusive
>are preallocated at the start. It allows to reduce memory consumption
>and time of creation of the integers in this range. In particular this
>affects the speed of short enumerations. Increasing the range to the
>maximum (from -32767 to 32767 inclusive), we can speed up longer
>enumerations.

[...]

> Shortcomings:
>
> 1) Memory consumption increases by constant 1-1.5 MB. Or half of it if
>the range is expanded only in a positive direction. This is not a
> problem on most modern computers. But would be better if the parameters
>NSMALLPOSINTS and NSMALLNEGINTS have been configurable at build time.
>
> 2) A little bit larger Python start time. I was not able to measure the
>difference, it is too small.

There is an additional consequence of this proposed change. I'm not sure
if this counts as an argument for, or against, the change, but beginners
and even some experienced programmers often find the difference between
identity and equality hard to deal with. Caching of small integers already
blurs the distinction:

py> a = 42
py> b = 42
py> a is b
True

Extending that behaviour up to 32767 will further blur the distinction.



-- 
Steven



More information about the Python-ideas mailing list