Open da-woods opened 2 years ago
I can see why this doesn't work - OptimizeBuiltinCallsTransform
can't easily change the return type. There's a similar failure to optimize in cdef int y = x + 1
The transform also handles CoerceFromPyTypeNode and calls coerce_to() in some cases. As long as there is a known C integer result type (so that we can safely assume a valid integer range for the result), coercion could change the return type after the fact by replacing the implementation again.
Is your feature request related to a problem? Please describe.
Suppose you have a comparison of an untyped variable with an int
Cython assumes that
x
is likely an int and has an optimized function to compare it to the C value1
(__Pyx_PyInt_EqObjC
). However this function returns a PythonTrue
/False
object. In this case (which I imagine is fairly common) the value is fed to anif
statement and so is immediately converted to a C True/False value:Describe the solution you'd like
It'd be nice to be able to skip this PyObject intermediate. It'd be needed only in the fallback case right at the end of
__Pyx_PyInt_EqObjC
.I imagine there's other similar optimized comparison functions that would also benefit from skipping the PyObject intermediate in the event that they're fed immediately to a C if statement (but I haven't looked in detail).