@@ -156,7 +156,7 @@ PyLongObject *
156
156
_PyLong_New (Py_ssize_t size )
157
157
{
158
158
assert (size >= 0 );
159
- PyLongObject * result ;
159
+ PyLongObject * result = NULL ;
160
160
if (size > (Py_ssize_t )MAX_LONG_DIGITS ) {
161
161
PyErr_SetString (PyExc_OverflowError ,
162
162
"too many digits in integer" );
@@ -165,19 +165,25 @@ _PyLong_New(Py_ssize_t size)
165
165
/* Fast operations for single digit integers (including zero)
166
166
* assume that there is always at least one digit present. */
167
167
Py_ssize_t ndigits = size ? size : 1 ;
168
- /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
169
- sizeof(digit)*size. Previous incarnations of this code used
170
- sizeof() instead of the offsetof, but this risks being
171
- incorrect in the presence of padding between the header
172
- and the digits. */
173
- result = PyObject_Malloc (offsetof(PyLongObject , long_value .ob_digit ) +
174
- ndigits * sizeof (digit ));
175
- if (!result ) {
176
- PyErr_NoMemory ();
177
- return NULL ;
168
+
169
+ if (ndigits == 1 ) {
170
+ result = (PyLongObject * )_Py_FREELIST_POP (PyLongObject , ints );
171
+ }
172
+ if (result == NULL ) {
173
+ /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
174
+ sizeof(digit)*size. Previous incarnations of this code used
175
+ sizeof() instead of the offsetof, but this risks being
176
+ incorrect in the presence of padding between the header
177
+ and the digits. */
178
+ result = PyObject_Malloc (offsetof(PyLongObject , long_value .ob_digit ) +
179
+ ndigits * sizeof (digit ));
180
+ if (!result ) {
181
+ PyErr_NoMemory ();
182
+ return NULL ;
183
+ }
184
+ _PyObject_Init ((PyObject * )result , & PyLong_Type );
178
185
}
179
186
_PyLong_SetSignAndDigitCount (result , size != 0 , size );
180
- _PyObject_Init ((PyObject * )result , & PyLong_Type );
181
187
/* The digit has to be initialized explicitly to avoid
182
188
* use-of-uninitialized-value. */
183
189
result -> long_value .ob_digit [0 ] = 0 ;
0 commit comments