Skip to content

gh-162350: Add AC vectorcall for string.templatelib - #162452

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

cmaloney merged 1 commit into
python:mainfrom
tanloong:templatelib_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Add AC vectorcall for Template and Interpolation.

Template now raises TypeError: Template() takes no keyword arguments for kwargs instead of TypeError: Template.__new__ only accepts *args arguments.

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| Template('a')                  | 177 ns | 134 ns: 1.32x faster   |
| Interpolation(1)               | 148 ns | 105 ns: 1.41x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.36x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
from string.templatelib import Template, Interpolation
"""

CASES = [
    ("Template('a')", "Template('a')"),
    ('Interpolation(1)', 'Interpolation(1)')
]

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

Copy link
Copy Markdown
Contributor

this does have a slight behavior change which I think is okay:

Template('a', **{})                     base: TypeError   head: Template(strings=('a',), ...)
Template(**{})                          base: TypeError   head: Template(strings=('',), ...)
Template.__new__(Template, 'a', **{})   base: TypeError   head: OK

(Some AC work I'm planning may adjust that case, but not important for the moment)

@cmaloney
cmaloney merged commit e8bc97b 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

2 participants