Skip to content

Commit ce841a9

Browse files
authored
gh-160147: Inline PyObject_IS_GC() in _PyObject_GC_MAY_BE_TRACKED() (#161189)
Move _PyType_IS_GC() and _PyObject_IS_GC() from pycore_object.h to pycore_gc.h. Replace PyObject_IS_GC() call with _PyObject_IS_GC() in _PyObject_GC_MAY_BE_TRACKED(). Help the compiler to inline _PyObject_IS_GC() code in _PyObject_GC_MAY_BE_TRACKED(), especially when LTO is not used.
1 parent 431fdec commit ce841a9

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

‎Include/internal/pycore_gc.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,26 @@ static inline int _PyObject_GC_IS_TRACKED(PyObject *op) {
8080
}
8181
#define _PyObject_GC_IS_TRACKED(op) _PyObject_GC_IS_TRACKED(_Py_CAST(PyObject*, op))
8282

83+
// Fast inlined version of PyType_IS_GC()
84+
static inline int
85+
_PyType_IS_GC(PyTypeObject *type)
86+
{
87+
return (type->tp_flags & Py_TPFLAGS_HAVE_GC) != 0;
88+
}
89+
90+
// Fast inlined version of PyObject_IS_GC()
91+
static inline int
92+
_PyObject_IS_GC(PyObject *obj)
93+
{
94+
PyTypeObject *type = Py_TYPE(obj);
95+
return (_PyType_IS_GC(type)
96+
&& (type->tp_is_gc == NULL || type->tp_is_gc(obj)));
97+
}
98+
8399
/* True if the object may be tracked by the GC in the future, or already is.
84100
This can be useful to implement some optimizations. */
85101
static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {
86-
if (!PyObject_IS_GC(obj)) {
102+
if (!_PyObject_IS_GC(obj)) {
87103
return 0;
88104
}
89105
if (PyTuple_CheckExact(obj)) {

‎Include/internal/pycore_object.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -816,18 +816,6 @@ _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(PyObject *op)
816816
return (PyWeakReference **)((char *)op + offset);
817817
}
818818

819-
// Fast inlined version of PyType_IS_GC()
820-
#define _PyType_IS_GC(t) _PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
821-
822-
// Fast inlined version of PyObject_IS_GC()
823-
static inline int
824-
_PyObject_IS_GC(PyObject *obj)
825-
{
826-
PyTypeObject *type = Py_TYPE(obj);
827-
return (_PyType_IS_GC(type)
828-
&& (type->tp_is_gc == NULL || type->tp_is_gc(obj)));
829-
}
830-
831819
// Fast inlined version of PyObject_Hash(). Dictionaries are very
832820
// likely to include string keys (class and instance attributes,
833821
// json, ...) so we include a fast path for strings.

0 commit comments

Comments
 (0)