Skip to content

Commit 68195ff

Browse files
cmaloneyclaude
andcommitted
gh-162350: Use AC vectorcall for complex()
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 1cc5802 commit 68195ff

3 files changed

Lines changed: 99 additions & 34 deletions

File tree

‎Lib/test/test_complex.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ 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"given by name \('imag'\) and position \(2\)",
615+
complex, 4.25, 1.5, imag=1.5)
612616

613617
# check that the sign of a zero in the real or imaginary part
614618
# 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: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,27 +1121,19 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
11211121
/* The constructor should only accept a string as a positional argument,
11221122
* not as by the 'real' keyword. But Argument Clinic does not allow
11231123
* to distinguish between argument passed positionally and by keyword.
1124-
* So the constructor must be split into two parts: actual_complex_new()
1125-
* handles the case of no arguments and one positional argument, and calls
1126-
* complex_new(), implemented with Argument Clinic, to handle the remaining
1127-
* cases: 'real' and 'imag' arguments. This separation is well suited
1128-
* for different constructor roles: converting a string or number to a complex
1124+
* So complex_new() declares 'real' and 'imag' twice: as positional-only and
1125+
* as keyword-only parameters. complex_new_impl() calls complex_from_arg()
1126+
* for one positional argument and handles the remaining cases itself:
1127+
* 'real' and 'imag' arguments. This separation is well suited for
1128+
* different constructor roles: converting a string or number to a complex
11291129
* number and constructing a complex number from real and imaginary parts.
11301130
*/
11311131
static PyObject *
1132-
actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1132+
complex_from_arg(PyTypeObject *type, PyObject *arg)
11331133
{
11341134
PyObject *res = NULL;
11351135
PyNumberMethods *nbr;
11361136

1137-
if (PyTuple_GET_SIZE(args) > 1 || (kwargs != NULL && PyDict_GET_SIZE(kwargs))) {
1138-
return complex_new(type, args, kwargs);
1139-
}
1140-
if (!PyTuple_GET_SIZE(args)) {
1141-
return complex_subtype_from_doubles(type, 0, 0);
1142-
}
1143-
1144-
PyObject *arg = PyTuple_GET_ITEM(args, 0);
11451137
/* Special-case for a single argument when type(arg) is complex. */
11461138
if (PyComplex_CheckExact(arg) && type == &PyComplex_Type) {
11471139
/* Note that we can't know whether it's safe to return
@@ -1190,10 +1182,16 @@ actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
11901182
}
11911183

11921184
/*[clinic input]
1185+
@vectorcall
11931186
@classmethod
1187+
@text_signature "(real=0, imag=0)"
11941188
complex.__new__ as complex_new
1195-
real as r: object(c_default="NULL") = 0
1196-
imag as i: object(c_default="NULL") = 0
1189+
real_pos: object = NULL
1190+
imag_pos: object = NULL
1191+
/
1192+
*
1193+
real as r: object = NULL
1194+
imag as i: object = NULL
11971195
11981196
Create a complex number from a string or numbers.
11991197
@@ -1204,8 +1202,9 @@ with the specified real and imaginary components.
12041202
[clinic start generated code]*/
12051203

12061204
static PyObject *
1207-
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
1208-
/*[clinic end generated code: output=b6c7dd577b537dc1 input=ff4268dc540958a4]*/
1205+
complex_new_impl(PyTypeObject *type, PyObject *real_pos, PyObject *imag_pos,
1206+
PyObject *r, PyObject *i)
1207+
/*[clinic end generated code: output=fdc6ecf8914112b7 input=09930f0cd44fd3a8]*/
12091208
{
12101209
PyObject *tmp;
12111210
PyNumberMethods *nbr, *nbi = NULL;
@@ -1214,7 +1213,30 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
12141213
int cr_is_complex = 0;
12151214
int ci_is_complex = 0;
12161215

1216+
/* See the comment before complex_from_arg(). */
1217+
if (real_pos != NULL) {
1218+
if (r != NULL) {
1219+
PyErr_SetString(PyExc_TypeError, "argument for complex() given "
1220+
"by name ('real') and position (1)");
1221+
return NULL;
1222+
}
1223+
if (imag_pos == NULL && i == NULL) {
1224+
return complex_from_arg(type, real_pos);
1225+
}
1226+
r = real_pos;
1227+
}
1228+
if (imag_pos != NULL) {
1229+
if (i != NULL) {
1230+
PyErr_SetString(PyExc_TypeError, "argument for complex() given "
1231+
"by name ('imag') and position (2)");
1232+
return NULL;
1233+
}
1234+
i = imag_pos;
1235+
}
12171236
if (r == NULL) {
1237+
if (i == NULL) {
1238+
return complex_subtype_from_doubles(type, 0, 0);
1239+
}
12181240
r = _PyLong_GetZero();
12191241
}
12201242
PyObject *orig_r = r;
@@ -1454,7 +1476,8 @@ PyTypeObject PyComplex_Type = {
14541476
0, /* tp_dictoffset */
14551477
0, /* tp_init */
14561478
PyType_GenericAlloc, /* tp_alloc */
1457-
actual_complex_new, /* tp_new */
1479+
complex_new, /* tp_new */
14581480
PyObject_Free, /* tp_free */
1481+
.tp_vectorcall = complex_vectorcall,
14591482
.tp_version_tag = _Py_TYPE_VERSION_COMPLEX,
14601483
};

0 commit comments

Comments
 (0)