High Performance Python C+ Bindings with PyPy and Cling
The use of Python as a high level productivity language on top of high performance libraries written in C+ requires efficient, highly functional, and easy to use cross language bindings. C+ was standardized in 1998 and up until 2011 it saw only one minor revision. Since then, the pace of revisions has increased considerably, with a lot of improvements made to expressing semantic intent in interface definitions. For automatic Python C+ bindings generators it is both the worst of times, as parsers need to keep up, and the best of times, as important information such as object ownership and thread safety can now be expressed. We present cppyy, which uses Cling, the Clang/LLVM based C+ interpreter, to automatically generate Python C+ bindings for PyPy. Cling provides dynamic access to a modern C+ parser and PyPy brings a full toolbox of dynamic optimizations for high performance. The use of Cling for parsing, provides up to date C+ support now and in the foreseeable future. We show that with PyPy the overhead of calls to C+ functions from Python can be reduced by an order of magnitude compared to the equivalent in CPython, making it sufficiently low to be unmeasurable for all but the shortest C+ functions. Similarly, access to data in C+ is reduced by two orders of magnitude over access from CPython. Our approach requires no intermediate language and more pythonistic presentations of the C+ libraries can be written in Python itself, with little performance cost due to inlining by PyPy. This allows for future dynamic optimizations to be fully transparent.
2016 6th Workshop on Python for High Performance and Scientific Computing (PyHPC)