https://github.com/python/cpython/commit/50082b8d89392b7655eb9a2790731b613cbc620f
commit: 50082b8d89392b7655eb9a2790731b613cbc620f
branch: main
author: Cody Maloney <[email protected]>
committer: encukou <[email protected]>
date: 2026-10-13T12:21:19+02:00
summary:
gh-87613: Use AC vectorcall for float (GH-158201)
Move float.__new__ to AC vectorcall dropping the hand-written.
Running microbenchmark from the original add (`pyperf timeit "float(0)"`
in gh-86039) shows no significant performance change.
The generated name matches the hand-written name so the tp_vectorcall
slot is unchanged.
Update the vectorcall slot-parity test to include float.
files:
M Lib/test/test_clinic.py
M Objects/clinic/floatobject.c.h
M Objects/floatobject.c
diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py
index 3b440fed7f6ce8a..0274edd384da439 100644
--- a/Lib/test/test_clinic.py
+++ b/Lib/test/test_clinic.py
@@ -5665,7 +5665,8 @@ def through_init(cls):
return cls, partial(cls.__init__, cls(1))
entry_points = [
- through_new(enumerate), # the only non-test @vectorcall function
+ through_new(enumerate),
+ through_new(float),
through_new(ac_tester.VcNew),
through_new(ac_tester.VcNewBase),
through_new(ac_tester.VcKwOnly),
diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h
index cc37ea55612a8fb..0e4c0614b592d50 100644
--- a/Objects/clinic/floatobject.c.h
+++ b/Objects/clinic/floatobject.c.h
@@ -241,6 +241,35 @@ float_new(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
return return_value;
}
+static PyObject *
+float_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *x = NULL;
+
+ assert(Py_Is(_PyType_CAST(type), &PyFloat_Type));
+ /* 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("float", kwnames)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("float", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ x = args[0];
+skip_optional:
+ return_value = float_new_impl(_PyType_CAST(type), x);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(float_from_number__doc__,
"from_number($type, number, /)\n"
"--\n"
@@ -349,4 +378,4 @@ float___format__(PyObject *self, PyObject *arg)
exit:
return return_value;
}
-/*[clinic end generated code: output=e0ffc05aa7fc4b10 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=95aaa6d7174f9e40 input=a9049054013a1b77]*/
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 5095da01e45dd22..d5a879cdd53ad62 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1556,6 +1556,7 @@ static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *x);
/*[clinic input]
+@vectorcall
@classmethod
float.__new__ as float_new
x: object(c_default="NULL") = 0
@@ -1566,7 +1567,7 @@ Convert a string or number to a floating-point number, if
possible.
static PyObject *
float_new_impl(PyTypeObject *type, PyObject *x)
-/*[clinic end generated code: output=ccf1e8dc460ba6ba input=55909f888aa0c8a6]*/
+/*[clinic end generated code: output=ccf1e8dc460ba6ba input=39b40d663c372a91]*/
{
if (type != &PyFloat_Type) {
if (x == NULL) {
@@ -1610,23 +1611,6 @@ float_subtype_new(PyTypeObject *type, PyObject *x)
return newobj;
}
-static PyObject *
-float_vectorcall(PyObject *type, PyObject *const *args,
- size_t nargsf, PyObject *kwnames)
-{
- if (!_PyArg_NoKwnames("float", kwnames)) {
- return NULL;
- }
-
- Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
- if (!_PyArg_CheckPositional("float", nargs, 0, 1)) {
- return NULL;
- }
-
- PyObject *x = nargs >= 1 ? args[0] : NULL;
- return float_new_impl(_PyType_CAST(type), x);
-}
-
/*[clinic input]
@classmethod
_______________________________________________
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]