Conversation
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.
Fixes mypyc/mypyc#1228
Reading
.realor.imagfrom a value typed asint,float,complex, or a union of them was compiled to a generic attribute lookup, whose result was then unboxed or type-checked again. Nativeint,boolandfloatvalues 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,complexor a union of them, which includes values narrowed byisinstance():int,boolandfloatvalues,x.realis the value itself (converted to anintfor abool), andx.imagis0or0.0.float(receivers typedfloat,complexorfloat | complex), newCPyComplex_RealandCPyComplex_Imagprimitives read exactcomplex,floatandintobjects directly as a C double.int(receivers such asint | float | complex), newCPyNumber_RealandCPyNumber_Imagprimitives return exactintandfloatobjects themselves, asint.realandfloat.realdo, and read exactcomplexobjects directly.Anything else (a subclass that overrides
realorimag, aboolin 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.imagwithz: complex: 59.3 -> 6.0z: complex: 55.8 -> 7.1x.real + x.imagwithx: float: 49.9 -> 6.0x.real + x.imagwithx: int: 41.8 -> 9.9x.realandx.imagwithx: 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.imagwithx: Any: 93.9 -> 52.7, or 6.6 together with [mypyc] Add isinstance primitives for complex, type, range, slice and memoryview #22097