8000 bpo-46753: Add the empty tuple to the _PyRuntimeState.global_objects.… · python/cpython@08deed1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08deed1

Browse files
bpo-46753: Add the empty tuple to the _PyRuntimeState.global_objects. (gh-31345)
https://bugs.python.org/issue46753
1 parent d5b7bba commit 08deed1

File tree

8 files changed

+213
-199
lines changed

8 files changed

+213
-199
lines changed

Include/internal/pycore_gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct {
2020
} PyGC_Head;
2121

2222
#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
23+
#define _PyGC_Head_UNUSED PyGC_Head
2324

2425
/* True if the object is currently tracked by the GC. */
2526
#define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)

Include/internal/pycore_global_objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_gc.h" // PyGC_Head
1112
#include "pycore_global_strings.h" // struct _Py_global_strings
1213

1314

@@ -40,6 +41,9 @@ struct _Py_global_objects {
4041
} bytes_characters[256];
4142

4243
struct _Py_global_strings strings;
44+
45+
_PyGC_Head_UNUSED _tuple_empty_gc_not_used;
46+
PyTupleObject tuple_empty;
4347
} singletons;
4448
};
4549

Include/internal/pycore_runtime_init.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,10 @@ extern "C" {
964964
INIT_ID(zipimporter), \
965965
}, \
966966
}, \
967+
\
968+
.tuple_empty = { \
969+
.ob_base = _PyVarObject_IMMORTAL_INIT(&PyTuple_Type, 0) \
970+
}, \
967971
}, \
968972
}
969973
/* End auto-generated code */

Include/internal/pycore_tuple.h

Lines changed: 35 additions & 20 deletions
6AAF
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,45 @@ extern void _PyTuple_Fini(PyInterpreterState *);
2020

2121
/* other API */
2222

23-
#ifndef WITH_FREELISTS
24-
// without freelists
25-
// for tuples only store empty tuple singleton
26-
# define PyTuple_MAXSAVESIZE 1
27-
# define PyTuple_MAXFREELIST 1
28-
#endif
23+
// PyTuple_MAXSAVESIZE - largest tuple to save on free list
24+
// PyTuple_MAXFREELIST - maximum number of tuples of each size to save
2925

30-
/* Speed optimization to avoid frequent malloc/free of small tuples */
31-
#ifndef PyTuple_MAXSAVESIZE
32-
// Largest tuple to save on free list
33-
# define PyTuple_MAXSAVESIZE 20
34-
#endif
35-
#ifndef PyTuple_MAXFREELIST
36-
// Maximum number of tuples of each size to save
37-
# define PyTuple_MAXFREELIST 2000
26+
#if defined(PyTuple_MAXSAVESIZE) && PyTuple_MAXSAVESIZE <= 0
27+
// A build indicated that tuple freelists should not be used.
28+
# define PyTuple_NFREELISTS 0
29+
# undef PyTuple_MAXSAVESIZE
30+
# undef PyTuple_MAXFREELIST
31+
32+
#elif !defined(WITH_FREELISTS)
33+
# define PyTuple_NFREELISTS 0
34+
# undef PyTuple_MAXSAVESIZE
35+
# undef PyTuple_MAXFREELIST
36+
37+
#else
38+
// We are using a freelist for tuples.
39+
# ifndef PyTuple_MAXSAVESIZE
40+
# define PyTuple_MAXSAVESIZE 20
41+
# endif
42+
# define PyTuple_NFREELISTS PyTuple_MAXSAVESIZE
43+
# ifndef PyTuple_MAXFREELIST
44+
# define PyTuple_MAXFREELIST 2000
45+
# endif
3846
#endif
3947

4048
struct _Py_tuple_state {
41-
#if PyTuple_MAXSAVESIZE > 0
42-
/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists,
43-
entry 0 is the empty tuple () of which at most one instance
44-
will be allocated. */
45-
PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
46-
int numfree[PyTuple_MAXSAVESIZE];
49+
#if PyTuple_NFREELISTS > 0
50+
/* There is one freelist for each size from 1 to PyTuple_MAXSAVESIZE.
51+
The empty tuple is handled separately.
52+
53+
Each tuple stored in the array is the head of the linked list
54+
(and the next available tuple) for that size. The actual tuple
55+
object is used as the linked list node, with its first item
56+
(ob_item[0]) pointing to the next node (i.e. the previous head).
57+
Each linked list is initially NULL. */
58+
PyTupleObject *free_list[PyTuple_NFREELISTS];
59+
int numfree[PyTuple_NFREELISTS];
60+
#else
61+
char _unused; // Empty structs are not allowed.
4762
#endif
4863
};
4964

0 commit comments

Comments
 (0)
0