[Python-Dev] C-level duck typing

Dag Sverre Seljebotn d.s.seljebotn at astro.uio.no
Wed May 16 11:09:02 CEST 2012


On 05/16/2012 10:36 AM, "Martin v. Löwis" wrote:
>  > And, we want this to somehow work with existing Python; we still
>  > support users on Python 2.4.
>
> This makes the question out-of-scope for python-dev - we only discuss
> new versions of Python here. Old versions cannot be developed anymore
> (as they are released already).

Point taken. Sorry about that, and I appreciate your patience with me.

I guess my idea was that if some mechanism was approved for future 
Python versions, we would feel easier about hacking around older Python 
versions.

Of course, nothing is better than this not being a problem, as you seem 
to suggest. But:

>
>> typedef struct {
>> unsigned long extension_id;
>> void *data;
>> } PyTypeObjectExtensionEntry;
>>
>> and then a type object can (somehow!) point to an array of these. The
>> array is linearly scanned
>
> It's unclear to me why you think that a linear scan is faster than
> a dictionary lookup. The contrary will be the case - the dictionary
> lookup (PyObject_GetAttr) will be much faster.

I've benchmarked using a PyObject* as a function-pointer-capsule using 
the above mechanism; that added about 2-3 nanoseconds of overhead on 
what would be a 5 nanosecond call in pure C. (There will only be 1 or 2 
entries in that list...)

Dict lookups are about 18 nanoseconds for me, using interned string 
objects (see below). Perhaps that can be reduced somewhat, but I highly 
doubt you'll get to 3-4 nanoseconds?

Cython benchmark (which does translate do what you'd do in C):

def hammer_dict(int n):
     cdef dict the_dict

     a = "hello"
     b = "there"
     the_dict = {a : a, b : a}
     for i in range(n):
         the_dict[b]

Dag


More information about the Python-Dev mailing list