@@ -152,6 +152,22 @@ long_normalize(PyLongObject *v)
152152# define MAX_LONG_DIGITS ((INT64_MAX-1) / PyLong_SHIFT)
153153#endif
154154
155+ static inline int
156+ maybe_freelist_push (PyObject * self )
157+ {
158+ assert (PyLong_CheckExact (self ));
159+ assert (Py_IS_TYPE (self , & PyLong_Type ));
160+
161+ PyLongObject * op = (PyLongObject * )self ;
162+ Py_ssize_t ndigits = _PyLong_DigitCount (op );
163+
164+ if (ndigits < PyLong_MAXSAVESIZE ) {
165+ return _Py_FREELIST_PUSH (ints [ndigits ], self , Py_ints_MAXFREELIST );
166+ }
167+ return 0 ;
168+ }
169+
170+
155171static PyLongObject *
156172long_alloc (Py_ssize_t size )
157173{
@@ -166,8 +182,8 @@ long_alloc(Py_ssize_t size)
166182 * assume that there is always at least one digit present. */
167183 Py_ssize_t ndigits = size ? size : 1 ;
168184
169- if (ndigits == 1 ) {
170- result = (PyLongObject * )_Py_FREELIST_POP (PyLongObject , ints );
185+ if (ndigits < PyLong_MAXSAVESIZE ) {
186+ result = (PyLongObject * )_Py_FREELIST_POP (PyLongObject , ints [ ndigits ] );
171187 }
172188 if (result == NULL ) {
173189 /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
@@ -247,7 +263,7 @@ _PyLong_FromMedium(sdigit x)
247263 assert (!IS_SMALL_INT (x ));
248264 assert (is_medium_int (x ));
249265
250- PyLongObject * v = (PyLongObject * )_Py_FREELIST_POP (PyLongObject , ints );
266+ PyLongObject * v = (PyLongObject * )_Py_FREELIST_POP (PyLongObject , ints [ 1 ] );
251267 if (v == NULL ) {
252268 v = PyObject_Malloc (sizeof (PyLongObject ));
253269 if (v == NULL ) {
@@ -3669,11 +3685,10 @@ _PyLong_ExactDealloc(PyObject *self)
36693685 _Py_SetImmortal (self );
36703686 return ;
36713687 }
3672- if ( _PyLong_IsCompact (( PyLongObject * ) self )) {
3673- _Py_FREELIST_FREE ( ints , self , PyObject_Free );
3674- return ;
3688+
3689+ if (! maybe_freelist_push ( self )) {
3690+ PyObject_Free ( self ) ;
36753691 }
3676- PyObject_Free (self );
36773692}
36783693
36793694static void
@@ -3689,10 +3704,13 @@ long_dealloc(PyObject *self)
36893704 _Py_SetImmortal (self );
36903705 return ;
36913706 }
3692- if (PyLong_CheckExact (self ) && _PyLong_IsCompact ((PyLongObject * )self )) {
3693- _Py_FREELIST_FREE (ints , self , PyObject_Free );
3707+ if (PyLong_CheckExact (self )) {
3708+ if (!maybe_freelist_push (self )) {
3709+ PyObject_Free (self );
3710+ }
36943711 return ;
36953712 }
3713+
36963714 Py_TYPE (self )-> tp_free (self );
36973715}
36983716
0 commit comments