Implementing a prototype HPy backend for Cython: improving Python extension performance
Thesis / Dissertation
2025
Permanent link to this Item
Authors
Supervisors
Journal Title
Link to Journal
Journal ISSN
Volume Title
Publisher
Publisher
University of Cape Town
Department
Faculty
License
Series
Abstract
The python programming language has become widely used, including in fields such as scientific computing and artificial intelligence. However, the original and most popular implementation of Python, CPython, is often criticised for its slow performance as compared to other programming languages. Alternate implemen-tations of Python, such as GraalPy, aim to improve the performance of Python coPython programming mpared to CPython, with optimisations such as Just-in-Time compilers and more complex garbage collectors. However, these alternate implementations have worse performance than CPython when using extension modules written in C or C++ using Python's C API, which is closely tied to the CPython implementation. HPy is a proposed alternative to the C API for writing C or C++ extension mod-ules for Python. HPy is implementation-neutral and does not have the same poor performance on non-CPython implementations as the C API. However, Cython, a program that compiles Python code to a C extension module, unfortunately does not yet support HPy. Cython allows users to generate C extensions without using the C API directly, which has made it popular, so HPy support in Cython would have potential benefits, such as automatic generation of HPy extensions. Here we describe the implementation and testing of a prototype HPy backend for Cython, with the aim of improving the performance of extensions generated with Cython on the GraalPy Python implementation. Our HPy backend replaces C API functions, variables, and macros generated by Cython with macros that map to either the C API or HPy. This allows files generated by Cython to compile to either the C API or HPy, while not increasing the amount of code generated. To compare performance, four simple benchmarks testing key aspects of performance were compiled into extension modules with Cython using either our HPy backend or the original C API backend and then run on either CPython or GraalPy. We found that, while HPy can improve extension module performance on GraalPy, HPy modules performed worse on CPython than the C API modules. This is likely due to optimisations in Cython for the C API which are not replicable in HPy, such as directly accessing the memory layout of Python objects. We also identified aspects of HPy that severely impact its performance. Overall, we find that HPy has significant potential advantages over the C API, but requires further improvements to be competitive with the C API on CPython.
Description
Keywords
Reference:
Spies, D.T. 2025. Implementing a prototype HPy backend for Cython: improving Python extension performance. . University of Cape Town ,Faculty of Science ,Department of Computer Science. http://hdl.handle.net/11427/42703