Skip to content

Commit 806f101

Browse files
cmaloneyclaude
andauthored
gh-162350: Use AC vectorcall for complex() (#162417)
Generate complex.__new__ and its vectorcall with Argument Clinic. To keep accepting a string only as a positional argument (complex('1') but not complex(real='1')), real and imag are declared twice: positional-only and keyword-only, with @text_signature keeping the visible "(real=0, imag=0)". The previous hand-written tp_new wrapper becomes the one-argument conversion called from complex_new_impl(). Error messages change when more than two arguments are passed in total: a duplicated or unknown keyword is now reported as such instead of "takes at most 2 arguments (N given)". Microbenchmarks (pyperf, PGO+LTO): | Benchmark | main | patch | |-------------------------------------|:-------:|:---------------------:| | complex(real=a_float, imag=a_float) | 67.4 ns | 37.9 ns: 1.78x faster | | complex(a_float, imag=a_float) | 62.2 ns | 39.2 ns: 1.59x faster | | complex(a_complex) | 18.9 ns | 12.0 ns: 1.57x faster | | complex(a_float, a_float) | 38.0 ns | 29.2 ns: 1.30x faster | | complex(an_int) | 36.9 ns | 29.9 ns: 1.24x faster | | complex(a_float) | 31.0 ns | 26.0 ns: 1.19x faster | | complex(a_str) | 48.4 ns | 41.8 ns: 1.16x faster | | complex() | 21.1 ns | 18.5 ns: 1.14x faster | Subclass construction (no vectorcall) is unchanged. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent af047f2 commit 806f101

3 files changed

Lines changed: 102 additions & 34 deletions

File tree

‎Lib/test/test_complex.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,12 @@ def check(z, x, y):
609609
check(complex(imag=1.5), 0.0, 1.5)
610610
check(complex(real=4.25, imag=1.5), 4.25, 1.5)
611611
check(complex(4.25, imag=1.5), 4.25, 1.5)
612+
self.assertRaisesRegex(TypeError, r"given by name \('real'\) and position \(1\)",
613+
complex, 4.25, real=1.5)
614+
self.assertRaisesRegex(TypeError, r"takes at most 2 arguments \(3 given\)",
615+
complex, 4.25, 1.5, imag=1.5)
616+
self.assertRaisesRegex(TypeError, r"takes at most 2 arguments \(3 given\)",
617+
complex, 1, 2, real=3)
612618

613619
# check that the sign of a zero in the real or imaginary part
614620
# is preserved when constructing from two floats.

‎Objects/clinic/complexobject.c.h‎

Lines changed: 53 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/complexobject.c‎

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,27 +1124,19 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
11241124
/* The constructor should only accept a string as a positional argument,
11251125
* not as by the 'real' keyword. But Argument Clinic does not allow
11261126
* to distinguish between argument passed positionally and by keyword.
1127-
* So the constructor must be split into two parts: actual_complex_new()
1128-
* handles the case of no arguments and one positional argument, and calls
1129-
* complex_new(), implemented with Argument Clinic, to handle the remaining
1130-
* cases: 'real' and 'imag' arguments. This separation is well suited
1131-
* for different constructor roles: converting a string or number to a complex
1127+
* So complex_new() declares 'real' and 'imag' twice: as positional-only and
1128+
* as keyword-only parameters. complex_new_impl() calls complex_from_arg()
1129+
* for one positional argument and handles the remaining cases itself:
1130+
* 'real' and 'imag' arguments. This separation is well suited for
1131+
* different constructor roles: converting a string or number to a complex
11321132
* number and constructing a complex number from real and imaginary parts.
11331133
*/
11341134
static PyObject *
1135-
actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1135+
complex_from_arg(PyTypeObject *type, PyObject *arg)
11361136
{
11371137
PyObject *res = NULL;
11381138
PyNumberMethods *nbr;
11391139

1140-
if (PyTuple_GET_SIZE(args) > 1 || (kwargs != NULL && PyDict_GET_SIZE(kwargs))) {
1141-
return complex_new(type, args, kwargs);
1142-
}
1143-
if (!PyTuple_GET_SIZE(args)) {
1144-
return complex_subtype_from_doubles(type, 0, 0);
1145-
}
1146-
1147-
PyObject *arg = PyTuple_GET_ITEM(args, 0);
11481140
/* Special-case for a single argument when type(arg) is complex. */
11491141
if (PyComplex_CheckExact(arg) && type == &PyComplex_Type) {
11501142
/* Note that we can't know whether it's safe to return
@@ -1193,10 +1185,16 @@ actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
11931185
}
11941186

11951187
/*[clinic input]
1188+
@vectorcall
11961189
@classmethod
1190+
@text_signature "(real=0, imag=0)"
11971191
complex.__new__ as complex_new
1198-
real as r: object(c_default="NULL") = 0
1199-
imag as i: object(c_default="NULL") = 0
1192+
real_pos: object = NULL
1193+
imag_pos: object = NULL
1194+
/
1195+
*
1196+
real as r: object = NULL
1197+
imag as i: object = NULL
12001198
12011199
Create a complex number from a string or numbers.
12021200
@@ -1207,8 +1205,9 @@ with the specified real and imaginary components.
12071205
[clinic start generated code]*/
12081206

12091207
static PyObject *
1210-
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
1211-
/*[clinic end generated code: output=b6c7dd577b537dc1 input=ff4268dc540958a4]*/
1208+
complex_new_impl(PyTypeObject *type, PyObject *real_pos, PyObject *imag_pos,
1209+
PyObject *r, PyObject *i)
1210+
/*[clinic end generated code: output=fdc6ecf8914112b7 input=09930f0cd44fd3a8]*/
12121211
{
12131212
PyObject *tmp;
12141213
PyNumberMethods *nbr, *nbi = NULL;
@@ -1217,7 +1216,31 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
12171216
int cr_is_complex = 0;
12181217
int ci_is_complex = 0;
12191218

1219+
/* See the comment before complex_from_arg(). */
1220+
int nargs = (real_pos != NULL) + (imag_pos != NULL) + (r != NULL) + (i != NULL);
1221+
if (nargs > 2) {
1222+
PyErr_Format(PyExc_TypeError,
1223+
"complex() takes at most 2 arguments (%d given)", nargs);
1224+
return NULL;
1225+
}
1226+
if (real_pos != NULL) {
1227+
if (r != NULL) {
1228+
PyErr_SetString(PyExc_TypeError, "argument for complex() given "
1229+
"by name ('real') and position (1)");
1230+
return NULL;
1231+
}
1232+
if (imag_pos == NULL && i == NULL) {
1233+
return complex_from_arg(type, real_pos);
1234+
}
1235+
r = real_pos;
1236+
}
1237+
if (imag_pos != NULL) {
1238+
i = imag_pos;
1239+
}
12201240
if (r == NULL) {
1241+
if (i == NULL) {
1242+
return complex_subtype_from_doubles(type, 0, 0);
1243+
}
12211244
r = _PyLong_GetZero();
12221245
}
12231246
PyObject *orig_r = r;
@@ -1457,7 +1480,8 @@ PyTypeObject PyComplex_Type = {
14571480
0, /* tp_dictoffset */
14581481
0, /* tp_init */
14591482
PyType_GenericAlloc, /* tp_alloc */
1460-
actual_complex_new, /* tp_new */
1483+
complex_new, /* tp_new */
14611484
PyObject_Free, /* tp_free */
1485+
.tp_vectorcall = complex_vectorcall,
14621486
.tp_version_tag = _Py_TYPE_VERSION_COMPLEX,
14631487
};

0 commit comments

Comments
 (0)