Skip to content

Commit

Permalink
hv_cli_prod.c: Fix producer profile for Python 3.11
Browse files Browse the repository at this point in the history
Python added _PyType_PreHeaderSize for the preheader and it's
no longer just sizeof(PyGC_Head)

See python/cpython#101430

For #41
  • Loading branch information
zhuyifei1999 committed May 13, 2023
1 parent e0cd351 commit e80cafb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/heapy/hv_cli_prod.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ PyDoc_STRVAR(hv_cli_prod_doc,
" memoize the classification sets.\n"
);

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
# define Py_BUILD_CORE
# undef _PyObject_LookupSpecial
/* _PyType_PreHeaderSize */
# include <internal/pycore_object.h>
# undef Py_BUILD_CORE
#endif

// The sizeof of PyGC_Head is not to be trusted upon even across Python minor
// releases. Eg: python/cpython@8766cb7
static Py_ssize_t sizeof_PyGC_Head;
Expand Down Expand Up @@ -100,6 +108,15 @@ hv_cli_prod_classify(ProdObject *self, PyObject *obj)
ptr = (Py_uintptr_t)obj;
}

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
// /~https://github.com/python/cpython/issues/101430
ptr -= _PyType_PreHeaderSize(Py_TYPE(obj));
// _PyType_PreHeaderSize would add an extra compile-time sizeof(PyGC_Head),
// which may be different from the true value in sizeof_PyGC_Head
if (PyType_IS_GC(Py_TYPE(obj)))
ptr += sizeof(PyGC_Head);
#endif

tb = _PyTraceMalloc_GetTraceback(0, (Py_uintptr_t)ptr);

if (!tb)
Expand Down

0 comments on commit e80cafb

Please sign in to comment.