Skip to content

gh-162350: Use AC vectorcall for framelocalsproxy_new to speed up types.FrameLocalsProxyType - #162424

Open
tanloong wants to merge 2 commits into
python:mainfrom
tanloong:framelocalsproxy_vectorcall
Open

tanloong wants to merge 2 commits into
python:mainfrom
tanloong:framelocalsproxy_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written framelocalsproxy_new with Argument Clinic + Vectorcall. Extra keywords now report FrameLocalsProxy() takes no keyword arguments instead of FrameLocalsProxy takes no keyword arguments.

Didn't keep old signature with@text_signature like in #162422 (comment) because inspect.signature(types.FrameLocalsProxyType) remains (frame, /).

Benchmark (default options):

+-----------------------------------+--------+----------------------+
| Benchmark                         | main   | patch                |
+===================================+========+======================+
| FrameLocalsProxyType(frame)       | 127 ns | 104 ns: 1.22x faster |
+-----------------------------------+--------+----------------------+
| types.FrameLocalsProxyType(frame) | 133 ns | 108 ns: 1.24x faster |
+-----------------------------------+--------+----------------------+
| Geometric mean                    | (ref)  | 1.23x faster         |
+-----------------------------------+--------+----------------------+
Details
import pyperf

SETUP = """
import sys
import types
from types import FrameLocalsProxyType

def f(self):
    pass

class C:
    def m(self):
        pass

obj = C()
code = f.__code__
gns = globals()
d = {}
frame = sys._getframe()
"""

CASES = [
    ("FrameLocalsProxyType(frame)", "FrameLocalsProxyType(frame)"),
    ("types.FrameLocalsProxyType(frame)", "types.FrameLocalsProxyType(frame)"),
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most code in cpython calls _PyFrameLocalsProxy_New to get this which still builds a tuple to do call protocol. Move it to call framelocalsproxy_new_impl directly which saves a bit of work in more cases.

With these PRs please include how argument errors change (extra keywords in this case now show up as FrameLocalsProxy() takes no keyword arguments). That's a reasonable change, but want to make the decision to change it recorded.

@tanloong

Copy link
Copy Markdown
Contributor Author

Have moved to call framelocalsproxy_new_impl directly in _PyFrameLocalsProxy_New, and included error message change in PR description.

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PRs! This one looks good to me. I'm away from my workstation a bit so will be a bit until this gets merged. Appreciate the work in building these PRs

@tanloong

Copy link
Copy Markdown
Contributor Author

Thank you for the quick review, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants