[Python-Dev] Returning int instead of long from struct when possible for performance

Bob Ippolito bob at redivi.com
Fri May 26 00:58:21 CEST 2006

Python ints are a heck of a lot faster to work with than Python longs  
and have the additional benefit that psyco <http:// 
psyco.sourceforge.net> can optimize the hell out of int but can't do  
anything at all for long. This is important because psyco is  
currently in pretty wide-spread use amongst people who need to  
squeeze every bit of performance out of their Python programs, and  
often yields 4x or better performance improvement in real-world tests.

A more far reaching change would be to add a new PyInteger_From* API  
for code that doesn't care if long or int is returned, but I don't  
see much of a reason to go that far at this point since the rest of  
the standard library wouldn't benefit much.

Unfortunately, this change to the struct module slightly alters the  
documented API for the following format codes: I, L, q, Q. Currently  
it is documented that those format codes will always return longs,  
regardless of their value.

I've prototyped this change on the trunk (behind a currently  
undefined PY_USE_INT_WHEN_POSSIBLE macro). The standard library test  
suite passes with this enabled, but it would break any doctests in  
third party code that check the result of an unpack operation with  
one of those format codes. Given the interchangeability of int and  
long, I don't foresee any other complications with this change.



More information about the Python-Dev mailing list