[Python-Dev] Strategy for converting the decimal module to C

Raymond Hettinger rhettinger at ewtllc.com
Tue Jul 18 22:37:36 CEST 2006


I briefly had a chance to look at some of the work being done on a C 
implementation of decimal, and it looks like the approach is following 
the Python version too literally.

Ideally, it should be written as if Python were not involved and 
afterwards add the appropriate method wrappers.  Contexts should be 
small structures that include the traps and flags as bitfields.  
Likewise, the internal representation of a decimal should be in a simple 
structure using a byte array for the decimal digits -- it should not be 
a Python object.  Internally, the implementation creates many temporary 
decimals for a scratchpad, it makes many copies of contexts, it 
frequently tests components of a context, and the functions frequently 
call each other.  Those operations need to be as cheap as possible -- 
that means no internal tuple objects, no ref counting, and no passing 
variables through tuples of arguments, etc.

I recommend writing most of the module to be independent of the Python C 
API.  After a working implementation is built, grafting on the wrappers 
should be a trivial step.  Unless we stay true to this course, the code 
will end-up being unnecessarily complex and the performance will be 
disappointing.


my-two-cents,


Raymond


P.S.  The dictionary approach to context objects should likely be 
abandoned for the C version.  If the API has to change a bit, then so be it.


More information about the Python-Dev mailing list