From d14c9255bc705a24375ab8433963a6272491b075 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Mon, 28 Sep 2020 04:53:17 -0400 Subject: [PATCH 1/3] Add vectorcall for float() --- Objects/floatobject.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 0606f29ff5408d..ef61ae7aada4e3 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1649,6 +1649,23 @@ 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; + } + + return float_new_impl((PyTypeObject *)type, args[0]); +} + + /*[clinic input] float.__getnewargs__ [clinic start generated code]*/ @@ -1937,6 +1954,7 @@ PyTypeObject PyFloat_Type = { 0, /* tp_init */ 0, /* tp_alloc */ float_new, /* tp_new */ + .tp_vectorcall = (vectorcallfunc)float_vectorcall, }; int From 69e31c644b2ebd860063dc02d94c9e2441b1b902 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 28 Sep 2020 08:58:30 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst b/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst new file mode 100644 index 00000000000000..ee2636704c2992 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst @@ -0,0 +1 @@ +Calls to ``float()`` are now faster due to the ``vectorcall`` calling convention. Patch by Dennis Sweeney. \ No newline at end of file From 7c9385858858cf9e45ecf46e5fedbaf2cad0f9ef Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Mon, 28 Sep 2020 05:24:58 -0400 Subject: [PATCH 3/3] Fix zero-argument case --- Lib/test/test_float.py | 3 +++ Objects/floatobject.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 9651281e24edbe..99c81f0b72a5a3 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -64,6 +64,9 @@ def test_float(self): # See bpo-34087 self.assertRaises(ValueError, float, '\u3053\u3093\u306b\u3061\u306f') + def test_noargs(self): + self.assertEqual(float(), 0.0) + def test_underscores(self): for lit in VALID_UNDERSCORE_LITERALS: if not any(ch in lit for ch in 'jJxXoObB'): diff --git a/Objects/floatobject.c b/Objects/floatobject.c index ef61ae7aada4e3..d0af0ea1a98257 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1662,7 +1662,8 @@ float_vectorcall(PyObject *type, PyObject * const*args, return NULL; } - return float_new_impl((PyTypeObject *)type, args[0]); + PyObject *x = nargs >= 1 ? args[0] : _PyLong_Zero; + return float_new_impl((PyTypeObject *)type, x); }