Skip to content

gh-162428: Use the freelist for exact complex() constructor results - #162429

Merged
StanFromIreland merged 1 commit into
python:mainfrom
cmaloney:landable/complex-ctor-freelist
Nov 22, 2026
Merged

StanFromIreland merged 1 commit into
python:mainfrom
cmaloney:landable/complex-ctor-freelist

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

complex_subtype_from_c_complex() allocated through tp_alloc even for exact complex, so results of complex(...) never came from the complex freelist even though complex_dealloc() pushed them onto it.

Benchmark main branch
complex() 20.9 ns 16.3 ns: 1.29x faster
complex(an_int) 37.7 ns 29.8 ns: 1.27x faster
complex(a_float) 31.5 ns 25.4 ns: 1.24x faster
complex(a_float, a_float) 39.0 ns 32.5 ns: 1.20x faster
complex(a_str) 49.6 ns 42.1 ns: 1.18x faster
complex(real=a_float, imag=a_float) 70.5 ns 63.1 ns: 1.12x faster
complex(a_float, imag=a_float) 63.4 ns 56.7 ns: 1.12x faster
complex(a_complex) 20.2 ns 19.3 ns: 1.04x faster
SubComplex(a_float) 41.4 ns 40.6 ns: 1.02x faster

Not significant: float(a_float) (control).

import pyperf

runner = pyperf.Runner()
setup = """
class SubComplex(complex): pass
a_float = 1.5; an_int = 3; a_complex = 1+2j; a_str = '1+2j'
"""
for stmt in [
    "complex()",
    "complex(a_float)",
    "complex(an_int)",
    "complex(a_complex)",
    "complex(a_str)",
    "complex(a_float, a_float)",
    "complex(a_float, imag=a_float)",
    "complex(real=a_float, imag=a_float)",
    "SubComplex(a_float)",
    "float(a_float)",
]:
    runner.timeit(stmt, stmt, setup=setup)

cc: @skirpichev @eendebakpt

…ults

complex_subtype_from_c_complex() allocated through tp_alloc even for
exact complex, so results of complex(...) never came from the complex
freelist even though complex_dealloc() pushed them onto it.

| Benchmark                           | main    | branch                |
|-------------------------------------|:-------:|:---------------------:|
| complex()                           | 20.9 ns | 16.3 ns: 1.29x faster |
| complex(an_int)                     | 37.7 ns | 29.8 ns: 1.27x faster |
| complex(a_float)                    | 31.5 ns | 25.4 ns: 1.24x faster |
| complex(a_float, a_float)           | 39.0 ns | 32.5 ns: 1.20x faster |
| complex(a_str)                      | 49.6 ns | 42.1 ns: 1.18x faster |
| complex(real=a_float, imag=a_float) | 70.5 ns | 63.1 ns: 1.12x faster |
| complex(a_float, imag=a_float)      | 63.4 ns | 56.7 ns: 1.12x faster |
| complex(a_complex)                  | 20.2 ns | 19.3 ns: 1.04x faster |
| SubComplex(a_float)                 | 41.4 ns | 40.6 ns: 1.02x faster |

Not significant: float(a_float) (control).

```python3
import pyperf

runner = pyperf.Runner()
setup = """
class SubComplex(complex): pass
a_float = 1.5; an_int = 3; a_complex = 1+2j; a_str = '1+2j'
"""
for stmt in [
    "complex()",
    "complex(a_float)",
    "complex(an_int)",
    "complex(a_complex)",
    "complex(a_str)",
    "complex(a_float, a_float)",
    "complex(a_float, imag=a_float)",
    "complex(real=a_float, imag=a_float)",
    "SubComplex(a_float)",
    "float(a_float)",
]:
    runner.timeit(stmt, stmt, setup=setup)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change is ok and small enough. I suspect in most performance critical applications this is not in the hot path (arithmetic ops like x + y do not hit the complex_subtype_from_c_complex.

@StanFromIreland
StanFromIreland merged commit 1330e91 into python:main Nov 22, 2026
60 of 64 checks passed
@cmaloney
cmaloney deleted the landable/complex-ctor-freelist branch November 22, 2026 18:11
@skirpichev

Copy link
Copy Markdown
Member

arithmetic ops like x + y do not hit the complex_subtype_from_c_complex

They hit PyComplex_FromCComplex directly, for result. So, I don't expect too much for benchmarks from #135233.

But LGTM, FWIW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants