sameness/identity

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Oct 1 12:51:40 EDT 2001


Oleg Broytmann <phd at phd.pp.ru> writes:

>    This is really hardcoded limit in the current CPython implementation :)
> The interpreter preallocates numbers from 0 to 99.

It's not really preallocation, rather lazy allocation and caching; see
intobject.c:PyInt_FromLong:

static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];

...

	if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
	    (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
		Py_INCREF(v);
		return (PyObject *) v;
	}
...
	PyObject_INIT(v, &PyInt_Type);
	v->ob_ival = ival;
	if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
		/* save this one for a following allocation */
		Py_INCREF(v);
		small_ints[ival + NSMALLNEGINTS] = v;
	}
	return (PyObject *) v;

The constants are 1 and 100, respectively.

Regards,
Martin



More information about the Python-list mailing list