aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/gc.c
diff options
context:
space:
mode:
authormpage <mpage@meta.com>2025-04-01 10:18:42 -0700
committerGitHub <noreply@github.com>2025-04-01 10:18:42 -0700
commit053c285f6b41f92fbdd1d4ff0c959cceefacd7cd (patch)
treec30af215989dca6fdd8f8ebb74396f29abdca308 /Python/gc.c
parente9556e100452ed5a92fcf0e333ab75b0da29cf5b (diff)
downloadcpython-053c285f6b41f92fbdd1d4ff0c959cceefacd7cd.tar.gz
cpython-053c285f6b41f92fbdd1d4ff0c959cceefacd7cd.zip
gh-130704: Strength reduce `LOAD_FAST{_LOAD_FAST}` (#130708)
Optimize `LOAD_FAST` opcodes into faster versions that load borrowed references onto the operand stack when we can prove that the lifetime of the local outlives the lifetime of the temporary that is loaded onto the stack.
Diffstat (limited to 'Python/gc.c')
-rw-r--r--Python/gc.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/Python/gc.c b/Python/gc.c
index e37d4b76456..dad088e09f8 100644
--- a/Python/gc.c
+++ b/Python/gc.c
@@ -544,6 +544,12 @@ visit_decref(PyObject *op, void *parent)
int
_PyGC_VisitStackRef(_PyStackRef *ref, visitproc visit, void *arg)
{
+ // This is a bit tricky! We want to ignore stackrefs with embedded
+ // refcounts when computing the incoming references, but otherwise treat
+ // them like normal.
+ if (!PyStackRef_RefcountOnObject(*ref) && (visit == visit_decref)) {
+ return 0;
+ }
Py_VISIT(PyStackRef_AsPyObjectBorrow(*ref));
return 0;
}
@@ -554,7 +560,7 @@ _PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg)
_PyStackRef *ref = _PyFrame_GetLocalsArray(frame);
/* locals and stack */
for (; ref < frame->stackpointer; ref++) {
- Py_VISIT(PyStackRef_AsPyObjectBorrow(*ref));
+ _Py_VISIT_STACKREF(*ref);
}
return 0;
}