Skip to content

[mypyc] Specialize .real and .imag on complex, float, int and bool - #22099

Open
rheard wants to merge 3 commits into
python:masterfrom
rheard:fix-mypyc-1228
Open

rheard wants to merge 3 commits into
python:masterfrom
rheard:fix-mypyc-1228

Conversation

@rheard

@rheard rheard commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1228

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)
  • if isinstance(x, (float, complex)): real, imag = x.real, x.imag with x: Any: 93.9 -> 52.7, or 6.6 together with [mypyc] Add isinstance primitives for complex, type, range, slice and memoryview #22097

* ``f.real``
* ``f.imag``

These are also fast for ``complex`` values and for unions such as

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
These are also fast for ``complex`` values and for unions such as
These are fast both for ``complex`` values and for unions such as

Comment thread mypyc/lib-rt/float_ops.c
Comment on lines +257 to +258
// Convert the real part of an int to a C double (see CPyComplex_Real)
double CPyComplex_LongAsDouble(PyObject *o) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this function is more general than the name/comment suggest, maybe rename to something like CPyDouble_FromLong? also a nit but real part of an int doesn't make sense, there's no other part.

@rheard rheard Oct 7, 2026 •

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.

Sure, I agree. I also thought it seemed more generic than the name suggested and was hoping there was a method I could just use... I'll think of a better name.

To your point around "real part of an int" doesn't make sense; I completely agree, and it makes even less sense for a bool, however this is valid interpreted Python (as uncomfortable as it makes me):

>>> a = int(10)
>>> a.real
10
>>> a.imag
0
>>>
>>> b = True
>>> b.real
1
>>> b.imag
0

EDIT: Obviously the imag part just returns 0 and the real part just returns the number itself, which is what this method recreates.... I was more adding it for complex, and just for all the other number types for completeness.

Comment on lines +370 to +371
if is_tagged(obj.type) or is_bool_or_bit_rprimitive(obj.type):
# Unboxed ints are always exact ints, and the real part of a bool is an int

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

a tagged integer can still be a generic python object for large ints so i think we'd need a runtime check here because the generic object might be a subclass that overrides real or imag.

return builder.coerce(obj, int_rprimitive, line) if is_real else builder.load_int(0, line)
if is_float_rprimitive(obj.type):
# Unboxed floats are always exact floats, so these can't be overridden
return obj if is_real else Float(0.0, line)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

found by codex: reusing the obj register can break evaluation order of expressions

x = 1.0
print(x.real + (x := 2.0))  # 4.0

might also need a new register for the return value of builder.coerce above.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.real and .imag on complex, float, int and bool values compile to generic attribute lookups

2 participants