[PEAK] Re: [Twisted-Python] Components

Phillip J. Eby pje at telecommunity.com
Thu Feb 26 17:06:09 EST 2004


At 03:41 PM 2/26/04 -0500, Bob Ippolito wrote:
>PyProtocols has written-in-C code for acceleration, I bet it wins, even 
>though it does do more work (which I believe happens *up front*, so 
>doesn't really affect runtime performance) with transitive adaptation.

Adapter lookup time in PyProtocols is roughly equivalent to 5 attribute 
lookups: __conform__, __adapt__, __class__, __mro__ or __bases__, and 
finally the adapter itself.  All of the transitive computation occurs up 
front, so the actual adapter lookup consists of looking up each class in 
the adaptee's __mro__ in a single dictionary.  (IOW, it's basically the 
same as a Python attribute lookup, which walks the __mro__ and does a 
dictionary lookup for each base class until the attribute is 
found.)  Adapter execution time is of course dependent on what the adapter 
does.  :)

The C code achieved about a 2-4x speedup over pure Python, mainly by 
cutting out function call and loop overhead, not by changing the 
fundamental algorithm, which can't really be improved upon without making 
assumptions I didn't want to make.  For example, if I were willing to 
assume that classes would never have their __bases__ or __mro__ changed, I 
could cache lookup results and thus occasionally save a few dictionary 
lookups.  However, for a general-purpose Python tool, I thought it better 
to support as much of Python's dynamic nature as practical.  (I *do* assume 
that protocol relationships are not dynamic, however.)




More information about the PEAK mailing list