[Cython] Typing of Python objects?

Robert Bradshaw robertwb at math.washington.edu
Wed Jan 30 21:40:04 CET 2008


We have added runtime type inference for some cases (e.g. tuple  
indexing) where the specialized function is significantly faster (for  
example a macro) compared to a generic call. However, too much of  
this can actually slow things down.

What we definitely would like to provide in the future are special  
types for dict, list, tuple (and maybe some others), so one could write

cdef foo(x, dict mydict):
     mydict[x] = 5

which would use the PyDict_SetItem method directly. This would raise  
a TypeError if mydict was not exactly a python dictionary object.

- Robert


On Jan 30, 2008, at 8:26 AM, Stefan Behnel wrote:

> Hi,
>
> Martin C. Martin wrote:
>> Are there any plans to add typing and type inference to Python  
>> objects
>> in Cython?  For example, it would be great if the following function
>> could call PyDict_SetItemString:
>>
>> cdef foo(char *string, mydict):
>>      mydict[string] = 5
>
> Note that SetItemString() is not as fast as it might look, as it  
> creates a
> Python string internally.
>
>
>> Of course, we'd need some way to indicate that mydict is a dict.
>> WingIDE uses this:
>>
>> cdef foo(char *string, mydict):
>>    isinstance(mydict, dict)
>>    mydict[string] = 5
>>
>> But in a pinch, this would do:
>>
>> cdef foo(char *string, mydict):
>>    if isinstance(mydict, dict):
>>      mydict[string] = 5
>>
>> Or
>>
>> cdef foo(char *string, mydict):
>>    assert isinstance(mydict, dict)
>>    mydict[string] = 5
>
> None of these is enough. You might just as well have a subclass of a
> dictionary, in which case calling PyDict_SetItem() might not do the  
> right
> thing. Cython could, however, add this optimisation based on
> "PyDict_CheckExact()" at runtime - although that would break  
> compatibility to
> Python 2.3... (ok, minus #ifdef's)
>
> I could imagine adding checks for exact types like this: for lists  
> and dicts
> on item assignments, and additionally for tuples on item access.  
> Cython
> already does things like these in a couple of places. However, it  
> can become
> expensive if we add too many conditional branches into simple  
> instructions,
> both in terms of time (CPU pipeline and branch prediction) and  
> space (code
> size, CPU cache). I do not know how this compares to the  
> performance of the
> generic calls.
>
>
>> For now, I guess I need to do a cdef extern and call
>> PyDict_SetItemString explicitly?
>
> Yes, this is how it currently works (and how it definitely works  
> best).
>
> Stefan
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev



More information about the Cython-dev mailing list