Skip to content

Commit a02c1ae

Browse files
Use _PyObject_GET_BASIC_WEAKREFS_LISTPTR() in performance-sensitive locations.
1 parent 53c7901 commit a02c1ae

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Modules/gcmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,12 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
794794
if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
795795
continue;
796796

797-
/* It supports weakrefs. Does it have any? */
798-
wrlist = (PyWeakReference **)
799-
_PyObject_GET_WEAKREFS_LISTPTR(op);
797+
/* It supports weakrefs. Does it have any?
798+
*
799+
* This is never triggered for static types so we can avoid the
800+
* (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR().
801+
*/
802+
wrlist = (PyWeakReference **)_PyObject_GET_BASIC_WEAKREFS_LISTPTR(op);
800803

801804
/* `op` may have some weakrefs. March over the list, clear
802805
* all the weakrefs, and move the weakrefs with callbacks

Objects/typeobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,10 @@ subtype_dealloc(PyObject *self)
15071507
finalizers since they might rely on part of the object
15081508
being finalized that has already been destroyed. */
15091509
if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
1510-
/* Modeled after GET_WEAKREFS_LISTPTR() */
1510+
/* Modeled after GET_WEAKREFS_LISTPTR().
1511+
1512+
This is never triggered for static types so we can avoid the
1513+
(slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR(). */
15111514
PyWeakReference **list = (PyWeakReference **) \
15121515
_PyObject_GET_BASIC_WEAKREFS_LISTPTR(self);
15131516
while (*list)

0 commit comments

Comments
 (0)