Search code examples
pythonperformancecachingallocationcpython

Why CPython pre-allocates some integer numbers?


From the CPython documentation here, is stated that:

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

Which makes this comparison true:

>>> a = -3
>>> b = -3
>>> a is b
True

I'm wondering what is the reason behind this, why are some numbers pre-allocated and why those numbers in particular?


Solution

  • Because the implementors of CPython have decided that that's a good range to have preallocated for perfomance reasons, as it covers the most commonly used integer values. There's nothing magical about the range [-5,256]. The few negative numbers are probably included in the range for common error codes and negative indexing of lists, and the upper limit was just set to a nice, round power of two.

    A comment from the CPython source code:

    /* Small integers are preallocated in this array so that they
       can be shared.
       The integers that are preallocated are those in the range
       -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
    */