@@ -37,6 +37,9 @@ tuple_alloc(Py_ssize_t size)
37
37
PyErr_BadInternalCall ();
38
38
return NULL ;
39
39
}
40
+ #ifdef Py_DEBUG
41
+ assert (size != 0 ); // The empty tuple is statically allocated.
42
+ #endif
40
43
41
44
PyTupleObject * op = maybe_freelist_pop (size );
42
45
if (op == NULL ) {
@@ -52,24 +55,17 @@ tuple_alloc(Py_ssize_t size)
52
55
return op ;
53
56
}
54
57
55
- static inline PyTupleObject * maybe_freelist_get_empty_singleton (void );
58
+ // The empty tuple singleton is not tracked by the GC.
59
+ // It does not contain any Python object.
60
+ // Note that tuple subclasses have their own empty instances.
56
61
57
62
static inline PyObject *
58
63
tuple_get_empty (void )
59
64
{
60
- PyTupleObject * op = maybe_freelist_get_empty_singleton ();
61
- if (op != NULL ) {
62
- Py_INCREF (op );
63
- }
64
- else {
65
- op = PyObject_GC_NewVar (PyTupleObject , & PyTuple_Type , 0 );
66
- _PyObject_GC_TRACK (op );
67
- }
68
- return (PyObject * )op ;
65
+ Py_INCREF (& _Py_SINGLETON (tuple_empty ));
66
+ return (PyObject * )& _Py_SINGLETON (tuple_empty );
69
67
}
70
68
71
- // Note that tuple subclasses have their own empty instances.
72
-
73
69
PyObject *
74
70
PyTuple_New (Py_ssize_t size )
75
71
{
@@ -190,6 +186,21 @@ PyTuple_Pack(Py_ssize_t n, ...)
190
186
static void
191
187
tupledealloc (PyTupleObject * op )
192
188
{
189
+ if (Py_SIZE (op ) == 0 ) {
190
+ /* The empty tuple is statically allocated. */
191
+ if (op == & _Py_SINGLETON (tuple_empty )) {
192
+ #ifdef Py_DEBUG
193
+ _Py_FatalRefcountError ("deallocating the empty tuple singleton" );
194
+ #else
195
+ return ;
196
+ #endif
197
+ }
198
+ #ifdef Py_DEBUG
199
+ /* tuple subclasses have their own empty instances. */
200
+ assert (!PyTuple_CheckExact (op ));
201
+ #endif
202
+ }
203
+
193
204
PyObject_GC_UnTrack (op );
194
205
Py_TRASHCAN_BEGIN (op , tupledealloc )
195
206
@@ -928,6 +939,9 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
928
939
return 0 ;
929
940
}
930
941
if (oldsize == 0 ) {
942
+ #ifdef Py_DEBUG
943
+ assert (v == & _Py_SINGLETON (tuple_empty ));
944
+ #endif
931
945
/* The empty tuple is statically allocated so we never
932
946
resize it in-place. */
933
947
Py_DECREF (v );
@@ -984,17 +998,6 @@ _PyTuple_InitTypes(PyInterpreterState *interp)
984
998
return _PyStatus_OK ();
985
999
}
986
1000
987
- static int maybe_freelist_init_empty_tuple (PyInterpreterState * );
988
-
989
- PyStatus
990
- _PyTuple_InitGlobalObjects (PyInterpreterState * interp )
991
- {
992
- if (maybe_freelist_init_empty_tuple (interp ) < 0 ) {
993
- return _PyStatus_NO_MEMORY ();
994
- }
995
- return _PyStatus_OK ();
996
- }
997
-
998
1001
static void maybe_freelist_clear (PyInterpreterState * , int );
999
1002
1000
1003
void
@@ -1163,44 +1166,6 @@ tuple_iter(PyObject *seq)
1163
1166
#define STATE (interp->tuple)
1164
1167
#define FREELIST_FINALIZED (STATE.numfree[0] < 0)
1165
1168
1166
- static int
1167
- maybe_freelist_init_empty_tuple (PyInterpreterState * interp )
1168
- {
1169
- #if PyTuple_NFREELISTS > 0
1170
- assert (STATE .free_list [0 ] == NULL );
1171
-
1172
- PyTupleObject * op = PyObject_GC_NewVar (PyTupleObject , & PyTuple_Type , 0 );
1173
- if (op == NULL ) {
1174
- return -1 ;
1175
- }
1176
- // The empty tuple singleton is not tracked by the GC.
1177
- // It does not contain any Python object.
1178
-
1179
- STATE .free_list [0 ] = op ;
1180
- assert (STATE .numfree [0 ] == 0 );
1181
- STATE .numfree [0 ] = 1 ;
1182
- #endif
1183
- return 0 ;
1184
- }
1185
-
1186
- static inline PyTupleObject *
1187
- maybe_freelist_get_empty_singleton (void )
1188
- {
1189
- #if PyTuple_NFREELISTS > 0
1190
- PyTupleObject * op = STATE .free_list [0 ];
1191
- // maybe_freelist_get_empty_singleton() must not be called
1192
- // before maybe_freelist_init_empty_tuple()
1193
- // or after maybe_freelist_clear(fini=1).
1194
- assert (op != NULL );
1195
- #ifdef Py_DEBUG
1196
- assert (STATE .numfree [0 ] == 1 );
1197
- #endif
1198
- return (PyObject * ) op ;
1199
- #else
1200
- return NULL ;
1201
- #endif
1202
- }
1203
-
1204
1169
static inline PyTupleObject *
1205
1170
maybe_freelist_pop (Py_ssize_t size )
1206
1171
{
@@ -1210,9 +1175,12 @@ maybe_freelist_pop(Py_ssize_t size)
1210
1175
/* maybe_freelist_pop() must not be called after maybe_freelist_fini(). */
1211
1176
assert (!FREELIST_FINALIZED );
1212
1177
#endif
1178
+ if (size == 0 ) {
1179
+ return NULL ;
1180
+ }
1213
1181
assert (size > 0 );
1214
1182
if (size < PyTuple_MAXSAVESIZE ) {
1215
- Py_ssize_t index = size ;
1183
+ Py_ssize_t index = size - 1 ;
1216
1184
PyTupleObject * op = STATE .free_list [index ];
1217
1185
if (op != NULL ) {
1218
1186
/* op is the head of a linked list, with the first item
@@ -1245,16 +1213,9 @@ maybe_freelist_push(PyTupleObject *op)
1245
1213
assert (!FREELIST_FINALIZED );
1246
1214
#endif
1247
1215
if (Py_SIZE (op ) == 0 ) {
1248
- #ifdef Py_DEBUG
1249
- // The empty tuple singleton must only be deallocated by
1250
- // maybe_freelist_fini(): not before, not after.
1251
- if (op == STATE .free_list [0 ] && STATE .numfree [0 ] < 0 ) {
1252
- _Py_FatalRefcountError ("deallocating the empty tuple singleton" );
1253
- }
1254
- #endif
1255
- return 1 ;
1216
+ return 0 ;
1256
1217
}
1257
- Py_ssize_t index = Py_SIZE (op );
1218
+ Py_ssize_t index = Py_SIZE (op ) - 1 ;
1258
1219
if (index < PyTuple_NFREELISTS
1259
1220
&& STATE .numfree [index ] < PyTuple_MAXFREELIST
1260
1221
&& Py_IS_TYPE (op , & PyTuple_Type ))
@@ -1274,20 +1235,7 @@ static void
1274
1235
maybe_freelist_clear (PyInterpreterState * interp , int fini )
1275
1236
{
1276
1237
#if PyTuple_NFREELISTS > 0
1277
- // The empty tuple singleton is only cleared during finalization.
1278
- if (fini ) {
1279
- assert (!_PyObject_GC_IS_TRACKED (STATE .free_list [0 ]));
1280
- // XXX Is this right?
1281
- assert (STATE .free_list [0 ].ob_item [0 ] == NULL );
1282
- #ifdef Py_DEBUG
1283
- STATE .numfree [0 ] = 0 ;
1284
- #endif
1285
- Py_CLEAR (STATE .free_list [0 ]);
1286
- #ifdef Py_DEBUG
1287
- STATE .numfree [0 ] = -1 ;
1288
- #endif
1289
- }
1290
- for (Py_ssize_t i = 1 ; i < PyTuple_NFREELISTS ; i ++ ) {
1238
+ for (Py_ssize_t i = 0 ; i < PyTuple_NFREELISTS ; i ++ ) {
1291
1239
PyTupleObject * p = STATE .free_list [i ];
1292
1240
STATE .free_list [i ] = NULL ;
1293
1241
STATE .numfree [i ] = fini ? -1 : 0 ;
@@ -1307,7 +1255,7 @@ _PyTuple_DebugMallocStats(FILE *out)
1307
1255
#if PyTuple_NFREELISTS > 0
1308
1256
PyInterpreterState * interp = _PyInterpreterState_GET ();
1309
1257
for (int i = 0 ; i < PyTuple_NFREELISTS ; i ++ ) {
1310
- int len = i ;
1258
+ int len = i + 1 ;
1311
1259
char buf [128 ];
1312
1260
PyOS_snprintf (buf , sizeof (buf ),
1313
1261
"free %d-sized PyTupleObject" , len );
0 commit comments