Repository navigation
gh-162350: Use AC vectorcall for complex() - #162417
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)
There was a problem hiding this comment.
It's still possible to get a couple error messages that say "takes 4 parameters"
Sure, something simple as
>>> complex(1, 2, 3, 4, 5)
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
complex(1, 2, 3, 4, 5)
~~~~~~~^^^^^^^^^^^^^^^
TypeError: complex() takes at most 4 arguments (5 given)that takes some new feature work to fix (AC needs to know how many argument positions alias eachother)
Can we have other use cases for this?
|
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>
There was a problem hiding this comment.
This still looks as a hack for me. But I think that any remaining edge cases, producing odd errors are not very important in practice.
So, probably it's OK as a temporary solution, until AC got support for multiple signatures. We have nice speedup on microbenchmarks and code looks simpler (no extra helper). LGTM, but I appreciate other reviews.
Note, that real-world performance impact, probably, will be negligible: I tried benchmarks from #135233 (add/mul and mysin): no significant difference. (Edit: another argument against NEWS entry, perhaps.)
|
As for other performance-related issues, this should be probably treated as a new feature PR, no? If so, this requires an approval from another core developer. CC @vstinner |
|
I've been viewing all these as a refactor generally "for general code refactoring that does not change user-facing behaviour". They tend to be fairly mechanical changes which improve performance while keeping user-facing behavior the same. The same arguments are accepted, the same exceptions raised, the same values computed for any given set of arguments. They do move more code into AC. Exception text does change but not type; type would be a breaking change. They're moving from older to newer forms: using the PEP 590 vectorcall more places. |
In general it's true. But this PR introduce a hack, that in principle has user-observable behavioral changes. |
|
Ack, happy to back out and do as an independent fix for this one |
|
No, now lets keep as is, unless Victor or Serhiy (or someone else) will be against this. |
|
Sounds good. Sorry I didn't quite connect the dots wanted two reviews for this one. |
|
This signature is ugly: it looks wrong to me to declare the two arguments twice: If you want to hack the code to get best performance, IMO Argument Clinic would be a better place than I'm not convinced that this hack is worth it. If you really want to keep it, please add a comment explaining why arguments are duplicated. |
Note, that this is more or less an internal implementation detail, it's almost hidden from users. Public signature is Though, now you can do something like
I think that this comment does it: cpython/Objects/complexobject.c Lines 1123 to 1132 in 6ffa102 No? |
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.