Skip to content

gh-162350: Use AC vectorcall for complex() - #162417

Open
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:landable/ac-vc-complex
Open

cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:landable/ac-vc-complex

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread Lib/test/test_complex.py Outdated
Comment on lines +614 to +615
self.assertRaisesRegex(TypeError, r"given by name \('imag'\) and position \(2\)",
complex, 4.25, 1.5, imag=1.5)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@cmaloney

Copy link
Copy Markdown
Contributor Author

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants