Fix GC tracking error for instances of mathutils types
Mathutils types were always GC tracked even when it wasn't intended.
Not having to track objects speeds up Python execution.
In an isolated benchmark created to stress test the GC
creating 4-million vectors (re-assigning them 100 times), this gives
an overall ~2.5x speedup, see: P3221.
Details:
Since [0] (which added support for sub-classed mathutils types)
tp_alloc was called which defaults to PyType_GenericAlloc which always
GC tracked the resulting object when Py_TPFLAGS_HAVE_GC was set.
Avoid using PyType_GenericAlloc unless the type is sub-classed,
in that case the object is un-tracked.
Add asserts that the tracked state is as expected before tracking &
un-tracking, to ensure changes to object creation don't cause objects
to be tracked unintentionally.
Also assign the PyTypeObject.tp_is_gc callback so types optionally GC
track objects only do so when an object is referenced.
[0]: fbd9364944
This commit is contained in:
@@ -17,9 +17,10 @@ extern char BaseMathObject_is_frozen_doc[];
|
||||
extern char BaseMathObject_is_valid_doc[];
|
||||
extern char BaseMathObject_owner_doc[];
|
||||
|
||||
PyObject *_BaseMathObject_new_impl(PyTypeObject *root_type, PyTypeObject *base_type);
|
||||
|
||||
#define BASE_MATH_NEW(struct_name, root_type, base_type) \
|
||||
((struct_name *)((base_type ? (base_type)->tp_alloc(base_type, 0) : \
|
||||
_PyObject_GC_New(&(root_type)))))
|
||||
((struct_name *)_BaseMathObject_new_impl(&root_type, base_type))
|
||||
|
||||
/** #BaseMathObject.flag */
|
||||
enum {
|
||||
@@ -76,6 +77,7 @@ PyObject *BaseMathObject_freeze(BaseMathObject *self);
|
||||
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg);
|
||||
int BaseMathObject_clear(BaseMathObject *self);
|
||||
void BaseMathObject_dealloc(BaseMathObject *self);
|
||||
int BaseMathObject_is_gc(BaseMathObject *self);
|
||||
|
||||
PyMODINIT_FUNC PyInit_mathutils(void);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user