In 2004, Stein considered using SWIG for Sage extensively, and implemented several basic types using this model. The idea was to write code in C++ for Sage that needed to be fast, then wrap it in SWIG. This ground to a halt, because the result was not sufficiently fast. First, there is overhead when writing code in C++ in the first place. Second, SWIG generates several layers of code between Python and the code that does the actual work; for implementing any of Sage's numerous basic arithmetic types, e.g., integers, the overhead is completely unacceptable, at least if one wants code that is as fast as Magma and PARI. For example, in my initial implementation of integer arithmetic using SWIG and GMP, there were the following six levels between the Sage interpreter and the GMP C library:
Python code to provide a clean interface |
SWIG Autogenerated Python code |
SWIG Autogenerated C++ extension code |
Handcode C++ Integer class |
GMP's C++ Interface |
GMP C Library |
Cython generated C code that provides everything needed |
GMP C Library |
See About this document... for information on suggesting changes.