Skip to content

Commit

Permalink
Simplify weakref/proxy creation
Browse files Browse the repository at this point in the history
As of 3.12, Allocation no longer triggers GC. This allows us to
stop having to recompute the "canonical" callback-less ref/proxy
post allocation.
  • Loading branch information
mpage committed Mar 14, 2024
1 parent b54d7c8 commit 4944b5f
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,24 +801,14 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
if (result != NULL)
Py_INCREF(result);
else {
/* Note: new_weakref() can trigger cyclic GC, so the weakref
list on ob can be mutated. This means that the ref and
proxy pointers we got back earlier may have been collected,
so we need to compute these values again before we use
them. */
/* We do not need to recompute ref/proxy here; new_weakref() cannot
trigger cyclic GC.
*/
result = new_weakref(ob, callback);
if (result != NULL) {
get_basic_refs(*list, &ref, &proxy);
if (callback == NULL) {
if (ref == NULL)
insert_head(result, list);
else {
/* Someone else added a ref without a callback
during GC. Return that one instead of this one
to avoid violating the invariants of the list
of weakrefs for ob. */
Py_SETREF(result, (PyWeakReference*)Py_NewRef(ref));
}
}
else {
PyWeakReference *prev;
Expand Down Expand Up @@ -858,11 +848,9 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
if (result != NULL)
Py_INCREF(result);
else {
/* Note: new_weakref() can trigger cyclic GC, so the weakref
list on ob can be mutated. This means that the ref and
proxy pointers we got back earlier may have been collected,
so we need to compute these values again before we use
them. */
/* We do not need to recompute ref/proxy here; new_weakref() cannot
trigger cyclic GC.
*/
result = new_weakref(ob, callback);
if (result != NULL) {
PyWeakReference *prev;
Expand All @@ -873,16 +861,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
else {
Py_SET_TYPE(result, &_PyWeakref_ProxyType);
}
get_basic_refs(*list, &ref, &proxy);
if (callback == NULL) {
if (proxy != NULL) {
/* Someone else added a proxy without a callback
during GC. Return that one instead of this one
to avoid violating the invariants of the list
of weakrefs for ob. */
Py_SETREF(result, (PyWeakReference*)Py_NewRef(proxy));
goto skip_insert;
}
prev = ref;
}
else
Expand All @@ -892,8 +871,6 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
insert_head(result, list);
else
insert_after(result, prev);
skip_insert:
;
}
}
return (PyObject *) result;
Expand Down

0 comments on commit 4944b5f

Please sign in to comment.