Python is about to get faster

Python has a lot of great qualities— its versatility, possession of thousands of useful libraries, simplicity— but speed is not one of them.

This is about to change in Python 3.11, still in its first beta phase ahead of its release later this year. Mark Shannon, Core Python (CPython) revealed details about this project at the PyCon 2022 conference earlier this week where progress on the goal of running Python code in the browser was shown.

In 2021, Microsoft funded a project for the Python Software Foundation (PSF) headed by Python creator Guido van Rossum and Shannon to multiply Python’s speed by 2. The goal is to push Python towards the performance of C.

Van Rossum was hired by Microsoft in 2020 with the option to carry out whatever project he chose. At PyCon 2021 Conference, he said he ‘chose to go back to my roots’ and would work on Python’s inefficiencies.

Speed doesn’t seem to be hindering Python’s progress since its adoption has been massively increased because of machine learning and data science thanks to Pandas, Tensor Flow and other platforms. These platforms are downloaded millions of times in a month and used in places not restricted by hardware.

The Faster CPython Project disclosed some details about CPython 3.11’s performance over the past year. The project revealed some results comparing the 3.11 beta preview to 3.10 on numerous performance metrics. It showed that 3.11 is 1.25 times faster than 3.10.

Shannon believes the improvements can extend Python’s use to more virtual machines.

‘Python is widely acknowledged as slow. Whilst Python will never attain the performance of low-level languages like C, Fortran, or even Java, we would like it to be competitive with fast implementations of scripting languages, like V8 for Javascript or luajit for lua’, he said last year in the Python Enhancement Proposal (PEP) 659.

‘Specifically, we want to achieve these performance goals with CPython to benefit all users of Python including those unable to use PyPy or other alternative virtual machines.’

The main approach detailed in PEP 659 is a ‘specializing, adaptive interpreter that specializes code aggressively, but over a small region, and is able to adjust to mis-specialization rapidly and at low cost.’

Optimizations for VMs are often ‘expensive’ and requiring long ‘warm up’ time. To avoid this, the VM should ‘speculate that specialization is justified even after a few executions of a function.’ This way, the interpreter just needs to optimize and de-optimize regularly and cheaply.

This would result in a faster interpreter for CPython that tracks individual bytecodes during the execution of a program. The development of the new interpreter is almost finished but still needs completion of dynamic specializations for loops and binary operations.

Also, memory consumption in 3.11 hasn’t changed from 3.10.

Shannon suggested that a Just-In-Time (JIT) compiler for Python’s performance is not a priority and would probably not arrive until Python 3.13.

According to the Faster Python implementation plan, CPython 3.12 might gain a ‘simple JIT compiler for small regions’ which compiles little regions of specialized code. 3.13 on the other hand, would improve the compiler to extend the regions for compilation.

By Marvellous Iwendi.

Source: ZDNet