Skip to content

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

Merged
cmaloney merged 2 commits into
python:mainfrom
cmaloney:landable/ac-vc-complex
Nov 28, 2026
Merged

cmaloney merged 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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

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.

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?

@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>
@skirpichev
skirpichev self-requested a review November 22, 2026 09:13

@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.

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.)

@cmaloney
cmaloney merged commit 806f101 into python:main Nov 28, 2026
54 checks passed
@cmaloney
cmaloney deleted the landable/ac-vc-complex branch November 28, 2026 04:14
@skirpichev

Copy link
Copy Markdown
Member

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

@cmaloney

Copy link
Copy Markdown
Contributor Author

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.

@skirpichev

Copy link
Copy Markdown
Member

I've been viewing all these as a refactor generally "for general code refactoring that does not change user-facing behaviour".

In general it's true. But this PR introduce a hack, that in principle has user-observable behavioral changes.

@cmaloney

Copy link
Copy Markdown
Contributor Author

Ack, happy to back out and do as an independent fix for this one

@skirpichev

Copy link
Copy Markdown
Member

No, now lets keep as is, unless Victor or Serhiy (or someone else) will be against this.

@cmaloney

Copy link
Copy Markdown
Contributor Author

Sounds good. Sorry I didn't quite connect the dots wanted two reviews for this one.

@vstinner

Copy link
Copy Markdown
Member

This signature is ugly: it looks wrong to me to declare the two arguments twice:

complex.__new__ as complex_new
    real_pos: object = NULL
    imag_pos: object = NULL
    /
    *
    real as r: object = NULL
    imag as i: object = NULL

If you want to hack the code to get best performance, IMO Argument Clinic would be a better place than complex.__new__().

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.

cc @serhiy-storchaka

@skirpichev

Copy link
Copy Markdown
Member

This signature is ugly: it looks wrong to me to declare the two arguments twice

Note, that this is more or less an internal implementation detail, it's almost hidden from users. Public signature is complex(real=0, imag=0), as before. (Which is also lie.)

Though, now you can do something like complex(1, 2, 3, 4, 5) to get a surprising error message. But if you feed 2 or less arguments - nothing bad happens.

please add a comment explaining why arguments are duplicated.

I think that this comment does it:

/* The constructor should only accept a string as a positional argument,
* not as by the 'real' keyword. But Argument Clinic does not allow
* to distinguish between argument passed positionally and by keyword.
* So complex_new() declares 'real' and 'imag' twice: as positional-only and
* as keyword-only parameters. complex_new_impl() calls complex_from_arg()
* for one positional argument and handles the remaining cases itself:
* 'real' and 'imag' arguments. This separation is well suited for
* different constructor roles: converting a string or number to a complex
* number and constructing a complex number from real and imaginary parts.

No?

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.

3 participants