Skip to content

gh-162350: Add AC vectorcall for contextvars.ContextVar - #162451

Merged
cmaloney merged 1 commit into
python:mainfrom
tanloong:contextvar_vectorcall
Nov 22, 2026
Merged

cmaloney merged 1 commit into
python:mainfrom
tanloong:contextvar_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written contextvar_tp_new with Argument Clinic + vectorcall.

Extra positional arguments now raise TypeError: ContextVar() takes exactly 1 positional argument (N given) instead of TypeError: ContextVar() takes at most 1 positional argument (N given).

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| ContextVar('x')                | 175 ns | 104 ns: 1.69x faster   |
| contextvars.ContextVar('x')    | 192 ns | 112 ns: 1.72x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.70x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import contextvars
ContextVar = contextvars.ContextVar
"""

CASES = [
    ("ContextVar('x')", "ContextVar('x')"),
    ("contextvars.ContextVar('x')", "contextvars.ContextVar('x')")
]

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

@cmaloney
cmaloney merged commit 3522677 into python:main Nov 22, 2026
62 checks passed
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