https://github.com/python/cpython/commit/e8bc97b55d1ba76b2593608919e4300e525310fd
commit: e8bc97b55d1ba76b2593608919e4300e525310fd
branch: main
author: Tan Long <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-22T14:51:10-08:00
summary:
gh-162350: Add AC vectorcall for string.templatelib (#162452)
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 |
+--------------------------------+--------+------------------------+
```
```python
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)
```
files:
A Objects/clinic/templateobject.c.h
M Objects/clinic/interpolationobject.c.h
M Objects/interpolationobject.c
M Objects/templateobject.c
diff --git a/Objects/clinic/interpolationobject.c.h
b/Objects/clinic/interpolationobject.c.h
index 2030e17e49e47a..665f00462fe2c6 100644
--- a/Objects/clinic/interpolationobject.c.h
+++ b/Objects/clinic/interpolationobject.c.h
@@ -14,7 +14,8 @@ interpolation_new_impl(PyTypeObject *type, PyObject *value,
PyObject *format_spec);
static PyObject *
-interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+interpolation_new_helper(PyTypeObject *type, PyObject *const *args,
+ Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@@ -46,14 +47,13 @@ interpolation_new(PyTypeObject *type, PyObject *args,
PyObject *kwargs)
#undef KWTUPLE
PyObject *argsbuf[4];
PyObject * const *fastargs;
- Py_ssize_t nargs = PyTuple_GET_SIZE(args);
- Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ Py_ssize_t noptargs = nargs + nkw - 1;
PyObject *value;
PyObject *expression = &_Py_STR(empty);
PyObject *conversion = Py_None;
PyObject *format_spec = &_Py_STR(empty);
- fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs,
kwargs, NULL, &_parser,
+ fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
/*minpos*/ 1, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
if (!fastargs) {
goto exit;
@@ -91,4 +91,63 @@ interpolation_new(PyTypeObject *type, PyObject *args,
PyObject *kwargs)
exit:
return return_value;
}
-/*[clinic end generated code: output=2391391e2d7708c0 input=a9049054013a1b77]*/
+
+static PyObject *
+interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ return interpolation_new_helper(type, _PyTuple_CAST(args)->ob_item,
+ PyTuple_GET_SIZE(args),
+ kwargs ? PyDict_GET_SIZE(kwargs) : 0,
+ kwargs, NULL);
+}
+
+static PyObject *
+interpolation_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *value;
+ PyObject *expression = &_Py_STR(empty);
+ PyObject *conversion = Py_None;
+ PyObject *format_spec = &_Py_STR(empty);
+
+ assert(_PyType_CAST(type)->tp_new == interpolation_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 (kwnames != NULL || nargs < 1 || nargs > 4) {
+ return interpolation_new_helper(_PyType_CAST(type), args, nargs,
+ kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
+ NULL, kwnames);
+ }
+ value = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ if (!PyUnicode_Check(args[1])) {
+ _PyArg_BadArgument("Interpolation", "argument 'expression'", "str",
args[1]);
+ goto exit;
+ }
+ expression = args[1];
+ if (nargs < 3) {
+ goto skip_optional;
+ }
+ if (!_conversion_converter(args[2], &conversion)) {
+ goto exit;
+ }
+ if (nargs < 4) {
+ goto skip_optional;
+ }
+ if (!PyUnicode_Check(args[3])) {
+ _PyArg_BadArgument("Interpolation", "argument 'format_spec'", "str",
args[3]);
+ goto exit;
+ }
+ format_spec = args[3];
+skip_optional:
+ return_value = interpolation_new_impl(_PyType_CAST(type), value,
expression, conversion, format_spec);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=d07caaaa0e78ca15 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/templateobject.c.h
b/Objects/clinic/templateobject.c.h
new file mode 100644
index 00000000000000..7719fcf47bb80e
--- /dev/null
+++ b/Objects/clinic/templateobject.c.h
@@ -0,0 +1,55 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#include "pycore_modsupport.h" // _PyArg_NoKeywords()
+#include "pycore_tuple.h" // _PyTuple_ITEMS()
+
+static PyObject *
+template_new_impl(PyTypeObject *type, PyObject * const *args,
+ Py_ssize_t args_length);
+
+static PyObject *
+template_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ PyTypeObject *base_tp = &_PyTemplate_Type;
+ PyObject * const *__clinic_args;
+ Py_ssize_t args_length;
+
+ if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
+ !_PyArg_NoKeywords("Template", kwargs)) {
+ goto exit;
+ }
+ __clinic_args = _PyTuple_ITEMS(args);
+ args_length = PyTuple_GET_SIZE(args);
+ return_value = template_new_impl(type, __clinic_args, args_length);
+
+exit:
+ return return_value;
+}
+
+static PyObject *
+template_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject * const *__clinic_args;
+ Py_ssize_t args_length;
+
+ assert(_PyType_CAST(type)->tp_new == template_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("Template", kwnames)) {
+ goto exit;
+ }
+ __clinic_args = args;
+ args_length = nargs;
+ return_value = template_new_impl(_PyType_CAST(type), __clinic_args,
args_length);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=38d45f3b38a21517 input=a9049054013a1b77]*/
diff --git a/Objects/interpolationobject.c b/Objects/interpolationobject.c
index e37724fb7852a2..96fabcf201bb97 100644
--- a/Objects/interpolationobject.c
+++ b/Objects/interpolationobject.c
@@ -50,6 +50,7 @@ typedef struct {
(assert(_PyInterpolation_CheckExact(op)), _Py_CAST(interpolationobject*,
(op)))
/*[clinic input]
+@vectorcall
@classmethod
Interpolation.__new__ as interpolation_new
@@ -63,7 +64,7 @@ static PyObject *
interpolation_new_impl(PyTypeObject *type, PyObject *value,
PyObject *expression, PyObject *conversion,
PyObject *format_spec)
-/*[clinic end generated code: output=6488e288765bc1a9 input=fc5c285c1dd23d36]*/
+/*[clinic end generated code: output=6488e288765bc1a9 input=de7e890ea8f801e5]*/
{
interpolationobject *self = PyObject_GC_New(interpolationobject, type);
if (!self) {
@@ -150,6 +151,7 @@ PyTypeObject _PyInterpolation_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_new = interpolation_new,
+ .tp_vectorcall = interpolation_vectorcall,
.tp_alloc = PyType_GenericAlloc,
.tp_dealloc = interpolation_dealloc,
.tp_clear = interpolation_clear,
diff --git a/Objects/templateobject.c b/Objects/templateobject.c
index 994b352d7dcd63..de7448ae882e14 100644
--- a/Objects/templateobject.c
+++ b/Objects/templateobject.c
@@ -5,6 +5,13 @@
#include "pycore_runtime.h" // _Py_STR()
#include "pycore_template.h"
+#include "clinic/templateobject.c.h"
+
+/*[clinic input]
+class Template "templateobject *" "&_PyTemplate_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee41f8d8c49f0271]*/
+
typedef struct {
PyObject_HEAD
PyObject *stringsiter;
@@ -89,21 +96,24 @@ typedef struct {
#define templateobject_CAST(op) \
(assert(_PyTemplate_CheckExact(op)), _Py_CAST(templateobject*, (op)))
+/*[clinic input]
+@vectorcall
+@classmethod
+Template.__new__ as template_new
+ *args: array
+[clinic start generated code]*/
+
static PyObject *
-template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+template_new_impl(PyTypeObject *type, PyObject * const *args,
+ Py_ssize_t args_length)
+/*[clinic end generated code: output=5831a1056a1a68d5 input=65f04e91969ffccd]*/
{
- if (kwds != NULL) {
- PyErr_SetString(PyExc_TypeError, "Template.__new__ only accepts *args
arguments");
- return NULL;
- }
-
- Py_ssize_t argslen = PyTuple_GET_SIZE(args);
Py_ssize_t stringslen = 0;
Py_ssize_t interpolationslen = 0;
int last_was_str = 0;
- for (Py_ssize_t i = 0; i < argslen; i++) {
- PyObject *item = PyTuple_GET_ITEM(args, i);
+ for (Py_ssize_t i = 0; i < args_length; i++) {
+ PyObject *item = args[i];
if (PyUnicode_Check(item)) {
if (!last_was_str) {
stringslen++;
@@ -142,8 +152,8 @@ template_new(PyTypeObject *type, PyObject *args, PyObject
*kwds)
last_was_str = 0;
Py_ssize_t stringsidx = 0, interpolationsidx = 0;
- for (Py_ssize_t i = 0; i < argslen; i++) {
- PyObject *item = PyTuple_GET_ITEM(args, i);
+ for (Py_ssize_t i = 0; i < args_length; i++) {
+ PyObject *item = args[i];
if (PyUnicode_Check(item)) {
if (last_was_str) {
PyObject *laststring = PyTuple_GET_ITEM(strings, stringsidx -
1);
@@ -391,6 +401,7 @@ PyTypeObject _PyTemplate_Type = {
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_as_sequence = &template_as_sequence,
.tp_new = template_new,
+ .tp_vectorcall = template_vectorcall,
.tp_alloc = PyType_GenericAlloc,
.tp_dealloc = template_dealloc,
.tp_clear = template_clear,
_______________________________________________
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]