This is a duplicate of #22099. I'm opening this due to Github issues yesterday. This includes patches due to @p-sawicki's review
Reading .real or .imag from a value typed as int, float, complex, or a union of them was compiled to a generic attribute lookup, whose result was then unboxed or type-checked again. Native int, bool and float values were even boxed first, only to read back the same value or zero. I ran into this in the same code as #22097.
This specializes these reads when the static type of the receiver is int, float, complex or a union of them, which includes values narrowed by isinstance():
On native int, bool and float values, x.real is the value itself (converted to an int for a bool), and x.imag is 0 or 0.0.
When the result is a float (receivers typed float, complex or float | complex), new CPyComplex_Real and CPyComplex_Imag primitives read exact complex, float and int objects directly as a C double.
When the result can be an int (receivers such as int | float | complex), new CPyNumber_Real and CPyNumber_Imag primitives return exact int and float objects themselves, as int.real and float.real do, and read exact complex objects directly.
Anything else (a subclass that overrides real or imag, a bool in a union, or an object of the wrong type at runtime) uses the same lookup as before, followed by the same unboxing or type check, so results and error messages don't change.
The run tests cover NaN, infinities, -0.0, a value equal to mypyc's float error value (-113.0), small and large ints, bools, subclasses that override the properties, and the error cases.
Microbenchmark results (Python 3.12, ns per value):
z.real + z.imag with z: complex: 59.3 -> 6.0
the same with ints passed as z: complex: 55.8 -> 7.1
x.real + x.imag with x: float: 49.9 -> 6.0
x.real + x.imag with x: int: 41.8 -> 9.9
x.real and x.imag with x: int | float | complex: 29.6 -> 8.1 with ints, 36.9 -> 13.0 with floats, 46.3 -> 18.8 with complex values (35.0 -> 25.7 when the three alternate)
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
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.
Fixes mypyc/mypyc#1228
This is a duplicate of #22099. I'm opening this due to Github issues yesterday. This includes patches due to @p-sawicki's review
Reading .real or .imag from a value typed as int, float, complex, or a union of them was compiled to a generic attribute lookup, whose result was then unboxed or type-checked again. Native int, bool and float values were even boxed first, only to read back the same value or zero. I ran into this in the same code as #22097.
This specializes these reads when the static type of the receiver is int, float, complex or a union of them, which includes values narrowed by isinstance():
Anything else (a subclass that overrides real or imag, a bool in a union, or an object of the wrong type at runtime) uses the same lookup as before, followed by the same unboxing or type check, so results and error messages don't change.
The run tests cover NaN, infinities, -0.0, a value equal to mypyc's float error value (-113.0), small and large ints, bools, subclasses that override the properties, and the error cases.
Microbenchmark results (Python 3.12, ns per value):