[Cython] Supporting cython.operator in C

Robert Bradshaw robertwb at gmail.com
Thu Jun 2 15:12:14 EDT 2016


On Thu, Jun 2, 2016 at 12:57 AM, Jeroen Demeyer <jdemeyer at cage.ugent.be> wrote:
> On 2016-06-02 05:43, Robert Bradshaw wrote:
>>>
>>> 2. "x || y" because Cython's "x or y" generates complicated code which
>>> isn't
>>> optimized as well as "x || y".
>>
>> I just tried using gcc -O3 and the resulting assembly is identical.
>
> Well, that would depend on what x and y are.
>
> A very concrete example:
>
> "if isinstance(x, (int,long))", which is parsed as "if PyInt_Check(x) or
> PyLong_Check(x)" is slightly slower than "if PyInt_Check(x) ||
> PyLong_Check(x)".

That is very surprising. How much slower? If this is the case, we
should be trying to fix Cython's code generation rather than adding
extra operators like || that one needs to know to use. I wonder if
this is due to gcc's expectations about the likelyhood of truth values
of the subexpressions in a logical or vs. an if...

For both assign and or, the problem boils down to the fact that
subexpressions in Python do not necessarily map to subexpressions in
C. If operator.assign(a, b) is simply syntactic sugar for "a = b" and
operator.logical_or syntactic sugar for "a or b" then there is no
advantage to providing them as attributes of operator. On the other
hand, if one wants to just "drop in" the C operators "=" and "||" then
consider

   a[i].x = foo()
   if b or a[i].x:
       ...

This would translate to

  [statements extracting a[i].x into tmp1]
  tmp1 = foo();
  [statements extracting a[i].x into tmp2 with side effects]
  if (b || tmp2) {
    ...
  }

The trouble is, in both cases one needs to come up with a *C
expression* representing a[i].x (and for assignment it must be an
lvalue) and add extra logic forbidding this to be assigned to a
temporary. This is not always (or even often?) possible, especially
when a is an arbitrary Python object.

Saying these operators are only allowed when it's possible, and having
extra logic to not store any intermediates in temporaries in that
case, makes the language inconsistent.

- Robert


More information about the cython-devel mailing list