Repository navigation
Conversation
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>
skirpichev
left a comment
There was a problem hiding this comment.
Looks promising, but I think that error messages could be adjusted to match the master. See examples.
Probably you should also add changelog entry, as speedup looks noticeable. Perhaps, also include few subclasses examples in your benchmark. (Unfortunately, pyperformance suite has no benchmarks for complex numbers.)
CC @serhiy-storchaka, what do you think?
| self.assertRaisesRegex(TypeError, r"given by name \('imag'\) and position \(2\)", | ||
| complex, 4.25, 1.5, imag=1.5) |
There was a problem hiding this comment.
In the master we have instead
>>> complex(4.25, 1.5, imag=1.5)
Traceback (most recent call last):
File "<python-input-5>", line 1, in <module>
complex(4.25, 1.5, imag=1.5)
~~~~~~~^^^^^^^^^^^^^^^^^^^^^
TypeError: complex() takes at most 2 arguments (3 given)Also:
>>> complex(1, 2, real=3)
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
complex(1, 2, real=3)
~~~~~~~^^^^^^^^^^^^^^
TypeError: complex() takes at most 2 arguments (3 given)vs in this pr:
>>> complex(1, 2, real=3)
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
complex(1, 2, real=3)
~~~~~~~^^^^^^^^^^^^^^
TypeError: argument for complex() given by name ('real') and position (1)I think error messages should match such for one of supported signatures. In this constructor we have essentially 3:
.. class:: complex(number=0, /)
complex(string, /)
complex(real=0, imag=0)There was a problem hiding this comment.
Adjusted these cases. It's still possible to get a couple error messages that say "takes 4 parameters", that takes some new feature work to fix (AC needs to know how many argument positions alias eachother)
|
re: NEWS I was avoiding them for individual Vectorcalls as there is not a lot "actionable", we just made the internals faster. These already fall under the "Interpreter startup is faster" What's New (https://docs.python.org/3.16/whatsnew/3.16.html#optimizations), and once have more of these landed I'm planning to add another high level What's New for all the performance improvements pointing to Vectorcall. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generate
complex.__new__and its vectorcall with Argument Clinic. To keep accepting a string only as a positional argument (complex('1')but notcomplex(real='1')), real and imag are declared twice: positional-only and keyword-only, with@text_signaturekeeping 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):
Subclass construction (no vectorcall) is unchanged.