Repository navigation
gh-162428: Use the freelist for exact complex() constructor results - #162429
Merged
StanFromIreland merged 1 commit intoNov 22, 2026
Merged
Conversation
…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
approved these changes
Nov 22, 2026
eendebakpt
left a comment
Contributor
There was a problem hiding this comment.
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
approved these changes
Nov 22, 2026
Member
They hit PyComplex_FromCComplex directly, for result. So, I don't expect too much for benchmarks from #135233. But LGTM, FWIW. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Not significant: float(a_float) (control).
cc: @skirpichev @eendebakpt