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:
2022-09-28 16:09:12 +10:00
parent ada2b9f6e4
commit 5270ac5ed8
13 changed files with 93 additions and 8 deletions

View File

@@ -153,6 +153,7 @@ static BPyGPUBuffer *pygpu_buffer_make_from_data(PyObject *parent,
if (parent) {
Py_INCREF(parent);
buffer->parent = parent;
BLI_assert(!PyObject_GC_IsTracked((PyObject *)buffer));
PyObject_GC_Track(buffer);
}
return buffer;
@@ -422,6 +423,11 @@ static PyObject *pygpu_buffer__tp_new(PyTypeObject *UNUSED(type), PyObject *args
return (PyObject *)buffer;
}
static int pygpu_buffer__tp_is_gc(BPyGPUBuffer *self)
{
return self->parent != NULL;
}
/* BPyGPUBuffer sequence methods */
static int pygpu_buffer__sq_length(BPyGPUBuffer *self)
@@ -677,6 +683,7 @@ PyTypeObject BPyGPU_BufferType = {
.tp_methods = pygpu_buffer__tp_methods,
.tp_getset = pygpu_buffer_getseters,
.tp_new = pygpu_buffer__tp_new,
.tp_is_gc = (inquiry)pygpu_buffer__tp_is_gc,
};
static size_t pygpu_buffer_calc_size(const int format,