https://github.com/python/cpython/commit/fd713538c5e71d8a04637ba21a8823e64e6043fa
commit: fd713538c5e71d8a04637ba21a8823e64e6043fa
branch: main
author: Tan Long <[email protected]>
committer: asvetlov <[email protected]>
date: 2026-11-22T16:54:03+01:00
summary:
gh-162350: Use AC vectorcall for framelocalsproxy_new to speed up
types.FrameLocalsProxyType (#162424)
* FrameLocalsProxy AC vectorcall
* call framelocalsproxy_new_impl() directly in _PyFrameLocalsProxy_New
files:
M Objects/clinic/frameobject.c.h
M Objects/frameobject.c
diff --git a/Objects/clinic/frameobject.c.h b/Objects/clinic/frameobject.c.h
index f7e764a0755ecf..9ea0eaa662a164 100644
--- a/Objects/clinic/frameobject.c.h
+++ b/Objects/clinic/frameobject.c.h
@@ -3,6 +3,65 @@ preserve
[clinic start generated code]*/
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
+#include "pycore_modsupport.h" // _PyArg_CheckPositional()
+
+PyDoc_STRVAR(framelocalsproxy_new__doc__,
+"FrameLocalsProxy(frame, /)\n"
+"--\n"
+"\n"
+"Create a write-through view of the locals dictionary for a frame.\n"
+"\n"
+" frame\n"
+" the frame object to wrap.");
+
+static PyObject *
+framelocalsproxy_new_impl(PyTypeObject *type, PyObject *frame);
+
+static PyObject *
+framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ PyTypeObject *base_tp = &PyFrameLocalsProxy_Type;
+ PyObject *frame;
+
+ if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
+ !_PyArg_NoKeywords("FrameLocalsProxy", kwargs)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("FrameLocalsProxy", PyTuple_GET_SIZE(args), 1,
1)) {
+ goto exit;
+ }
+ frame = PyTuple_GET_ITEM(args, 0);
+ return_value = framelocalsproxy_new_impl(type, frame);
+
+exit:
+ return return_value;
+}
+
+static PyObject *
+framelocalsproxy_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *frame;
+
+ assert(_PyType_CAST(type)->tp_new == framelocalsproxy_new);
+ /* Make sure the type object is immutable: the generated
+ * vectorcall doesn't deal e.g. with users reassigning __init__. */
+ assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
+ if (!_PyArg_NoKwnames("FrameLocalsProxy", kwnames)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("FrameLocalsProxy", nargs, 1, 1)) {
+ goto exit;
+ }
+ frame = args[0];
+ return_value = framelocalsproxy_new_impl(_PyType_CAST(type), frame);
+
+exit:
+ return return_value;
+}
PyDoc_STRVAR(frame_locals__doc__,
"Return the mapping used by the frame to look up local variables.");
@@ -322,4 +381,4 @@ frame_repr(PyObject *self)
#define FRAME_F_GENERATOR_GETSETDEF {"f_generator",
(getter)frame_generator_get, (setter)NULL, frame_generator__doc__},
-/*[clinic end generated code: output=e86fc251b0396a11 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e6d74bb87e4b1a95 input=a9049054013a1b77]*/
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 475be76dc3da21..cb987f1606611f 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -40,6 +40,11 @@ class frame "PyFrameObject *" "&PyFrame_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2d1dbf2e06cf351f]*/
+/*[clinic input]
+class FrameLocalsProxy "PyFrameLocalsProxyObject *" "&PyFrameLocalsProxy_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b877b1c7551e6925]*/
+
// Returns new reference or NULL
static PyObject *
@@ -452,36 +457,32 @@ framelocalsproxy_dealloc(PyObject *self)
Py_TYPE(self)->tp_free(self);
}
+/*[clinic input]
+@vectorcall
+@classmethod
+FrameLocalsProxy.__new__ as framelocalsproxy_new
+ frame: object
+ /
+
+Create a write-through view of the locals dictionary for a frame.
+
+ frame
+ the frame object to wrap.
+[clinic start generated code]*/
+
static PyObject *
-framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+framelocalsproxy_new_impl(PyTypeObject *type, PyObject *frame)
+/*[clinic end generated code: output=803b377132a928b9 input=5ab4ffd26ec8e290]*/
{
- if (PyTuple_GET_SIZE(args) != 1) {
- PyErr_Format(PyExc_TypeError,
- "FrameLocalsProxy expected 1 argument, got %zd",
- PyTuple_GET_SIZE(args));
- return NULL;
- }
- PyObject *item = PyTuple_GET_ITEM(args, 0);
-
- if (!PyFrame_Check(item)) {
- PyErr_Format(PyExc_TypeError, "expect frame, not %T", item);
+ if (!PyFrame_Check(frame)) {
+ PyErr_Format(PyExc_TypeError, "expect frame, not %T", frame);
return NULL;
}
- PyFrameObject *frame = (PyFrameObject*)item;
-
- if (kwds != NULL && PyDict_Size(kwds) != 0) {
- PyErr_SetString(PyExc_TypeError,
- "FrameLocalsProxy takes no keyword arguments");
- return 0;
- }
-
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject
*)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
}
-
- ((PyFrameLocalsProxyObject*)self)->frame =
(PyFrameObject*)Py_NewRef(frame);
-
+ self->frame = (PyFrameObject *)Py_NewRef(frame);
return (PyObject *)self;
}
@@ -988,15 +989,6 @@ static PyMethodDef framelocalsproxy_methods[] = {
{NULL, NULL} /* sentinel */
};
-PyDoc_STRVAR(framelocalsproxy_doc,
-"FrameLocalsProxy($frame)\n"
-"--\n"
-"\n"
-"Create a write-through view of the locals dictionary for a frame.\n"
-"\n"
-" frame\n"
-" the frame object to wrap.");
-
PyTypeObject PyFrameLocalsProxy_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "FrameLocalsProxy",
@@ -1017,20 +1009,14 @@ PyTypeObject PyFrameLocalsProxy_Type = {
.tp_alloc = PyType_GenericAlloc,
.tp_new = framelocalsproxy_new,
.tp_free = PyObject_GC_Del,
- .tp_doc = framelocalsproxy_doc,
+ .tp_doc = framelocalsproxy_new__doc__,
+ .tp_vectorcall = framelocalsproxy_vectorcall,
};
PyObject *
_PyFrameLocalsProxy_New(PyFrameObject *frame)
{
- PyObject* args = PyTuple_Pack(1, frame);
- if (args == NULL) {
- return NULL;
- }
-
- PyObject* proxy = framelocalsproxy_new(&PyFrameLocalsProxy_Type, args,
NULL);
- Py_DECREF(args);
- return proxy;
+ return framelocalsproxy_new_impl(&PyFrameLocalsProxy_Type, (PyObject
*)frame);
}
static PyMemberDef frame_memberlist[] = {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]